// matrix.h #ifndef matrix_H #define matrix_H #undef trf_info #define trf_error #include "smatrix.h" template class matrix : public array { private: int _nrow; int _ncol; int ij( int i, int j ) const { return _ncol*i+j; }; void ostr(std::ostream& stream) const; public: matrix(int M,int N) : array(M*N) { _nrow = M; _ncol = N; }; virtual ~matrix(void) { }; matrix(const matrix& mat); matrix(const smatrix& sma); matrix &operator=(const matrix& mat); int nrow() const { return _nrow; }; int ncol() const { return _ncol; }; T& operator()( int i, int j ) const { return data[ij(i,j)]; }; matrix transpose( ); matrix& operator+=(const matrix& mat); matrix& operator-=(const matrix& mat); matrix& operator*=(const T& tval); #if defined(DEFECT_GUIDING_DECLS) friend matrix operator+(const matrix& mat1,const matrix& mat2); friend matrix operator-(const matrix& mat1,const matrix& mat2); friend smatrix operator%(const matrix& mat,const smatrix& sma); #elif defined(DEFECT_NO_EXPLICIT_QUALIFICATION) template friend matrix operator+(const matrix& mat1,const matrix& mat2); template friend matrix operator-(const matrix& mat1,const matrix& mat2); template friend smatrix operator%(const matrix& mat,const smatrix& sma); #else friend matrix operator+ <> (const matrix& mat1,const matrix& mat2); friend matrix operator- <> (const matrix& mat1,const matrix& mat2); friend smatrix operator%<> (const matrix& mat, const smatrix& sma); #endif }; // copier template inline matrix::matrix(const matrix& mat) : array(mat) { #ifdef trf_info std::cout << "copying a matrix\n"; #endif _nrow = mat._nrow; _ncol = mat._ncol; } // assignment template inline matrix &matrix::operator=(const matrix& mat) { #ifdef trf_info std::cout << "assigning a matrix\n"; #endif assert( _ncol == mat._ncol ); if ( _ncol == mat._ncol ) (array&)*this = mat; else { #ifdef trf_error std::cerr << "matrix::operator=: # columns does not match\n"; } #endif return *this; } // transpose template inline matrix matrix::transpose( ) { matrix mat( _ncol, _nrow ); for ( int irow=0; irow inline matrix &matrix::operator+=(const matrix& mat) { assert( _ncol == mat._ncol ); if ( _ncol == mat._ncol ) (array&)*this += mat; else { #ifdef trf_error std::cerr << "matrix::operator+=: # columns does not match\n"; #endif } return *this; } // -= template inline matrix &matrix::operator-=(const matrix& mat) { assert( _ncol == mat._ncol ); if ( _ncol == mat._ncol ) (array&)*this -= mat; else { #ifdef trf_error std::cerr << "matrix::operator-=: # columns does not match\n"; #endif } return *this; } // *= template inline matrix &matrix::operator*=(const T& t) { (array&)*this *= t; return *this; } template inline matrix operator*(const T& val,const matrix& mat) { matrix lhs = mat; lhs *= val; return lhs; } template inline matrix operator*(const matrix& mat,const T& val) { matrix lhs = mat; lhs *= val; return lhs; } // addition: matrix+matrix template inline matrix operator+(const matrix& mat1,const matrix& mat2) { matrix sum = mat1; sum += mat2; return sum; } // subtraction: matrix+matrix template inline matrix operator-(const matrix& mat1,const matrix& mat2) { matrix sum = mat1; sum -= mat2; return sum; } template matrix operator*(const matrix& mat1,const matrix& mat2); template nvector operator*(const matrix& mat1,const nvector& vec2); // // mutiplication: matrix = smatrix*smatrix // template matrix operator*(const smatrix& sma1,const smatrix& sma2); // // mutiplication: smatrix*matrix // template inline matrix operator*(const smatrix& sma1,const matrix& mat2) { return matrix(sma1)*mat2; } // // mutiplication: matrix*smatrix // template inline matrix operator*(const matrix& mat1,const smatrix& sma2) { return mat1*matrix(sma2); } // multiplication: M % S == M * S * MT // template smatrix operator%(const matrix& mat, const smatrix& sma); #ifndef __xlC__ #include "matrix.c" #endif #endif