--- trunk/src/math/SquareMatrix.hpp 2004/10/13 06:51:09 70 +++ trunk/src/math/SquareMatrix.hpp 2004/10/20 18:07:08 123 @@ -29,10 +29,10 @@ * @date 10/11/2004 * @version 1.0 */ -#ifndef MATH_SQUAREMATRIX_HPP + #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,350 +53,53 @@ 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]; - } - - /** - * 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]; - } - - /** - * 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); - - 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] - } - - } - - /** - * 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]; - + SquareMatrix& operator =(const RectMatrix& m) { + RectMatrix::operator=(m); 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; - } + + /** Retunrs an identity matrix*/ - /** - * Multiples a scalar into every element of this matrix. - * @param s the scalar value - */ - SquareMatrix& operator *=(const double s) { - this->mul(s); - return *this; - } - - /** - * Divides every element of this matrix by a scalar. - * @param s the scalar value - */ - SquareMatrix& 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 - */ - 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() { + static SquareMatrix identity() { + SquareMatrix m; + for (unsigned int i = 0; i < Dim; i++) - for (unsigned int i = 0; i < Dim; i++) + for (unsigned int j = 0; j < Dim; j++) if (i == j) - data_[i][j] = 1.0; + m(i, j) = 1.0; else - data_[i][j] = 0.0; - } + m(i, j) = 0.0; - /** Sets the value of this matrix to the inversion of itself. */ - void inverse() { - inverse(*this); + return m; } - /** - * 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]); - } + /** + * Retunrs the inversion of this matrix. + * @todo need implementation + */ + SquareMatrix inverse() { + SquareMatrix result; - /** - * 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 { + return result; + } + /** + * Returns the determinant of this matrix. + * @todo need implementation + */ + Real determinant() const { + Real det; + return det; } /** Returns the trace of this matrix. */ - double trace() const { - double tmp = 0; + Real trace() const { + Real tmp = 0; for (unsigned int i = 0; i < Dim ; i++) tmp += data_[i][i]; @@ -408,26 +111,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); + /** Tests if this matrix is orthogonal. */ + bool isOrthogonal() { + SquareMatrix tmp; - t.transpose(); + tmp = *this * transpose(); - return isUnitMatrix(*this * t); + return tmp.isDiagonal(); } /** 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 +142,250 @@ 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; + } + + /** @todo need implementation */ + void diagonalize() { + //jacobi(m, eigenValues, ortMat); } - - protected: - double data_[Dim][Dim]; /**< matrix element */ + /** + * Jacobi iteration routines for computing eigenvalues/eigenvectors of + * real symmetric matrix + * + * @return true if success, otherwise return false + * @param a symmetric matrix whose eigenvectors are to be computed. On return, the matrix is + * overwritten + * @param w will contain the eigenvalues of the matrix On return of this function + * @param v the columns of this matrix will contain the eigenvectors. The eigenvectors are + * normalized and mutually orthogonal. + */ + + static int jacobi(SquareMatrix& a, Vector& d, + SquareMatrix& v); };//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; + Program: Visualization Toolkit + Module: $RCSfile: SquareMatrix.hpp,v $ - result.add(m1, m2); + Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen + All rights reserved. + See Copyright.txt or http://www.kitware.com/Copyright.htm for details. - 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; + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the above copyright notice for more information. - 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; +#define VTK_ROTATE(a,i,j,k,l) g=a(i, j);h=a(k, l);a(i, j)=g-s*(h+g*tau);\ + a(k, l)=h+s*(g-h*tau) - result.mul(m1, m2); +#define VTK_MAX_ROTATIONS 20 - 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 - */ + // Jacobi iteration for the solution of eigenvectors/eigenvalues of a nxn + // real symmetric matrix. Square nxn matrix a; size of matrix in n; + // output eigenvalues in w; and output eigenvectors in v. Resulting + // eigenvalues/vectors are sorted in decreasing order; eigenvectors are + // normalized. template - inline Vector operator *(const SquareMatrix& m, - const SquareMatrix& v) { - Vector result; + int SquareMatrix::jacobi(SquareMatrix& a, Vector& w, + SquareMatrix& v) { + const int n = Dim; + int i, j, k, iq, ip, numPos; + Real tresh, theta, tau, t, sm, s, h, g, c, tmp; + Real bspace[4], zspace[4]; + Real *b = bspace; + Real *z = zspace; - 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; + // only allocate memory if the matrix is large + if (n > 4) + { + b = new Real[n]; + z = new Real[n]; + } + + // initialize + for (ip=0; ip 3 && (fabs(w[ip])+g) == fabs(w[ip]) + && (fabs(w[iq])+g) == fabs(w[iq])) + { + a(ip, iq) = 0.0; + } + else if (fabs(a(ip, iq)) > tresh) + { + h = w[iq] - w[ip]; + if ( (fabs(h)+g) == fabs(h)) + { + t = (a(ip, iq)) / h; + } + else + { + theta = 0.5*h / (a(ip, iq)); + t = 1.0 / (fabs(theta)+sqrt(1.0+theta*theta)); + if (theta < 0.0) + { + t = -t; + } + } + c = 1.0 / sqrt(1+t*t); + s = t*c; + tau = s/(1.0+c); + h = t*a(ip, iq); + z[ip] -= h; + z[iq] += h; + w[ip] -= h; + w[iq] += h; + a(ip, iq)=0.0; + + // ip already shifted left by 1 unit + for (j = 0;j <= ip-1;j++) + { + VTK_ROTATE(a,j,ip,j,iq); + } + // ip and iq already shifted left by 1 unit + for (j = ip+1;j <= iq-1;j++) + { + VTK_ROTATE(a,ip,j,j,iq); + } + // iq already shifted left by 1 unit + for (j=iq+1; j= VTK_MAX_ROTATIONS ) + { + std::cout << "vtkMath::Jacobi: Error extracting eigenfunctions" << std::endl; + return 0; + } + + // sort eigenfunctions these changes do not affect accuracy + for (j=0; j= tmp) // why exchage if same? + { + k = i; + tmp = w[k]; + } + } + if (k != j) + { + w[k] = w[j]; + w[j] = tmp; + for (i=0; i> 1) + (n & 1); + for (j=0; j= 0.0 ) + { + numPos++; + } + } + // if ( numPos < ceil(double(n)/double(2.0)) ) + if ( numPos < ceil_half_n) + { + for(i=0; i 4) + { + delete [] b; + delete [] z; + } + return 1; } + + } #endif //MATH_SQUAREMATRIX_HPP +