yang.h

Go to the documentation of this file.
00001 // classes for vectors and matrices
00002 // From D. Yang: C++ and Object-Oriented Numeric Computing,
00003 // Springer-Verlag, 2000
00004 // 
00005 #ifndef _YANG_H_
00006 #define _YANG_H_
00007 
00008 
00009 #include <iostream>
00010 
00011 
00012 class Mtx;
00013 
00014 class Vtr {       
00015   int lenth;               // number of entries in the vector
00016   double* ets;             // entries of the vector
00017 public: 
00018   Vtr(int, double*);       // costruct a vector with given entries
00019   Vtr(int, double = 0);    // a vector with all entries same
00020   Vtr(const Vtr&);         // copy constructor
00021   ~Vtr(){ delete[] ets; }  // destructor is defined inline
00022 
00023   Vtr& operator=(const Vtr&);                        // copy assignment 
00024   Vtr& operator+=(const Vtr &);                      // v += v2
00025   Vtr& operator-=(const Vtr &);                      // v -= v2
00026   double& operator[](int i) const { return ets[i]; } // eg v[i] = 10;
00027   double maxnorm() const;                            // maximum norm
00028   double twonorm() const;                            // L-2 norm
00029   int size() const { return lenth; }                 // return length of vector
00030 
00031   friend Vtr operator+(const Vtr&);                  // unary +, v = + v2
00032   friend Vtr operator-(const Vtr&);                  // unary -, v = - v2
00033   friend Vtr operator+(const Vtr&, const Vtr&);      // binary +, v = v1+v2
00034   friend Vtr operator-(const Vtr&, const Vtr&);      // binary -, v = v1-v2
00035   friend double dot(const Vtr&, const Vtr&);         // dot product
00036   friend Vtr operator*(const double, const Vtr&);    // vec-scalar x 
00037   friend Vtr operator*(const Vtr&, const double);    // vec-scalar x
00038   friend Vtr operator*(const Vtr&, const Vtr&);      // component-wise multiply
00039   friend Vtr operator*(const Vtr&, const Mtx&);      // vec-matrix x
00040   friend Vtr operator/(const Vtr&, const double);
00041   friend std::ostream& operator<<(std::ostream&, const Vtr&); // output operator
00042 };
00043 
00044 inline Vtr operator*(const Vtr & v, const double scalar) {
00045   return scalar*v; 
00046 }
00047 
00048 class Mtx {
00049 private: 
00050   int nrows;                                   // number of rows in the matrix 
00051   int ncols;                                   // number of columns in  matrix 
00052   double** ets;                                // entries of the matrix
00053 public: 
00054   Mtx(int n, int m, double**);                 // construct an n by m matrix
00055   Mtx(int n, int m, double d = 0);             // all entries equal d
00056   Mtx(const Mtx &);                            // copy constructor
00057   ~Mtx();                                      // destructor
00058 
00059   Mtx& operator=(const Mtx&);                  // overload =
00060   Mtx& operator+=(const Mtx&);                 // overload +=
00061   Mtx& operator-=(const Mtx&);                 // overload -=
00062   Vtr operator*(const Vtr&) const;             // matrix vector multiply
00063   double* operator[](int i) const { return ets[i]; }      // subscripting, row i
00064   
00065   // Implement plus as a member fcn. minus could also be so implemented.
00066   // But I choose to implement minus as a friend just to compare the difference 
00067   Mtx& operator+();                            // unary +, eg, mat1 = + mat2
00068   Mtx operator+(const Mtx&);                   // binary +, eg, m = m1 + m2
00069   friend Mtx operator-(const Mtx&);            // unary -, eg, m1 = -m2
00070   friend Mtx operator-(const Mtx&,const Mtx&); // binary -, eg, m = m1 - m2
00071 
00072   // Added by Peter Seidler...
00073   int rows() const {return nrows;}
00074   int cols() const {return ncols;}
00075   void getcol(int i, Vtr& vec) const;
00076   void setcol(int i, const Vtr& vec);
00077   void clear();
00078   int transpose(Mtx& dest) const;
00079   Mtx operator*(const Mtx&) const; 
00080   friend Vtr operator*(const Vtr&, const Mtx&);    // vec-matrix x
00081   const double& operator()(int i, int j) const {return ets[i][j];}
00082   double& operator()(int i, int j) {return ets[i][j];}
00083   friend std::ostream& operator<<(std::ostream&, const Mtx&);   
00084 
00085   int QRdecomp(Mtx& Q, Mtx& R);
00086   int QRdecomp_slow(Mtx& Q, Mtx& R);
00087 };
00088 
00089 typedef Vtr Vec_DP;
00090 typedef Mtx Mat_DP;
00091 
00092 #endif // _YANG_H_
Generated on Wed May 26 16:21:15 2010 for OverSim by  doxygen 1.6.3