--- trunk/OOPSE-2.0/src/math/RectMatrix.hpp 2004/10/21 21:30:34 1629 +++ trunk/OOPSE-2.0/src/math/RectMatrix.hpp 2004/10/21 21:31:39 1630 @@ -46,308 +46,315 @@ namespace oopse { template class RectMatrix { public: + typedef Real ElemType; + typedef Real* ElemPoinerType; + + /** default constructor */ + RectMatrix() { + for (unsigned int i = 0; i < Row; i++) + for (unsigned int j = 0; j < Col; j++) + data_[i][j] = 0.0; + } - /** default constructor */ - RectMatrix() { - for (unsigned int i = 0; i < Row; i++) - for (unsigned int j = 0; j < Col; j++) - data_[i][j] = 0.0; - } + /** Constructs and initializes every element of this matrix to a scalar */ + RectMatrix(Real s) { + for (unsigned int i = 0; i < Row; i++) + for (unsigned int j = 0; j < Col; j++) + data_[i][j] = s; + } - /** Constructs and initializes every element of this matrix to a scalar */ - RectMatrix(Real s) { - for (unsigned int i = 0; i < Row; i++) - for (unsigned int j = 0; j < Col; j++) - data_[i][j] = s; - } + /** copy constructor */ + RectMatrix(const RectMatrix& m) { + *this = m; + } + + /** destructor*/ + ~RectMatrix() {} - /** copy constructor */ - RectMatrix(const RectMatrix& m) { - *this = m; - } - - /** destructor*/ - ~RectMatrix() {} - - /** copy assignment operator */ - RectMatrix& operator =(const RectMatrix& m) { - if (this == &m) + /** copy assignment operator */ + RectMatrix& operator =(const RectMatrix& m) { + if (this == &m) + return *this; + + for (unsigned int i = 0; i < Row; i++) + for (unsigned int j = 0; j < Col; j++) + data_[i][j] = m.data_[i][j]; return *this; + } - for (unsigned int i = 0; i < Row; i++) - for (unsigned int j = 0; j < Col; j++) - data_[i][j] = m.data_[i][j]; - return *this; - } - - /** - * Return the reference of a single element of this matrix. - * @return the reference of a single element of this matrix - * @param i row index - * @param j colum index - */ - double& operator()(unsigned int i, unsigned int j) { - //assert( i < Row && j < Col); - return data_[i][j]; - } + /** + * Return the reference of a single element of this matrix. + * @return the reference of a single element of this matrix + * @param i row index + * @param j colum index + */ + Real& operator()(unsigned int i, unsigned int j) { + //assert( i < Row && j < Col); + return data_[i][j]; + } - /** - * Return the value of a single element of this matrix. - * @return the value of a single element of this matrix - * @param i row index - * @param j colum index - */ - double operator()(unsigned int i, unsigned int j) const { - - return data_[i][j]; - } + /** + * Return the value of a single element of this matrix. + * @return the value of a single element of this matrix + * @param i row index + * @param j colum index + */ + Real operator()(unsigned int i, unsigned int j) const { + + return data_[i][j]; + } - /** - * Returns a row of this matrix as a vector. - * @return a row of this matrix as a vector - * @param row the row index - */ - Vector getRow(unsigned int row) { - Vector v; + /** Returns the pointer of internal array */ + Real* getArrayPointer() { + return &data_[0][0]; + } - for (unsigned int i = 0; i < Row; i++) - v[i] = data_[row][i]; + /** + * Returns a row of this matrix as a vector. + * @return a row of this matrix as a vector + * @param row the row index + */ + Vector getRow(unsigned int row) { + Vector v; - return v; - } + for (unsigned int i = 0; i < Row; i++) + v[i] = data_[row][i]; - /** - * Sets a row of this matrix - * @param row the row index - * @param v the vector to be set - */ - void setRow(unsigned int row, const Vector& v) { + return v; + } - for (unsigned int i = 0; i < Row; i++) - data_[row][i] = v[i]; - } + /** + * Sets a row of this matrix + * @param row the row index + * @param v the vector to be set + */ + void setRow(unsigned int row, const Vector& v) { - /** - * Returns a column of this matrix as a vector. - * @return a column of this matrix as a vector - * @param col the column index - */ - Vector getColum(unsigned int col) { - Vector v; + for (unsigned int i = 0; i < Row; i++) + data_[row][i] = v[i]; + } - for (unsigned int j = 0; j < Col; j++) - v[j] = data_[j][col]; + /** + * Returns a column of this matrix as a vector. + * @return a column of this matrix as a vector + * @param col the column index + */ + Vector getColum(unsigned int col) { + Vector v; - return v; - } + for (unsigned int j = 0; j < Col; j++) + v[j] = data_[j][col]; - /** - * Sets a column of this matrix - * @param col the column index - * @param v the vector to be set - */ - void setColum(unsigned int col, const Vector& v){ + return v; + } - for (unsigned int j = 0; j < Col; j++) - data_[j][col] = v[j]; - } + /** + * Sets a column of this matrix + * @param col the column index + * @param v the vector to be set + */ + void setColum(unsigned int col, const Vector& v){ - /** - * swap two rows of this matrix - * @param i the first row - * @param j the second row - */ - void swapRow(unsigned int i, unsigned int j){ - assert(i < Row && j < Row); + for (unsigned int j = 0; j < Col; j++) + data_[j][col] = v[j]; + } - for (unsigned int k = 0; k < Col; k++) - std::swap(data_[i][k], data_[j][k]); - } + /** + * swap two rows of this matrix + * @param i the first row + * @param j the second row + */ + void swapRow(unsigned int i, unsigned int j){ + assert(i < Row && j < Row); - /** - * swap two colums of this matrix - * @param i the first colum - * @param j the second colum - */ - void swapColum(unsigned int i, unsigned int j){ - assert(i < Col && j < Col); - - for (unsigned int k = 0; k < Row; k++) - std::swap(data_[k][i], data_[k][j]); - } + for (unsigned int k = 0; k < Col; k++) + std::swap(data_[i][k], data_[j][k]); + } - /** - * Tests if this matrix is identical to matrix m - * @return true if this matrix is equal to the matrix m, return false otherwise - * @m matrix to be compared - * - * @todo replace operator == by template function equal - */ - bool operator ==(const RectMatrix& m) { - for (unsigned int i = 0; i < Row; i++) - for (unsigned int j = 0; j < Col; j++) - if (!equal(data_[i][j], m.data_[i][j])) - return false; + /** + * swap two colums of this matrix + * @param i the first colum + * @param j the second colum + */ + void swapColum(unsigned int i, unsigned int j){ + assert(i < Col && j < Col); + + for (unsigned int k = 0; k < Row; k++) + std::swap(data_[k][i], data_[k][j]); + } - return true; - } + /** + * Tests if this matrix is identical to matrix m + * @return true if this matrix is equal to the matrix m, return false otherwise + * @m matrix to be compared + * + * @todo replace operator == by template function equal + */ + bool operator ==(const RectMatrix& m) { + for (unsigned int i = 0; i < Row; i++) + for (unsigned int j = 0; j < Col; j++) + if (!equal(data_[i][j], m.data_[i][j])) + return false; - /** - * Tests if this matrix is not equal to matrix m - * @return true if this matrix is not equal to the matrix m, return false otherwise - * @m matrix to be compared - */ - bool operator !=(const RectMatrix& m) { - return !(*this == m); - } + return true; + } - /** Negates the value of this matrix in place. */ - inline void negate() { - for (unsigned int i = 0; i < Row; i++) - for (unsigned int j = 0; j < Col; j++) - data_[i][j] = -data_[i][j]; - } - - /** - * Sets the value of this matrix to the negation of matrix m. - * @param m the source matrix - */ - inline void negate(const RectMatrix& m) { - for (unsigned int i = 0; i < Row; i++) - for (unsigned int j = 0; j < Col; j++) - data_[i][j] = -m.data_[i][j]; - } - - /** - * Sets the value of this matrix to the sum of itself and m (*this += m). - * @param m the other matrix - */ - inline void add( const RectMatrix& m ) { - for (unsigned int i = 0; i < Row; i++) - for (unsigned int j = 0; j < Col; j++) - data_[i][j] += m.data_[i][j]; - } - - /** - * Sets the value of this matrix to the sum of m1 and m2 (*this = m1 + m2). - * @param m1 the first matrix - * @param m2 the second matrix - */ - inline void add( const RectMatrix& m1, const RectMatrix& m2 ) { - for (unsigned int i = 0; i < Row; i++) - for (unsigned int j = 0; j < Col; j++) - data_[i][j] = m1.data_[i][j] + m2.data_[i][j]; - } - - /** - * Sets the value of this matrix to the difference of itself and m (*this -= m). - * @param m the other matrix - */ - inline void sub( const RectMatrix& m ) { - for (unsigned int i = 0; i < Row; i++) - for (unsigned int j = 0; j < Col; j++) - data_[i][j] -= m.data_[i][j]; - } - - /** - * Sets the value of this matrix to the difference of matrix m1 and m2 (*this = m1 - m2). - * @param m1 the first matrix - * @param m2 the second matrix - */ - inline void sub( const RectMatrix& m1, const RectMatrix& m2){ - for (unsigned int i = 0; i < Row; i++) - for (unsigned int j = 0; j < Col; j++) - data_[i][j] = m1.data_[i][j] - m2.data_[i][j]; - } + /** + * Tests if this matrix is not equal to matrix m + * @return true if this matrix is not equal to the matrix m, return false otherwise + * @m matrix to be compared + */ + bool operator !=(const RectMatrix& m) { + return !(*this == m); + } - /** - * Sets the value of this matrix to the scalar multiplication of itself (*this *= s). - * @param s the scalar value - */ - inline void mul( double s ) { - for (unsigned int i = 0; i < Row; i++) - for (unsigned int j = 0; j < Col; j++) - data_[i][j] *= s; - } - - /** - * Sets the value of this matrix to the scalar multiplication of matrix m (*this = s * m). - * @param s the scalar value - * @param m the matrix - */ - inline void mul( double s, const RectMatrix& m ) { - for (unsigned int i = 0; i < Row; i++) - for (unsigned int j = 0; j < Col; j++) - data_[i][j] = s * m.data_[i][j]; - } - - /** - * Sets the value of this matrix to the scalar division of itself (*this /= s ). - * @param s the scalar value - */ - inline void div( double s) { - for (unsigned int i = 0; i < Row; i++) - for (unsigned int j = 0; j < Col; j++) - data_[i][j] /= s; - } - - /** - * Sets the value of this matrix to the scalar division of matrix m (*this = m /s). - * @param s the scalar value - * @param m the matrix - */ - inline void div( double s, const RectMatrix& m ) { - for (unsigned int i = 0; i < Row; i++) - for (unsigned int j = 0; j < Col; j++) - data_[i][j] = m.data_[i][j] / s; - } - - /** - * Multiples a scalar into every element of this matrix. - * @param s the scalar value - */ - RectMatrix& operator *=(const double s) { - this->mul(s); - return *this; - } - - /** - * Divides every element of this matrix by a scalar. - * @param s the scalar value - */ - RectMatrix& operator /=(const double s) { - this->div(s); - return *this; - } - - /** - * Sets the value of this matrix to the sum of the other matrix and itself (*this += m). - * @param m the other matrix - */ - RectMatrix& operator += (const RectMatrix& m) { - add(m); - return *this; - } - - /** - * Sets the value of this matrix to the differerence of itself and the other matrix (*this -= m) - * @param m the other matrix - */ - RectMatrix& operator -= (const RectMatrix& m){ - sub(m); - return *this; - } - - /** Return the transpose of this matrix */ - RectMatrix transpose(){ - RectMatrix result; + /** Negates the value of this matrix in place. */ + inline void negate() { + for (unsigned int i = 0; i < Row; i++) + for (unsigned int j = 0; j < Col; j++) + data_[i][j] = -data_[i][j]; + } - for (unsigned int i = 0; i < Row; i++) - for (unsigned int j = 0; j < Col; j++) - result(j, i) = data_[i][j]; + /** + * Sets the value of this matrix to the negation of matrix m. + * @param m the source matrix + */ + inline void negate(const RectMatrix& m) { + for (unsigned int i = 0; i < Row; i++) + for (unsigned int j = 0; j < Col; j++) + data_[i][j] = -m.data_[i][j]; + } + + /** + * Sets the value of this matrix to the sum of itself and m (*this += m). + * @param m the other matrix + */ + inline void add( const RectMatrix& m ) { + for (unsigned int i = 0; i < Row; i++) + for (unsigned int j = 0; j < Col; j++) + data_[i][j] += m.data_[i][j]; + } + + /** + * Sets the value of this matrix to the sum of m1 and m2 (*this = m1 + m2). + * @param m1 the first matrix + * @param m2 the second matrix + */ + inline void add( const RectMatrix& m1, const RectMatrix& m2 ) { + for (unsigned int i = 0; i < Row; i++) + for (unsigned int j = 0; j < Col; j++) + data_[i][j] = m1.data_[i][j] + m2.data_[i][j]; + } + + /** + * Sets the value of this matrix to the difference of itself and m (*this -= m). + * @param m the other matrix + */ + inline void sub( const RectMatrix& m ) { + for (unsigned int i = 0; i < Row; i++) + for (unsigned int j = 0; j < Col; j++) + data_[i][j] -= m.data_[i][j]; + } + + /** + * Sets the value of this matrix to the difference of matrix m1 and m2 (*this = m1 - m2). + * @param m1 the first matrix + * @param m2 the second matrix + */ + inline void sub( const RectMatrix& m1, const RectMatrix& m2){ + for (unsigned int i = 0; i < Row; i++) + for (unsigned int j = 0; j < Col; j++) + data_[i][j] = m1.data_[i][j] - m2.data_[i][j]; + } - return result; - } + /** + * Sets the value of this matrix to the scalar multiplication of itself (*this *= s). + * @param s the scalar value + */ + inline void mul( Real s ) { + for (unsigned int i = 0; i < Row; i++) + for (unsigned int j = 0; j < Col; j++) + data_[i][j] *= s; + } + + /** + * Sets the value of this matrix to the scalar multiplication of matrix m (*this = s * m). + * @param s the scalar value + * @param m the matrix + */ + inline void mul( Real s, const RectMatrix& m ) { + for (unsigned int i = 0; i < Row; i++) + for (unsigned int j = 0; j < Col; j++) + data_[i][j] = s * m.data_[i][j]; + } + + /** + * Sets the value of this matrix to the scalar division of itself (*this /= s ). + * @param s the scalar value + */ + inline void div( Real s) { + for (unsigned int i = 0; i < Row; i++) + for (unsigned int j = 0; j < Col; j++) + data_[i][j] /= s; + } + + /** + * Sets the value of this matrix to the scalar division of matrix m (*this = m /s). + * @param s the scalar value + * @param m the matrix + */ + inline void div( Real s, const RectMatrix& m ) { + for (unsigned int i = 0; i < Row; i++) + for (unsigned int j = 0; j < Col; j++) + data_[i][j] = m.data_[i][j] / s; + } + + /** + * Multiples a scalar into every element of this matrix. + * @param s the scalar value + */ + RectMatrix& operator *=(const Real s) { + this->mul(s); + return *this; + } + + /** + * Divides every element of this matrix by a scalar. + * @param s the scalar value + */ + RectMatrix& operator /=(const Real s) { + this->div(s); + return *this; + } + + /** + * Sets the value of this matrix to the sum of the other matrix and itself (*this += m). + * @param m the other matrix + */ + RectMatrix& operator += (const RectMatrix& m) { + add(m); + return *this; + } + + /** + * Sets the value of this matrix to the differerence of itself and the other matrix (*this -= m) + * @param m the other matrix + */ + RectMatrix& operator -= (const RectMatrix& m){ + sub(m); + return *this; + } + + /** Return the transpose of this matrix */ + RectMatrix transpose(){ + RectMatrix result; + + for (unsigned int i = 0; i < Row; i++) + for (unsigned int j = 0; j < Col; j++) + result(j, i) = data_[i][j]; + + return result; + } protected: Real data_[Row][Col];