// =======================< LEFT.HPP >======================== // * Class MatrixLeft * // * Description: Chapter 12 * // * Scientific C++ Building Numerical Libraries * // * the Object-Oriented Way * // * by G. Buzzi-Ferraris * // * Addison-Wesley (1993) * // =========================================================== // ****** Constructors for MatrixLeft: * // * MatrixLeft L; // default * // * MatrixLeft L = left; // copy-initializer * // * MatrixLeft L(3,3); // sizes and places at 0 * // * MatrixLeft L(3,3, * // * 1., * // * 2.,3., * // * 4.,5.,6.);// sizes and initialises * // * float x[]= * // * { * // * 1., * // * 2.,3., * // * 4.,5.,6. * // * }; * // * MatrixLeft L(3,3,x); // from array * // * MatrixLeft L("LEFT.DAT"); // Formatted File * // * MatrixLeft L('*',LEFT.BIN"); // Binary File * // *********************************************************** // ***** Access functions : * // * i = L.Rows(); // numRows * // * i = L.Columns(); // numColumns * // * xf = L.GetValue(i); * // * xf = L(i,j); * // * L(i,j) = xf; * // * xf = L[i][j]; * // * L[i][j] = xf; * // * L.SetValue(i,j,xf); * // *********************************************************** // ***** Assignments: * // * L = left; // left MatrixLeft * // *********************************************************** // ***** Operators for tests: * // * if(L1 == L2) * // * if(L1 != L2) * // *********************************************************** // ***** Implemented operations : * // * Sum(L1,L2,&L3); // L3 = L1 + L2; * // * L3 = L1 + L2; // L3 = L1 + L2; * // * Sum(L,R,&A); // A = L + R; * // * Sum(R,L,&A); // A = R + L; * // * Sum(L1,L2,&L1); // L1 = L1 + L2; * // * Sum(&L1,L2); // L1 = L1 + L2; * // * L1 += L2; // L1 = L1 + L2; * // * Sum(L1,L2,&L2); // L2 = L1 + L2; * // * Sum(L1,&L2); // L2 = L1 + L2; * // * Sum(L1,L1,&L1); // L1 = L1 + L1; * // * Sum(&L1); // L1 = L1 + L1; * // * Difference(L1,L2,&L3); // L3 = L1 - L2; * // * L3 = L1 - L2; // L3 = L1 - L2; * // * Difference(L1,L2,&L1); // L1 = L1 - L2; * // * Difference(&L1,L2); // L1 = L1 - L2; * // * L1 -= L2; // L1 = L1 - L2; * // * Difference(L1,L2,&L2); // L2 = L1 - L2; * // * Difference(L1,&L2); // L2 = L1 - L2; * // * Difference(L1,L1,&L1); // L1 = L1 - L1; * // * Difference(&L1); // L1 = L1 - L1; * // * Minus(L1,&L2); // L2 = -L1; * // * L2 = -L1; // L2 = -L1; * // * Minus(&L1); // L1 = -L1; * // * Product(L1,L2,&L3); // L3 = L1*L2; * // * L3 = L1*L2; * // * Product(L1,L2,&L1); // L1 = L1*L2; * // * Product(&L1,L2); // L1 = L1*L2; * // * L1 *= L2; // L1 = L1*L2; * // * Product(L1,L1,&L1); // L1 = L1*L1; * // * Product(&L1); // L1 = L1*L1; * // * Product(L1,x,&y); // y = L1*x; * // * y = L1*x; * // * Product(3.,L1,&L2); // L2 = 3.*L1; * // * L2 = 3.*L1; * // * Product(3.,&L1); // L1 = 3.*L1; * // * L1 *= 3.; // L1 = 3.*L1; * // *********************************************************** // ***** Functions for linear algebra: * // * Solve(L,b,&x); * // * Solve(L,&bx); else Solve(L,b,&b); * // * Solve(L,B,&X); * // * Solve(l,&BX); else Solve(L,B,&B); * // * TranposeSolve(L,b,&x); * // * TransposeSolve(L,&bx); else TransposeSolve(L,b,&b); * // * L.Determinant(); * // * L.ConditionNumber(); * // *********************************************************** #ifndef LEFT_HPP #define LEFT_HPP // preventive declarations class Vector; class Matrix; class MatrixLeft; class MatrixRight; class MatrixSymm; // =========================================================== // ================= class MatrixLeft ==================== // =========================================================== class MatrixLeft { friend class Vector; friend class Matrix; friend class MatrixRight; friend class MatrixSymm; private: static const char *const ERROR; static int count; // for whoAmI float **matrix; int numRows,numColumns; int size; int whoAmI; // initialisation constructors void Initialize(int rows,int columns); // deinitialisation void Deinitialize(void); // MatrixLeft A('*',3,3); MatrixLeft(char,int rows,int columns); public: // =========================================================== // ****************** constructors *********************** // =========================================================== // default // MatrixLeft L MatrixLeft(void); // copy-initializer // MatrixLeft L = left; MatrixLeft(MatrixLeft &rval); // sizes and initialises at 0 // MatrixLeft L(3,3); MatrixLeft(int rows,int columns); // sizes and initialises // MatrixLeft L(2,2,1.,2.,3.); MatrixLeft(int rows,int columnsn,double a11,...); // from array // MatrixLeft L(3,3,w) MatrixLeft(int rows,int columns,float *initvalues); // from formatted File // MatrixLeft L("LEFT.DAT"); MatrixLeft(char *filematrix); // from binary File // MatrixLeft L('*',"LEFT.BIN"); MatrixLeft(char,char *filematrix); // =========================================================== // ******************** destructor *********************** // =========================================================== ~MatrixLeft(void); // =========================================================== // ********** Non-modifying access functions ************* // =========================================================== // row number int Rows(void) const {return numRows;} // column number int Columns(void) const {return numColumns;} int WhoAmI(void) const {return whoAmI;} // receives the values with control float GetValue(int row,int col) const; // =========================================================== // ************* Modifying access functions ************** // =========================================================== // assigns values with control void SetValue(int row,int col,float val); // assigns and receives vector values with control float &operator () (int row,int col); // assigns and receives vector values without control float *operator [](int i) {return matrix[i];} // =========================================================== // **************** assignment operators ******************** // =========================================================== MatrixLeft &operator = (const MatrixLeft &rval); // =========================================================== // **************** operators for tests ****************** // =========================================================== friend char operator == (const MatrixLeft &lval,const MatrixLeft &rval); friend char operator != (const MatrixLeft &lval,const MatrixLeft &rval); // =========================================================== // ==================== OPERATIONS ======================= // =========================================================== // =========================================================== // *********************** Sum *************************** // =========================================================== // Sum(A,B,&C); C = A + B; friend void Sum (const MatrixLeft &lval,const MatrixLeft &rval, MatrixLeft *result); // C = A + B; friend MatrixLeft operator + (const MatrixLeft &lval,const MatrixLeft &rval); // Sum(L,R,&C); C = L + R; friend void Sum (MatrixLeft &lval,MatrixRight &rval,Matrix *result); // Sum(R,L,&C); C = R + L; friend void Sum (MatrixRight &lval,MatrixLeft &rval,Matrix *result); // Sum(&A,B); A = A + B; friend void Sum (MatrixLeft *lvalAndResult,const MatrixLeft &rval); // A += B; A = A + B; MatrixLeft &operator += (const MatrixLeft &rval); // Sum(B,&A); A = B + A; friend void Sum (const MatrixLeft &lval,MatrixLeft *rvalAndResult); // Sum(&A); A = A + A; friend void Sum (MatrixLeft *lvalRvalAndResult); // =========================================================== // ******************** Difference *********************** // =========================================================== // Difference(A,B,&C); C = A - B; friend void Difference (const MatrixLeft &lval,const MatrixLeft &rval, MatrixLeft *result); // C = A - B; friend MatrixLeft operator - (const MatrixLeft &lval,const MatrixLeft &rval); // Difference(&A,B); A = A - B; friend void Difference (MatrixLeft *lvalAndResult,const MatrixLeft &rval); // A -= B; A = A - B; MatrixLeft &operator -= (const MatrixLeft &rval); // Difference(B,&A); A = B - A; friend void Difference (const MatrixLeft &lval,MatrixLeft *rvalAndResult); // =========================================================== // *********************** Minus ************************* // =========================================================== friend void Minus (const MatrixLeft &rval,MatrixLeft *result); friend MatrixLeft operator - // unary (const MatrixLeft &rval); friend void Minus (MatrixLeft *rvalAndResult); // =========================================================== // ********************** Product ************************ // =========================================================== friend void Product // C=A*B; (const MatrixLeft &lval,const MatrixLeft &rval, MatrixLeft *result); friend MatrixLeft operator * (const MatrixLeft &lval,const MatrixLeft &rval); friend void Product // A = A * B; (Matrix *lvalAndResult,const Matrix &rval); // A *= B; A = A*B MatrixLeft &operator *= (const MatrixLeft &rval); friend void Product // B = A * B; (const MatrixLeft &lval,MatrixLeft *rvalAndResult); friend void Product // A = A * A; (MatrixLeft *lvalRvalAndResult); friend void Product // y =A*x; (const MatrixLeft &lval,const Vector &rval, Vector *result); friend Vector operator * // y = A*x; (const MatrixLeft &lval,const Vector &rval); friend void Product // A = 3.*B; (float lval,const MatrixLeft &rval, MatrixLeft *result); friend MatrixLeft operator * // A = 3.*B; (float lval,const MatrixLeft &rval); friend void Product // A = 3.* A; (float lval,MatrixLeft *rvalAndResult); MatrixLeft &operator *= // A = 3.*A; (float rval); // =========================================================== // ********************* TProduct ************************ // =========================================================== friend void TProduct(const MatrixLeft &lvalAndRval, MatrixSymm *result); // =========================================================== // ********************* ProductT ************************ // =========================================================== friend void ProductT(const MatrixLeft &lvalAndRval, MatrixSymm *result); // =========================================================== // ********************** Division *********************** // =========================================================== friend void Division // A =B/3.; (const MatrixLeft &lval,float rval, MatrixLeft *result); // A = B/3.; friend MatrixLeft operator / (const MatrixLeft &lval,float rval); friend void Division // A /= 3.; (MatrixLeft *lvalAndResult,float rval); // A /= 3.; MatrixLeft &operator /= (float rval); // =========================================================== // =========== Non-modifying functions =================== // =========================================================== // ***********************< Print >*************************** void Print(char *msg=""); // ************************< Save >*************************** void Save(char *filematrix); // formatted void Save(char,char *filematrix);// binary // ***********************< Norms >*************************** float NormT(void); float NormR(void); float NormC(void); float NormF(void); float NormI(void){return NormR();} float Norm1(void){return NormC();} // =========================================================== // ============= Modifying Functions ======================= // =========================================================== friend void Delete(MatrixLeft *L); // eliminates MatrixLeft friend void ChangeDimensions(int rows,int columns, MatrixLeft *result,char zero = 0); // from formatted file friend void Recover(MatrixLeft *A,char *filematrix); // from binary file friend void Recover(MatrixLeft *A,char,char *filematrix); friend MatrixLeft Transpose(const MatrixRight &R); friend MatrixRight Transpose(const MatrixLeft &L); friend char Inverse(MatrixLeft *L); friend void Swap(MatrixLeft *lval,MatrixLeft *rval); // =========================================================== // ========== Functions for linear algebra ================ // =========================================================== friend void Solve(const MatrixLeft &R,Vector *bx); friend void Solve(const MatrixLeft &R, const Vector &b,Vector *x); friend void Solve(const MatrixLeft &R,Matrix *BX); friend void Solve(const MatrixLeft &R, const Matrix &B,Matrix *X); friend void TransposeSolve(const MatrixLeft &R, Vector *bx); friend void TransposeSolve(const MatrixLeft &R, const Vector &b,Vector *x); double Determinant(void); float ConditionNumber(); }; #endif LEFT_HPP