--- trunk/OOPSE-4/src/math/SquareMatrix.hpp 2004/10/13 06:51:09 1563 +++ trunk/OOPSE-4/src/math/SquareMatrix.hpp 2004/10/13 23:53:40 1567 @@ -32,7 +32,7 @@ #ifndef MATH_SQUAREMATRIX_HPP #define MATH_SQUAREMATRIX_HPP -#include "Vector3d.hpp" +#include "math/RectMatrix.hpp" namespace oopse { @@ -43,7 +43,7 @@ namespace oopse { * @template Dim the dimension of the square matrix */ template - class SquareMatrix{ + class SquareMatrix : public RectMatrix { public: /** default constructor */ @@ -53,345 +53,44 @@ namespace oopse { data_[i][j] = 0.0; } - /** Constructs and initializes every element of this matrix to a scalar */ - SquareMatrix(double s) { - for (unsigned int i = 0; i < Dim; i++) - for (unsigned int j = 0; j < Dim; j++) - data_[i][j] = s; - } - /** copy constructor */ - SquareMatrix(const SquareMatrix& m) { - *this = m; + SquareMatrix(const RectMatrix& m) : RectMatrix(m) { } - /** destructor*/ - ~SquareMatrix() {} - /** copy assignment operator */ - SquareMatrix& operator =(const SquareMatrix& m) { - for (unsigned int i = 0; i < Dim; i++) - for (unsigned int j = 0; j < Dim; j++) - data_[i][j] = m.data_[i][j]; + SquareMatrix& operator =(const RectMatrix& m) { + RectMatrix::operator=(m); + 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) { - return data_[i][j]; - } + + /** Retunrs an identity matrix*/ - /** - * 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]; - } - - /** - * 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; - - for (unsigned int i = 0; i < Dim; i++) - v[i] = data_[row][i]; - - return v; - } - - /** - * 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) { - Vector v; - - for (unsigned int i = 0; i < Dim; i++) - data_[row][i] = v[i]; - } - - /** - * 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 < Dim; i++) - v[i] = data_[i][col]; - - return v; - } - - /** - * 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){ - Vector v; - - for (unsigned int i = 0; i < Dim; i++) - data_[i][col] = v[i]; - } - - /** Negates the value of this matrix in place. */ - inline void negate() { - for (unsigned int i = 0; i < Dim; i++) - for (unsigned int j = 0; j < Dim; 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 SquareMatrix& m) { - for (unsigned int i = 0; i < Dim; i++) - for (unsigned int j = 0; j < Dim; 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 SquareMatrix& m ) { - for (unsigned int i = 0; i < Dim; i++) - for (unsigned int j = 0; j < Dim; 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 SquareMatrix& m1, const SquareMatrix& m2 ) { - for (unsigned int i = 0; i < Dim; i++) - for (unsigned int j = 0; j < Dim; 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 SquareMatrix& m ) { - for (unsigned int i = 0; i < Dim; i++) - for (unsigned int j = 0; j < Dim; 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 SquareMatrix& m1, const Vector &m2){ - for (unsigned int i = 0; i < Dim; i++) - for (unsigned int j = 0; j < Dim; j++) - data_[i][j] = m1.data_[i][j] - m2.data_[i][j]; - } - - /** - * 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 < Dim; i++) - for (unsigned int j = 0; j < Dim; 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 SquareMatrix& m ) { - for (unsigned int i = 0; i < Dim; i++) - for (unsigned int j = 0; j < Dim; j++) - data_[i][j] = s * m.data_[i][j]; - } - - /** - * Sets the value of this matrix to the multiplication of this matrix and matrix m - * (*this = *this * m). - * @param m the matrix - */ - inline void mul(const SquareMatrix& m ) { - SquareMatrix tmp(*this); + static SquareMatrix identity() { + SquareMatrix m; - for (unsigned int i = 0; i < Dim; i++) - for (unsigned int j = 0; j < Dim; j++) { - - data_[i][j] = 0.0; - for (unsigned int k = 0; k < Dim; k++) - data_[i][j] = tmp.data_[i][k] * m.data_[k][j] - } - } - - /** - * Sets the value of this matrix to the left multiplication of matrix m into itself - * (*this = m * *this). - * @param m the matrix - */ - inline void leftmul(const SquareMatrix& m ) { - SquareMatrix tmp(*this); - - for (unsigned int i = 0; i < Dim; i++) - for (unsigned int j = 0; j < Dim; j++) { - - data_[i][j] = 0.0; - for (unsigned int k = 0; k < Dim; k++) - data_[i][j] = m.data_[i][k] * tmp.data_[k][j] - } - } - - /** - * Sets the value of this matrix to the multiplication of matrix m1 and matrix m2 - * (*this = m1 * m2). - * @param m1 the first matrix - * @param m2 the second matrix - */ - inline void mul(const SquareMatrix& m1, - const SquareMatrix& m2 ) { - for (unsigned int i = 0; i < Dim; i++) - for (unsigned int j = 0; j < Dim; j++) { - - data_[i][j] = 0.0; - for (unsigned int k = 0; k < Dim; k++) - data_[i][j] = m1.data_[i][k] * m2.data_[k][j] - } + for (unsigned int i = 0; i < Dim; i++) + for (unsigned int j = 0; j < Dim; j++) + if (i == j) + m(i, j) = 1.0; + else + m(i, j) = 0.0; + return m; } - - /** - * 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 < Dim; i++) - for (unsigned int j = 0; j < Dim; j++) - data_[i][j] /= s; - } - - inline SquareMatrix& operator=(const SquareMatrix& v) { - if (this == &v) - return *this; - - for (unsigned int i = 0; i < Dim; i++) - data_[i] = v[i]; - - return *this; - } - - /** - * Sets the value of this matrix to the scalar division of matrix v1 (*this = v1 / s ). - * @paran v1 the source matrix - * @param s the scalar value - */ - inline void div( const SquareMatrix& v1, double s ) { - for (unsigned int i = 0; i < Dim; i++) - data_[i] = v1.data_[i] / s; - } - /** - * Multiples a scalar into every element of this matrix. - * @param s the scalar value - */ - SquareMatrix& operator *=(const double s) { - this->mul(s); - return *this; - } + /** Retunrs the inversion of this matrix. */ + SquareMatrix inverse() { + SquareMatrix result; - /** - * Divides every element of this matrix by a scalar. - * @param s the scalar value - */ - SquareMatrix& operator /=(const double s) { - this->div(s); - return *this; + return result; } - /** - * Sets the value of this matrix to the sum of the other matrix and itself (*this += m). - * @param m the other matrix - */ - SquareMatrix& operator += (const SquareMatrix& 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 - */ - SquareMatrix& operator -= (const SquareMatrix& m){ - sub(m); - return *this; - } - - /** set this matrix to an identity matrix*/ - - void identity() { - for (unsigned int i = 0; i < Dim; i++) - for (unsigned int i = 0; i < Dim; i++) - if (i == j) - data_[i][j] = 1.0; - else - data_[i][j] = 0.0; - } - - /** Sets the value of this matrix to the inversion of itself. */ - void inverse() { - inverse(*this); - } - - /** - * Sets the value of this matrix to the inversion of other matrix. - * @ param m the source matrix - */ - void inverse(const SquareMatrix& m); - /** Sets the value of this matrix to the transpose of itself. */ - void transpose() { - for (unsigned int i = 0; i < Dim - 1; i++) - for (unsigned int j = i; j < Dim; j++) - std::swap(data_[i][j], data_[j][i]); - } - /** - * Sets the value of this matrix to the transpose of other matrix. - * @ param m the source matrix - */ - void transpose(const SquareMatrix& m) { - - if (this == &m) { - transpose(); - } else { - for (unsigned int i = 0; i < Dim; i++) - for (unsigned int j =0; j < Dim; j++) - data_[i][j] = m.data_[i][j]; - } - } - /** Returns the determinant of this matrix. */ double determinant() const { - + double det; + return det; } /** Returns the trace of this matrix. */ @@ -408,26 +107,26 @@ namespace oopse { bool isSymmetric() const { for (unsigned int i = 0; i < Dim - 1; i++) for (unsigned int j = i; j < Dim; j++) - if (fabs(data_[i][j] - data_[j][i]) > epsilon) + if (fabs(data_[i][j] - data_[j][i]) > oopse::epsilon) return false; return true; } /** Tests if this matrix is orthogona. */ - bool isOrthogonal() const { - SquareMatrix t(*this); + bool isOrthogonal() { + SquareMatrix tmp; - t.transpose(); + tmp = *this * transpose(); - return isUnitMatrix(*this * t); + return tmp.isUnitMatrix(); } /** Tests if this matrix is diagonal. */ bool isDiagonal() const { for (unsigned int i = 0; i < Dim ; i++) for (unsigned int j = 0; j < Dim; j++) - if (i !=j && fabs(data_[i][j]) > epsilon) + if (i !=j && fabs(data_[i][j]) > oopse::epsilon) return false; return true; @@ -439,92 +138,13 @@ namespace oopse { return false; for (unsigned int i = 0; i < Dim ; i++) - if (fabs(data_[i][i] - 1) > epsilon) + if (fabs(data_[i][i] - 1) > oopse::epsilon) return false; return true; - } - - protected: - double data_[Dim][Dim]; /**< matrix element */ + } };//end SquareMatrix - - /** Negate the value of every element of this matrix. */ - template - inline SquareMatrix operator -(const SquareMatrix& m) { - SquareMatrix result(m); - - result.negate(); - - return result; - } - - /** - * Return the sum of two matrixes (m1 + m2). - * @return the sum of two matrixes - * @param m1 the first matrix - * @param m2 the second matrix - */ - template - inline SquareMatrix operator + (const SquareMatrix& m1, - const SquareMatrix& m2) { - SquareMatrixresult; - - result.add(m1, m2); - - return result; - } - - /** - * Return the difference of two matrixes (m1 - m2). - * @return the sum of two matrixes - * @param m1 the first matrix - * @param m2 the second matrix - */ - template - inline SquareMatrix operator - (const SquareMatrix& m1, - const SquareMatrix& m2) { - SquareMatrixresult; - - result.sub(m1, m2); - - return result; - } - - /** - * Return the multiplication of two matrixes (m1 * m2). - * @return the multiplication of two matrixes - * @param m1 the first matrix - * @param m2 the second matrix - */ - template - inline SquareMatrix operator *(const SquareMatrix& m1, - const SquareMatrix& m2) { - SquareMatrix result; - - result.mul(m1, m2); - - return result; - } - - /** - * Return the multiplication of matrixes m and vector v (m * v). - * @return the multiplication of matrixes and vector - * @param m the matrix - * @param v the vector - */ - template - inline Vector operator *(const SquareMatrix& m, - const SquareMatrix& v) { - Vector result; - - for (unsigned int i = 0; i < Dim ; i++) - for (unsigned int j = 0; j < Dim ; j++) - result[i] += m(i, j) * v[j]; - - return result; - } } #endif //MATH_SQUAREMATRIX_HPP