Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

Basemat.hh

Go to the documentation of this file.
00001 #ifndef BASEMAT_TMPL_HEADER
00002 #define BASEMAT_TMPL_HEADER
00003 
00004 // ==== HEADER Basemat.hh ====
00005 
00011 // 8-Dec-2000. Andras Aszodi
00012 
00013 // ---- COMPATIBILITY NOTES ----
00014 
00015 // Exception support is assumed.
00016 // This module depends on the Standard Template Library.
00017  
00018 // ---- COMPILER COMMAND LINE MACROS ----
00019 
00020 // Define the macro INCLUDE_TMPL_DEFS if your compiler
00021 // does not support automatic template source inclusion.
00022 
00023 // ---- STANDARD HEADERS ----
00024 
00025 #include <stdlib.h>
00026 #include <math.h>
00027 
00028 #ifdef _STANDARD_C_PLUS_PLUS
00029 #include <iostream>
00030 #include <sstream>
00031 #include <vector>
00032 #include <algorithm>
00033 #include <numeric>
00034 #include <functional>
00035     using namespace std;
00036 #else
00037 #include <iostream.h>
00038 #include <strstream.h>
00039 #define istringstream istrstream 
00040 #define ostringstream ostrstream 
00041 #include <vector.h>
00042 #include <algo.h>
00043 #include <function.h>
00044 #endif
00045 
00046 // ---- MODULE HEADERS ----
00047 
00048 #include "Refstorage.hh"
00049 #include"Vector.hh"
00050 #include "Rangedimexc.hh"
00051 
00052 // ==== CLASSES ====
00053 
00054 namespace RazorBack {
00055 
00063 template <class T_>
00064 class Matstorage_
00065 {
00066     // data
00067     vector<T_> Elems;   // STL vector of matrix elements
00068     size_t Eno;  // total no. of elements
00069     
00070     // methods
00071     public:
00072     
00074     explicit Matstorage_(size_t N=0): Elems(N), Eno(N) {}
00075     
00077     const vector<T_>& elems() const { return Elems; }
00078     vector<T_>& elems() { return Elems; }
00079     size_t eno() const { return Eno; }
00080     size_t eno(size_t Enew)
00081     {
00082         if (Enew==Eno) return Eno;
00083         size_t Eold=Eno;
00084         if (Enew>Eold)
00085             Elems.insert(Elems.end(), Enew-Eold, T_(0));    // grows
00086         else
00087             Elems.erase(Elems.begin()+Enew, Elems.end());   // shrinks
00088         Eno=Enew;
00089         return Eold;
00090     }
00091 };
00092 // END OF CLASS Matstorage_
00093 
00101 template <class T_>
00102 class Basemat_: protected Refstorage_< Matstorage_<T_> >
00103 {
00104 
00105     // data
00106     bool Ftnidx;        //< use [1..N] FORTRAN indexing when true, see operator()
00107     bool Check;         //< enable range checking when true, see operator()
00108     
00110     public:
00111     
00113     Basemat_(): Ftnidx(false), Check(false) {}
00114     
00116     virtual ~Basemat_() {}
00117         
00119     
00124     virtual size_t rno() const =0;
00125     virtual size_t cno() const =0;
00126     
00133     virtual typename vector<T_>::const_iterator operator[](size_t Idx) const =0;
00134     virtual typename vector<T_>::iterator operator[](size_t Idx) =0;
00135     virtual const T_& operator()(size_t Ridx, size_t Cidx) const =0;
00136     virtual T_& operator()(size_t Ridx, size_t Cidx) =0;
00137     
00143     virtual Vector_<T_> row(size_t Idx) const =0;
00144     virtual void row(const Vector_<T_>& Vec, size_t Idx) =0;
00145     virtual Vector_<T_> col(size_t Idx) const =0;
00146     virtual void col(const Vector_<T_>& Vec, size_t Idx) =0;
00147     
00149     Basemat_<T_>& set_values(const T_& X=T_(0))
00150     {
00151         clone();
00152         fill(elems().begin(), elems().end(), X);
00153         return(*this);
00154     }
00155 
00161     bool ftn_idx() const { return Ftnidx; }
00162     bool ftn_idx(bool Fi) { bool Of=Ftnidx; Ftnidx=Fi; return Of; }
00163     
00169     bool range_check() const { return Check; }
00170     bool range_check(bool Ci) { bool Oc=Check; Check=Ci; return Oc; }
00171     
00177     Basemat_<T_>& operator*=(const T_& Factor);
00178     Basemat_<T_>& operator/=(const T_& Div);
00179     
00181     
00186     virtual Vector_<T_> diag() const =0;
00187     virtual void diag(const Vector_<T_>& Vec) =0;
00188 
00193     virtual void diag_matrix(const T_& Dval) =0;
00194 
00196     virtual T_ trace() const =0;
00197 
00204     void transform(unary_function<T_, T_> Unaryfunc)
00205     {
00206         clone();
00207         transform(elems().begin(), elems().end(),
00208                 elems().begin(), Unaryfunc);
00209     }
00210 
00211         // I/O
00212     
00219     virtual void read_matrix(istream& In) =0;
00220     
00227     virtual void list_matrix(ostream& Out, 
00228             unsigned int Prec=6, unsigned int Width=13, 
00229             unsigned int Linewidth=80) const;
00230 
00231     // hidden methods
00232     protected:
00233             
00234     // direct data access (no non-const cloning!)
00235     const vector<T_>& elems() const { return data().elems(); }
00236     vector<T_>& elems() { return data().elems(); }
00237     const T_& elems(size_t Idx) const { return data().elems()[Idx]; }
00238     T_& elems(size_t Idx) { return data().elems()[Idx]; }
00239     size_t eno() const { return data().eno(); }
00240     size_t eno(size_t E) { return data().eno(E); }
00241 
00242     virtual void print_rows(ostream& Out, size_t Rw, size_t Jbeg, 
00243         size_t Items, size_t Width, size_t Prec) const =0;
00244 
00245     private:
00246     static size_t prt_width(int N) { return int(ceil(log10(double(N)))); }
00247 };
00248 // END OF CLASS Basemat_
00249 
00250 // ---- OUT-OF-CLASS FUNCTIONS ----
00251 
00252 // These were originally friend templates but EGCS 1.1.2
00253 // did not like them. 12-Jul-1999.
00254 
00256 template <class T_>
00257 istream& operator >> (istream& In, Basemat_<T_>& Mat)
00258 {
00259     Mat.read_matrix(In); return(In);
00260 }
00261 
00263 template <class T_>
00264 ostream& operator << (ostream& Out, const Basemat_<T_>& Mat)
00265 {
00266     Mat.list_matrix(Out); return(Out);
00267 }
00268 
00269 } // RazorBack
00270 
00271 // ==== END OF HEADER Basemat.hh ====
00272 
00273 #ifdef INCLUDE_TMPL_DEFS
00274 #include "Basemat.cc"
00275 #endif
00276 
00277 #endif      // BASEMAT_TMPL_HEADER

Generated at Wed Aug 21 09:33:22 2002 for The Razorback C++ Library: Linear Algebra by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001