Derived ClassDerived Class
Derived class and Base classDerived class and Base class
class base_class1 { …..};
class base_calss2 { …..};
class derived_class : public base_class1, public base_class2 { double x, y, z;
// constructors derived_class (double r1, double r1, double r3) : base_class1(r1, r2), base_class2(r3);
} ;
Properties of a derived classProperties of a derived class
1. Derived class 擁有 base class 的所有 members 和 member functions .2. 在建構 derived class 的 constructor 時 , 也必須賦予 base class 的建
構規則 .3. Derived class 可以經由 base class 的運算符作 + - * = 等運算 .
例如 : dsqMatrix aMtx(n, xarray); dcoVector cVct(n, yarray); droVector rVct(n , yaary);
cout << (aMtx*cVct) ; // print (1 x n) matrix; cout << (rVct*Amtx); // print (n x 1) matrix
// 運算 aMtx * cVct 是藉由 Matrix(n, n) * Matrix(n, 1) 的規則進行 .// 運算 rVct * aMtx 是藉由 Matrix(1, n) * Matrix(n, n) 的規則進行 .// cout 也是經由 Matrix 的 overload 進行 .
Class MatrixClass Matrix
General (n x m) matrix
Column Vectors
Row vectors
Square (nxn) Matrix
Hermitian Matrix
Define maritrx +,-,*Operations & transport
Class Class gnrMatrixgnrMatrix
template <class XTP> class gnrMatrix{ protected: int n_row, n_col; XTP *a_mtx; public: // constructors …..}
Better structureBetter structure
dgrMatrix :
(n x m) real matrix
And operators
cgrMatrix:
(n x m) complex matrix
And operators
Operators between cgrMatrix and dgrMatrix
cspmatrix.h
csqMatrix : (n x n) complex matrix
croVector : (1 x n) complex vector
ccoVector : (n x 1) complex vector
dspmatrix.h dsqMatrix : (n x n) real matrix droVector : (1 x n) real vector dcoVector : (n x 1) real vector
base class
derived class
Class Class dgrMatrixdgrMatrix
class dgrMatrix{ protected:
int n_row; int n_col; double* a_mtx;
public: ….;}
Test program
constructorsconstructors
dgrMatrix() {};
dgrMatrix(const int, const int);
dgrMatrix(const int, const int, double*);
dgrMatrix(const int, const int, const double); dgrMatrix(const dgrMatrix &); // copy constructor
~dgrMatrix() {delete [] a_mtx;};
Member functionsMember 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;dgrMatrix transport () const;void print (int) const;double norm () const;double abs () const;dgrMatrix unit () const;
dgrMatrix getrow (const int) const;dgrMatrix getcol (const int) const;void putrow (const int, double*);void putcol (const int, double*);double* getarray () const;
i/o stream overloadi/o stream overloadostream &operator<<(ostream &ous, dgrMatrix 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, dgrMatrix &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;}
operatorsoperators friend class cgrMatrix;
dgrMatrix operator- (const dgrMatrix &);
dgrMatrix operator+ (const dgrMatrix &, const dgrMatrix &);
dgrMatrix operator- (const dgrMatrix &, const dgrMatrix &);
dgrMatrix operator* (const double, const dgrMatrix &);
dgrMatrix operator* (const dgrMatrix &s, const double x); dgrMatrix operator* (const dgrMatrix &, const dgrMatrix &);
Complex MatrixComplex Matrix
class cgrMatrix{ protected: int n_row; int n_col; complex<double> *a_mtx;
public: ……;}
Test code
constructorsconstructors
cgrMatrix() {};
cgrMatrix(const int, const int);
cgrMatrix(const int, const int, complex<double> *);
cgrMatrix(const int, const int, double *);
cgrMatrix(const int, const int, const complex<double> );
cgrMatrix(const int, const int, const double );
cgrMatrix(const cgrMatrix &mx);
cgrMatrix(const dgrMatrix &mx);
operatorsoperators cgrMatrix & operator=(const cgrMatrix &); cgrMatrix & operator=(const dgrMatrix & mx); cgrMatrix operator- (const cgrMatrix &); cgrMatrix operator+ (const cgrMatrix &, const cgrMatrix &); cgrMatrix operator- (const cgrMatrix &, const cgrMatrix &); cgrMatrix operator* (const complex<double> & , const cgrMatrix &); cgrMatrix operator* (const cgrMatrix &, const cgrMatrix &); cgrMatrix operator+ (const dgrMatrix &x1, const cgrMatrix &x2); cgrMatrix operator+ (const cgrMatrix &x1, const dgrMatrix &x2); cgrMatrix operator- (const dgrMatrix &x1, const cgrMatrix &x2); cgrMatrix operator- (const cgrMatrix &x1, const dgrMatrix &x2); cgrMatrix operator* (const double &r, const cgrMatrix &mx); cgrMatrix operator* (const cgrMatrix &mx, const double &r); cgrMatrix operator* (const cgrMatrix &mx, const complex<double> &r); cgrMatrix operator* (const dgrMatrix &m1, const cgrMatrix &m2); cgrMatrix operator* (const cgrMatrix &m1, const dgrMatrix &m2);
dspmatrix.h: real special matricesdspmatrix.h: real special matrices
1. Class dsqMatrix: real square matrix (n x n)
2. Class droVector: real row vector (1 x n)
3. Class dcoVector: real column vector (n x 1).
Class dsqMatrixClass dsqMatrixclass dsqMatrix : public dgrMatrix{ public: dsqMatrix() : dgrMatrix() {}; dsqMatrix(const int n) : dgrMatrix(n, n){}; dsqMatrix(const int n, const double x) : dgrMatrix(n, n, x){}; dsqMatrix(const int n, double *xpt) : dgrMatrix(n, n, xpt) {}; dsqMatrix(const dsqMatrix &sqx) : dgrMatrix(sqx.nrow(), sqx.ncol(), sqx.getarray()){}; dsqMatrix(const dgrMatrix &grx);
// memeber functions double trace() const; // 對角線元素的和 dsqmatrix diagonal() const; // 抽取對角線元素 ..};
Class dcoVectorClass dcoVector
class droVector : public dgrMatrix{ public: droVector():dgrMatrix(){}; droVector(int n):dgrMatrix(1, n){}; droVector(int n, double x) : dgrMatrix(1, n, x){}; droVector(int n, double *xpt) : dgrMatrix(1, n, xpt){}; droVector(const dgrMatrix &mtx);};
Class dcoVectorClass dcoVector
class dcoVector : public dgrMatrix{ public: dcoVector():dgrMatrix(){}; dcoVector(int n):dgrMatrix(n, 1){}; dcoVector(int n, double x) : dgrMatrix(n, 1, x){}; dcoVector(int n, double *xpt) : dgrMatrix(n, 1, xpt){}; dcoVector(const dgrMatrix &mtx);};
cspmatrix: complex special matrix cspmatrix: complex special matrix
1. Class csqMatrix: complex square matrix (n x n)
2. Class croVector: complex row vector (1 x n)
3. Class ccoVector: complex column vector (n x 1).
Class csqMatrixClass csqMatrixclass csqMatrix : public cgrMatrix{ public: csqMatrix() : cgrMatrix() {}; csqMatrix(const int n) : cgrMatrix(n, n){}; csqMatrix(const int n, const double x) : cgrMatrix(n, n, x){}; csqMatrix(const int n, const complex<double> x) : cgrMatrix(n, n, x){}; csqMatrix(const int n, double *xpt) : cgrMatrix(n, n, xpt) {}; csqMatrix(const int n, complex<double> *xpt) : cgrMatrix(n, n, xpt) {}; csqMatrix(const dsqMatrix &sqx) : cgrMatrix(sqx.nrow(), sqx.ncol(), sqx.getarray()){}; csqMatrix(const csqMatrix &sqx) : cgrMatrix(sqx.nrow(), sqx.ncol(), sqx.getarray()){}; csqMatrix(const cgrMatrix &); csqMatrix(const dgrMatrix &);// memeber functions complex<double> trace() const; };
Class croVectorClass croVector
class croVector : public cgrMatrix{ public: croVector():cgrMatrix(){}; croVector(int n):cgrMatrix(1, n){}; croVector(int n, double x) : cgrMatrix(1, n, x){}; croVector(int n, complex<double> x) : cgrMatrix(1, n, x){}; croVector(int n, double *xpt) : cgrMatrix(1, n, xpt){}; croVector(int n, complex<double> *xpt) : cgrMatrix(1, n, xpt){}; croVector(const cgrMatrix &); croVector(const dgrMatrix &);};
Class ccoVectorClass ccoVector
class ccoVector : public cgrMatrix{ public: ccoVector():cgrMatrix(){}; ccoVector(int n):cgrMatrix(n, 1){}; ccoVector(int n, double x) : cgrMatrix(n, 1, x){}; ccoVector(int n, complex<double> x) : cgrMatrix(n, 1, x){}; ccoVector(int n, double *xpt) : cgrMatrix(n, 1, xpt){}; ccoVector(int n, complex<double> *xpt) : cgrMatrix(n, 1, xpt){}; ccoVector(const cgrMatrix &); ccoVector(const dgrMatrix &);};
Write your classes for the Write your classes for the test code test code to run…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); dsqMatrix dmtx(4, aary); csqMatrix cmtx(4, cary); ………
Final works -- using matrix classesFinal works -- using matrix classes
using iterative method solving 10 linear equations in ttdsp2.txt A * X = B (A0 + A1) * X = B separate A into large (diagonal part) A0 and smaller A1. A0 * X = B - A1 * X X = (1/A0 * B) - (1/A0 * A1) *X where 1/A0 is the inverse of A0. Let vector V = (1/A0) * B, and matrix M = (1/A0) * A1
Using iterative method, starting with an arbitray X1 X2 = V - M * X1, then replace X1 = X2. Do these processes iteratively until | X2 - X1 | < tolerance.(Practically you may mix X1 and X2 for better convergence.)
Data file Data file ttdsp2.txtttdsp2.txt for Eq. A x = b for Eq. A x = b
10 10 8.97 -1.06 0.84 0.08 1.97 0.41 0.34 0.16 0.57 1.27 -1.88 4.00 1.15 0.73 -1.84 1.37 0.07 -1.94 0.58 -0.75 0.73 1.01 -7.49 -1.80 0.48 0.08 -0.94 0.66 1.71 0.26 0.29 0.45 1.80 6.69 0.95 0.30 0.11 0.48 -0.26 0.32 1.02 1.77 -0.61 0.65 3.91 -1.67 1.38 -0.41 0.15 -0.32 -1.94 1.72 1.11 1.83 -1.66 3.71 1.18 0.33 0.92 0.51 0.73 1.95 0.09 -0.24 1.12 0.82 -9.55 -1.02 -1.74 0.05 1.68 -0.98 1.97 1.77 0.84 1.25 -1.17 4.00 -1.42 -1.94 0.72 1.20 0.79 0.20 1.00 1.15 1.32 1.12 3.38 1.08 0.39 0.86 -0.23 0.15 0.38 0.52 0.94 0.45 1.66 4.24 -44.37 -7.81 -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)
1. read dimension n , m from file ttdsp2.txt 2. build a square matrix amtx, and column vector bvct objects with dimension n (m=n). 3. provide member function in base class dgrMatrix to read a matrix from the file. 4. provide member function in dsqMatrix to extract the diaginal element as an (n x n) square Matrix -- A0 5. substract A0 from amtx to get A1: A1 = amtx - A0 6. inverse the diagonal elements of A0 ==> 1 / A0 7. Multiply A1 with 1/A0: A2 = 1/A0 * A1. 8. Multiply bvct with 1/AO : dvct = 1/A0 * bvct. 9. Start with XX2 = column vector of all element = 1, and a empty XX1 (provide memeber function of this effect.) 10. XX1 = XX2. 11. XX2 = dvct - A2 * XX1. 12. repeat step 10--12 until | XX2 - XX1 | < tolerance (1.0E-6) 13. Check the answer, compare bvct with amtx*XX2.
Mission: Make your classes definition work with my main program
This concludes our course.
Thank you for your attention.