// ======================< RIGHT.HPP >======================== // * Class MatrixRight * // * Description: Chapter 12 * // * Scientific C++ Building Numerical Libraries * // * the Object-Oriented Way * // * by G. Buzzi-Ferraris * // * Addison-Wesley (1993) * // =========================================================== // ****** Constructions for MatrixRight: * // * MatrixRight R; // default * // * MatrixRight R = right; // copy-initializer * // * MatrixRight L(3,3); // sizes and places at 0 * // * MatrixRight R(3,3, * // * 1.,2.,3., * // * 4.,5., * // * 6.);// sizes and initialises * // * float x[]= * // * { * // * 1.,2.,3., * // * 4.,5., * // * 6. * // * }; * // * MatrixRight R(3,3,x); // from array * // * MatrixRight R("MAT.DAT"); // Formatted File * // * MatrixRight R('*',MAT.BIN"); // Binary File * // *********************************************************** // ***** Access functions : * // * i = R.Rows(); // numRows * // * i = R.Columns(); // numColumns * // * xf = R.GetValue(i); * // * xf = R(i,j); * // * R(i,j) = xf; * // * xf = R[i][j]; * // * R[i][j] = xf; * // * R.SetValue(i,j,xf); * // *********************************************************** // ***** Assignments: * // * R = right; // right MatrixRight * // *********************************************************** // ***** Operators for tests: * // * if(R1 == R2) * // * if(R1 != R2) * // *********************************************************** // ***** Implemented operations : * // * Sum(R1,R2,&R3); // R3 = R1 + R2; * // * R3 = R1 + R2; // R3 = R1 + R2; * // * Sum(L,R,&A); // A = L + R; * // * Sum(R,L,&A); // A = R + L; * // * Sum(R1,R2,&R1); // R1 = R1 + R2; * // * Sum(&R1,R2); // R1 = R1 + R2; * // * R1 += R2; // R1 = R1 + R2; * // * Sum(R1,R2,&R2); // R2 = R1 + R2; * // * Sum(R1,&R2); // R2 = R1 + R2; * // * Sum(R1,R1,&R1); // R1 = R1 + R1; * // * Sum(&R1); // R1 = R1 + R1; * // * Difference(R1,R2,&R3); // R3 = R1 - R2; * // * R3 = R1 - R2; // R3 = R1 - R2; * // * Difference(R1,R2,&R1); // R1 = R1 - R2; * // * Difference(&R1,R2); // R1 = R1 - R2; * // * R1 -= R2; // R1 = R1 - R2; * // * Difference(R1,R2,&R2); // R2 = R1 - R2; * // * Difference(R1,&R2); // R2 = R1 - R2; * // * Difference(R1,R1,&R1); // R1 = R1 - R1; * // * Difference(&R1); // R1 = R1 - R1; * // * Minus(R1,&R2); // R2 = -R1; * // * R2 = -R1; // R2 = -R1; * // * Minus(&R1); // R1 = -R1; * // * Product(R1,R2,&R3); // R3 = R1*R2; * // * R3 = R1*R2; * // * Product(R1,R2,&R1); // R1 = R1*R2; * // * Product(&R1,R2); // R1 = R1*R2; * // * R1 *= R2; // R1 = R1*R2; * // * Product(R1,R1,&R1); // R1 = R1*R1; * // * Product(&R1); // R1 = R1*R1; * // * Product(R1,x,&y); // y = R1*x; * // * y = R1*x; * // * Product(3.,R1,&R2); // R2 = 3.*R1; * // * R2 = 3.*R1; * // * Product(3.,&R1); // R1 = 3.*R1; * // * R1 *= 3.; // R1 = 3.*R1; * // *********************************************************** // ***** Functions for linear algebra: * // * Solve(R,b,&x); * // * Solve(R,&bx); else Solve(R,b,&b); * // * Solve(R,B,&X); * // * Solve(R,&BX); else Solve(R,B,&B); * // * TranposeSolve(R,b,&x); * // * TransposeSolve(R,&bx); else TransposeSolve(R,b,&b); * // * R.Determinant(); * // * R.ConditionNumber(); * // *********************************************************** #ifndef RIGHT_HPP #define RIGHT_HPP // preventive declarations class Vector; class Matrix; class MatrixLeft; class MatrixRight; class MatrixSymm; // =========================================================== // ================= class MatrixRight =================== // =========================================================== class MatrixRight { friend class Vector; friend class Matrix; friend class MatrixLeft; 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); // MatrixRight A('*',3,3); MatrixRight(char,int rows,int columns); public: // =========================================================== // ****************** constructors *********************** // =========================================================== // default // MatrixRight R MatrixRight(void); // copy-initializer // MatrixRight R = right; MatrixRight(MatrixRight &rval); // sizes and initialises to 0 // MatrixRight R(3,3); MatrixRight(int rows,int columns); // sizes and initialises // MatrixRight R(2,2,1.,2.,3.); MatrixRight(int rows,int columns,double a11,...); // from array // MatrixRight R(3,3,w) MatrixRight(int rows,int columns,float *initvalues); // from formatted File // MatrixRight R("RIGHT.DAT"); MatrixRight(char *filematrix); // from binary File // MatrixRight R('*',"RIGHT.BIN"); MatrixRight(char,char *filematrix); // =========================================================== // ******************** destructor *********************** // =========================================================== ~MatrixRight(void); // =========================================================== // ************ Non-modifying access functions *********** // =========================================================== // number of rows int Rows(void) const {return numRows;} // number of columns int Columns(void) const {return numColumns;} int WhoAmI(void) const {return whoAmI;} // receive 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 ****************** // =========================================================== MatrixRight &operator = (const MatrixRight &rval); // =========================================================== // ***************** operators for tests ***************** // =========================================================== friend char operator == (const MatrixRight &lval,const MatrixRight &rval); friend char operator != (const MatrixRight &lval,const MatrixRight &rval); // =========================================================== // ==================== OPERATIONS ======================= // =========================================================== // =========================================================== // *********************** Sum *************************** // =========================================================== // Sum(A,B,&C); C = A + B; friend void Sum (const MatrixRight &lval,const MatrixRight &rval, MatrixRight *result); // C = A + B; friend MatrixRight operator + (const MatrixRight &lval,const MatrixRight &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 (MatrixRight *lvalAndResult,const MatrixRight &rval); // A += B; A = A + B; MatrixRight &operator += (const MatrixRight &rval); // Sum(B,&A); A = B + A; friend void Sum (const MatrixRight &lval,MatrixRight *rvalAndResult); // Sum(&A); A = A + A; friend void Sum (MatrixRight *lvalRvalAndResult); // =========================================================== // ******************** Difference *********************** // =========================================================== // Difference(A,B,&C); C = A - B; friend void Difference (const MatrixRight &lval,const MatrixRight &rval, MatrixRight *result); // C = A - B; friend MatrixRight operator - (const MatrixRight &lval,const MatrixRight &rval); // Difference(&A,B); A = A - B; friend void Difference (MatrixRight *lvalAndResult,const MatrixRight &rval); // A -= B; A = A - B; MatrixRight &operator -= (const MatrixRight &rval); // Difference(B,&A); A = B - A; friend void Difference (const MatrixRight &lval,MatrixRight *rvalAndResult); // =========================================================== // *********************** Minus ************************* // =========================================================== friend void Minus (const MatrixRight &rval,MatrixRight *result); friend MatrixRight operator - // unary (const MatrixRight &rval); friend void Minus (MatrixRight *rvalAndResult); // =========================================================== // ********************** Product ************************ // =========================================================== friend void Product // C=A*B; (const MatrixRight &lval,const MatrixRight &rval, MatrixRight *result); friend MatrixRight operator * (const MatrixRight &lval,const MatrixRight &rval); friend void Product // A = A * B; (Matrix *lvalAndResult,const Matrix &rval); // A *= B; A = A*B MatrixRight &operator *= (const MatrixRight &rval); friend void Product // B = A * B; (const MatrixRight &lval,MatrixRight *rvalAndResult); friend void Product // A = A * A; (MatrixRight *lvalRvalAndResult); friend void Product // y =A*x; (const MatrixRight &lval,const Vector &rval, Vector *result); friend Vector operator * // y = A*x; (const MatrixRight &lval,const Vector &rval); friend void Product // A = 3.*B; (float lval,const MatrixRight &rval, MatrixRight *result); friend MatrixRight operator * // A = 3.*B; (float lval,const MatrixRight &rval); friend void Product // A = 3.* A; (float lval,MatrixRight *rvalAndResult); MatrixRight &operator *= // A = 3.*A; (float rval); // =========================================================== // ********************* TProduct ************************ // =========================================================== friend void TProduct(const MatrixRight &lvalAndRval, MatrixSymm *result); // =========================================================== // ********************* ProductT ************************ // =========================================================== friend void ProductT(const MatrixRight &lvalAndRval, MatrixSymm *result); // =========================================================== // ********************** Division *********************** // =========================================================== friend void Division // A =B/3.; (const MatrixRight &lval,float rval, MatrixRight *result); // A = B/3.; friend MatrixRight operator / (const MatrixRight &lval,float rval); friend void Division // A /= 3.; (MatrixRight *lvalAndResult,float rval); // A /= 3.; MatrixRight &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(MatrixRight *R); friend void ChangeDimensions(int rows,int columns, MatrixRight *result,char zero = 0); // from formatted file friend void Recover(MatrixRight *A,char *filematrix); // from binary file friend void Recover(MatrixRight *A,char,char *filematrix); friend MatrixLeft Transpose(const MatrixRight &R); friend MatrixRight Transpose(const MatrixLeft &L); friend char Inverse(MatrixRight *R); friend void Swap(MatrixRight *lval,MatrixRight *rval); // =========================================================== // ========== Functions for linear algebra ================ // =========================================================== friend void Solve(const MatrixRight &R,Vector *bx); friend void Solve(const MatrixRight &R, const Vector &b,Vector *x); friend void Solve(const MatrixRight &R,Matrix *BX); friend void Solve(const MatrixRight &R, const Matrix &B,Matrix *X); friend void TransposeSolve(const MatrixRight &R, Vector *bx); friend void TransposeSolve(const MatrixRight &R, const Vector &b,Vector *x); double Determinant(void); float ConditionNumber(); }; #endif RIGHT_HPP