Derived Class Derived class and Base class baseclass
Derived Class
Derived class and Base class base_class 1 { …. . }; class base_calss 2 { …. . }; class derived_class : public base_class 1, public base_class 2 { double x, y, z; // constructors derived_class (double r 1, double r 3) : base_class 1(r 1, r 2), base_class 2(r 3); };
Properties of a derived class 1. Derived class 擁有 base class 的所有 members 和 member functions. 2. 在建構 derived class 的 constructor 時, 也必須賦予 base class 的建 構規則. 3. Derived class 可以經由 base class 的運算符作 + - * = 等運算. 例如: dsq. Matrix a. Mtx(n, xarray); dco. Vector c. Vct(n, yarray); dro. Vector r. Vct(n , yaary); cout << (a. Mtx*c. Vct) ; // print (1 x n) matrix; cout << (r. Vct*Amtx); // print (n x 1) matrix // 運算 a. Mtx * c. Vct 是藉由 Matrix(n, n) * Matrix(n, 1) 的規則進行. // 運算 r. Vct * a. Mtx 是藉由 Matrix(1, n) * Matrix(n, n) 的規則進行. // cout 也是經由 Matrix 的 overload 進行.
Class Matrix Hermitian Matrix Column Vectors General (n x m) matrix Row vectors Square (nxn) Matrix Define maritrx +, -, * Operations & transport
Class gnr. Matrix template <class XTP> class gnr. Matrix { protected: int n_row, n_col; XTP *a_mtx; public: // constructors …. . }
Better structure derived class dspmatrix. h dsq. Matrix : (n x n) real matrix dro. Vector : (1 x n) real vector dco. Vector : (n x 1) real vector cspmatrix. h csq. Matrix : (n x n) complex matrix cro. Vector : (1 x n) complex vector cco. Vector : (n x 1) complex vector dgr. Matrix : (n x m) real matrix Operators between cgr. Matrix and dgr. Matrix And operators cgr. Matrix: (n x m) complex matrix And operators base class
Class dgr. Matrix class dgr. Matrix { protected: int n_row; int n_col; double* a_mtx; public: …. ; } Test program
constructors dgr. Matrix() {}; dgr. Matrix(const int, const int); dgr. Matrix(const int, double*); dgr. Matrix(const int, const double); dgr. Matrix(const dgr. Matrix &); // copy constructor ~dgr. Matrix() {delete [] a_mtx; };
Member functions void put (const int i, const int j, const double x) ; void put (const int i, const double x); double get (const int i, const int j) const; double get (const int i) const; double &pos (const int i, const int j) {return this->a_mtx[i*n_col+j]; } int ncol () const; int nrow () const; int ndim () const; dgr. Matrix transport () const; dgr. Matrix getrow (const int) const; void print (int) const; dgr. Matrix getcol (const int) const; double norm () const; void putrow (const int, double*); double abs () const; void putcol (const int, double*); dgr. Matrix unit () const; double* getarray () const;
i/o stream overload ostream &operator<<(ostream &ous, dgr. Matrix mtx) { int i, j, w=8; cout << " row = " << mtx. nrow() << " col = " << mtx. ncol() << "n"; for (i=0; i<mtx. nrow(); i++) { for (j=0; j<mtx. ncol(); j++) cout << setw(w) << mtx. get(i, j); cout << "n"; } return ous; } ifstream &operator>>(ifstream &ins, dgr. Matrix &mtx) { int i, j; for (i=0; i<mtx. nrow(); i++) { for (j=0; j<mtx. ncol(); j++) ins >> mtx. pos(i, j) ; } return ins; }
operators friend class cgr. Matrix; dgr. Matrix operator- (const dgr. Matrix &); dgr. Matrix operator+ (const dgr. Matrix &, const dgr. Matrix &); dgr. Matrix operator- (const dgr. Matrix &, const dgr. Matrix &); dgr. Matrix operator* (const double, const dgr. Matrix &); dgr. Matrix operator* (const dgr. Matrix &s, const double x); dgr. Matrix operator* (const dgr. Matrix &, const dgr. Matrix &);
Complex Matrix class cgr. Matrix { protected: int n_row; int n_col; complex<double> *a_mtx; public: ……; } Test code
constructors cgr. Matrix() {}; cgr. Matrix(const int, const int); cgr. Matrix(const int, complex<double> *); cgr. Matrix(const int, double *); cgr. Matrix(const int, const complex<double> ); cgr. Matrix(const int, const double ); cgr. Matrix(const cgr. Matrix &mx); cgr. Matrix(const dgr. Matrix &mx);
operators cgr. Matrix & operator=(const cgr. Matrix &); cgr. Matrix & operator=(const dgr. Matrix & mx); cgr. Matrix operator- (const cgr. Matrix &); cgr. Matrix operator+ (const cgr. Matrix &, const cgr. Matrix &); cgr. Matrix operator- (const cgr. Matrix &, const cgr. Matrix &); cgr. Matrix operator* (const complex<double> & , const cgr. Matrix &); cgr. Matrix operator* (const cgr. Matrix &, const cgr. Matrix &); cgr. Matrix operator+ (const dgr. Matrix &x 1, const cgr. Matrix &x 2); cgr. Matrix operator+ (const cgr. Matrix &x 1, const dgr. Matrix &x 2); cgr. Matrix operator- (const dgr. Matrix &x 1, const cgr. Matrix &x 2); cgr. Matrix operator- (const cgr. Matrix &x 1, const dgr. Matrix &x 2); cgr. Matrix operator* (const double &r, const cgr. Matrix &mx); cgr. Matrix operator* (const cgr. Matrix &mx, const double &r); cgr. Matrix operator* (const cgr. Matrix &mx, const complex<double> &r); cgr. Matrix operator* (const dgr. Matrix &m 1, const cgr. Matrix &m 2); cgr. Matrix operator* (const cgr. Matrix &m 1, const dgr. Matrix &m 2);
dspmatrix. h: real special matrices 1. Class dsq. Matrix: real square matrix (n x n) 2. Class dro. Vector: real row vector (1 x n) 3. Class dco. Vector: real column vector (n x 1).
Class dsq. Matrix class dsq. Matrix : public dgr. Matrix { public: dsq. Matrix() : dgr. Matrix() {}; dsq. Matrix(const int n) : dgr. Matrix(n, n){}; dsq. Matrix(const int n, const double x) : dgr. Matrix(n, n, x){}; dsq. Matrix(const int n, double *xpt) : dgr. Matrix(n, n, xpt) {}; dsq. Matrix(const dsq. Matrix &sqx) : dgr. Matrix(sqx. nrow(), sqx. ncol(), sqx. getarray()){}; dsq. Matrix(const dgr. Matrix &grx); // memeber functions double trace() const; //對角線元素的和 dsqmatrix diagonal() const; // 抽取對角線元素. .
Class dco. Vector class dro. Vector : public dgr. Matrix { public: dro. Vector(): dgr. Matrix(){}; dro. Vector(int n): dgr. Matrix(1, n){}; dro. Vector(int n, double x) : dgr. Matrix(1, n, x){}; dro. Vector(int n, double *xpt) : dgr. Matrix(1, n, xpt){}; dro. Vector(const dgr. Matrix &mtx); };
Class dco. Vector class dco. Vector : public dgr. Matrix { public: dco. Vector(): dgr. Matrix(){}; dco. Vector(int n): dgr. Matrix(n, 1){}; dco. Vector(int n, double x) : dgr. Matrix(n, 1, x){}; dco. Vector(int n, double *xpt) : dgr. Matrix(n, 1, xpt){}; dco. Vector(const dgr. Matrix &mtx); };
cspmatrix: complex special matrix 1. Class csq. Matrix: complex square matrix (n x n) 2. Class cro. Vector: complex row vector (1 x n) 3. Class cco. Vector: complex column vector (n x 1).
Class csq. Matrix class csq. Matrix : public cgr. Matrix { public: csq. Matrix() : cgr. Matrix() {}; csq. Matrix(const int n) : cgr. Matrix(n, n){}; csq. Matrix(const int n, const double x) : cgr. Matrix(n, n, x){}; csq. Matrix(const int n, const complex<double> x) : cgr. Matrix(n, n, x){}; csq. Matrix(const int n, double *xpt) : cgr. Matrix(n, n, xpt) {}; csq. Matrix(const int n, complex<double> *xpt) : cgr. Matrix(n, n, xpt) {}; csq. Matrix(const dsq. Matrix &sqx) : cgr. Matrix(sqx. nrow(), sqx. ncol(), sqx. getarray()){}; csq. Matrix(const cgr. Matrix &); csq. Matrix(const dgr. Matrix &); // memeber functions complex<double> trace() const; };
Class cro. Vector class cro. Vector : public cgr. Matrix { public: cro. Vector(): cgr. Matrix(){}; cro. Vector(int n): cgr. Matrix(1, n){}; cro. Vector(int n, double x) : cgr. Matrix(1, n, x){}; cro. Vector(int n, complex<double> x) : cgr. Matrix(1, n, x){}; cro. Vector(int n, double *xpt) : cgr. Matrix(1, n, xpt){}; cro. Vector(int n, complex<double> *xpt) : cgr. Matrix(1, n, xpt){}; cro. Vector(const cgr. Matrix &); cro. Vector(const dgr. Matrix &); };
Class cco. Vector class cco. Vector : public cgr. Matrix { public: cco. Vector(): cgr. Matrix(){}; cco. Vector(int n): cgr. Matrix(n, 1){}; cco. Vector(int n, double x) : cgr. Matrix(n, 1, x){}; cco. Vector(int n, complex<double> x) : cgr. Matrix(n, 1, x){}; cco. Vector(int n, double *xpt) : cgr. Matrix(n, 1, xpt){}; cco. Vector(int n, complex<double> *xpt) : cgr. Matrix(n, 1, xpt){}; cco. Vector(const cgr. Matrix &); cco. Vector(const dgr. Matrix &); };
Write your classes for the test code to run… // testing headfile cspmatrix. h & dspmatrix. h #include "cspmatrix. h" #include "dspmatrix. h" main() { int i, j; double aary[16]; complex<double> cary[16]; for (i=0; i<16; i++) aary[i] = i - 5. 768; for (i=0; i<16; i++) cary[i] = complex<double> (i-8. 123, 7. 4 - i); dsq. Matrix dmtx(4, aary); csq. Matrix cmtx(4, cary); ………
Final works -- using matrix classes using iterative method solving 10 linear equations in ttdsp 2. txt A*X = B (A 0 + A 1) * X = B separate A into large (diagonal part) A 0 and smaller A 1. A 0 * X = B - A 1 * X X = (1/A 0 * B) - (1/A 0 * A 1) *X where 1/A 0 is the inverse of A 0. Let vector V = (1/A 0) * B, and matrix M = (1/A 0) * A 1 Using iterative method, starting with an arbitray X 1 X 2 = V - M * X 1, then replace X 1 = X 2. Do these processes iteratively until | X 2 - X 1 | < tolerance. (Practically you may mix X 1 and X 2 for better convergence. )
Data file ttdsp 2. txt for Eq. A x = b 10 10 8. 97 -1. 88 0. 73 0. 29 1. 02 -1. 94 0. 73 1. 68 0. 72 0. 39 -44. 37 -1. 06 4. 00 1. 01 0. 45 1. 77 1. 72 1. 95 -0. 98 1. 20 0. 86 -7. 81 0. 84 0. 08 1. 97 0. 41 0. 34 0. 16 0. 57 1. 27 1. 15 0. 73 -1. 84 1. 37 0. 07 -1. 94 0. 58 -0. 75 -7. 49 -1. 80 0. 48 0. 08 -0. 94 0. 66 1. 71 0. 26 1. 80 6. 69 0. 95 0. 30 0. 11 0. 48 -0. 26 0. 32 -0. 61 0. 65 3. 91 -1. 67 1. 38 -0. 41 0. 15 -0. 32 1. 11 1. 83 -1. 66 3. 71 1. 18 0. 33 0. 92 0. 51 0. 09 -0. 24 1. 12 0. 82 -9. 55 -1. 02 -1. 74 0. 05 1. 97 1. 77 0. 84 1. 25 -1. 17 4. 00 -1. 42 -1. 94 0. 79 0. 20 1. 00 1. 15 1. 32 1. 12 3. 38 1. 08 -0. 23 0. 15 0. 38 0. 52 0. 94 0. 45 1. 66 4. 24 -43. 75 5. 76 -35. 72 13. 94 11. 68 32. 30 -22. 18 -11. 76 Line 1 : matrix dimension, number of rows and columns. Line 2 -10: matrix elements of A (10 x 10) Line 11 : vector B (1 x 10)
Mission: Make your classes definition work with my main program 1. read dimension n , m from file ttdsp 2. txt 2. build a square matrix amtx, and column vector bvct objects with dimension n (m=n). 3. provide member function in base class dgr. Matrix to read a matrix from the file. 4. provide member function in dsq. Matrix to extract the diaginal element as an (n x n) square Matrix -- A 0 5. substract A 0 from amtx to get A 1: A 1 = amtx - A 0 6. inverse the diagonal elements of A 0 ==> 1 / A 0 7. Multiply A 1 with 1/A 0: A 2 = 1/A 0 * A 1. 8. Multiply bvct with 1/AO : dvct = 1/A 0 * bvct. 9. Start with XX 2 = column vector of all element = 1, and a empty XX 1 (provide memeber function of this effect. ) 10. XX 1 = XX 2. 11. XX 2 = dvct - A 2 * XX 1. 12. repeat step 10 --12 until | XX 2 - XX 1 | < tolerance (1. 0 E-6) 13. Check the answer, compare bvct with amtx*XX 2.
This concludes our course. Thank you for your attention.
- Slides: 27