--- trunk/src/math/SquareMatrix.hpp 2004/10/14 23:28:09 76 +++ trunk/src/math/SquareMatrix.hpp 2004/10/22 23:09:57 146 @@ -29,7 +29,7 @@ * @date 10/11/2004 * @version 1.0 */ -#ifndef MATH_SQUAREMATRIX_HPP + #ifndef MATH_SQUAREMATRIX_HPP #define MATH_SQUAREMATRIX_HPP #include "math/RectMatrix.hpp" @@ -45,286 +45,312 @@ namespace oopse { template class SquareMatrix : public RectMatrix { public: + typedef Real ElemType; + typedef Real* ElemPoinerType; - /** default constructor */ - SquareMatrix() { - for (unsigned int i = 0; i < Dim; i++) - for (unsigned int j = 0; j < Dim; j++) - data_[i][j] = 0.0; - } + /** default constructor */ + SquareMatrix() { + for (unsigned int i = 0; i < Dim; i++) + for (unsigned int j = 0; j < Dim; j++) + data_[i][j] = 0.0; + } - /** copy constructor */ - SquareMatrix(const RectMatrix& m) : RectMatrix(m) { - } - - /** copy assignment operator */ - SquareMatrix& operator =(const RectMatrix& m) { - RectMatrix::operator=(m); - return *this; - } - - /** Retunrs an identity matrix*/ - - static SquareMatrix identity() { - SquareMatrix m; + /** copy constructor */ + SquareMatrix(const RectMatrix& m) : RectMatrix(m) { + } - 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; + /** copy assignment operator */ + SquareMatrix& operator =(const RectMatrix& m) { + RectMatrix::operator=(m); + return *this; + } + + /** Retunrs an identity matrix*/ - return m; - } + static SquareMatrix identity() { + SquareMatrix m; + + 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; - /** Retunrs the inversion of this matrix. */ - SquareMatrix inverse() { - SquareMatrix result; + return m; + } - return result; - } + /** + * Retunrs the inversion of this matrix. + * @todo need implementation + */ + SquareMatrix inverse() { + SquareMatrix result; - /** Returns the determinant of this matrix. */ - double determinant() const { - double det; - return det; - } + return result; + } - /** Returns the trace of this matrix. */ - double trace() const { - double tmp = 0; - - for (unsigned int i = 0; i < Dim ; i++) - tmp += data_[i][i]; + /** + * Returns the determinant of this matrix. + * @todo need implementation + */ + Real determinant() const { + Real det; + return det; + } - return tmp; - } + /** Returns the trace of this matrix. */ + Real trace() const { + Real tmp = 0; + + for (unsigned int i = 0; i < Dim ; i++) + tmp += data_[i][i]; - /** Tests if this matrix is symmetrix. */ - 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]) > oopse::epsilon) - return false; - - return true; - } + return tmp; + } - /** Tests if this matrix is orthogonal. */ - bool isOrthogonal() { - SquareMatrix tmp; + /** Tests if this matrix is symmetrix. */ + 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]) > oopse::epsilon) + return false; + + return true; + } - tmp = *this * transpose(); + /** Tests if this matrix is orthogonal. */ + bool isOrthogonal() { + SquareMatrix tmp; - return tmp.isDiagonal(); - } + tmp = *this * transpose(); - /** 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]) > oopse::epsilon) - return false; - - return true; - } + return tmp.isDiagonal(); + } - /** Tests if this matrix is the unit matrix. */ - bool isUnitMatrix() const { - if (!isDiagonal()) - return false; - - for (unsigned int i = 0; i < Dim ; i++) - if (fabs(data_[i][i] - 1) > oopse::epsilon) + /** 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]) > oopse::epsilon) + return false; + + return true; + } + + /** Tests if this matrix is the unit matrix. */ + bool isUnitMatrix() const { + if (!isDiagonal()) return false; - return true; - } + for (unsigned int i = 0; i < Dim ; i++) + if (fabs(data_[i][i] - 1) > oopse::epsilon) + return false; + + return true; + } - void diagonalize() { - jacobi(m, eigenValues, ortMat); - } - - /** - * Finds the eigenvalues and eigenvectors of a symmetric matrix - * @param eigenvals a reference to a vector3 where the - * eigenvalues will be stored. The eigenvalues are ordered so - * that eigenvals[0] <= eigenvals[1] <= eigenvals[2]. - * @return an orthogonal matrix whose ith column is an - * eigenvector for the eigenvalue eigenvals[i] - */ - SquareMatrix findEigenvectors(Vector& eigenValues) { - SquareMatrix ortMat; - - if ( !isSymmetric()){ - throw(); + /** @todo need implementation */ + void diagonalize() { + //jacobi(m, eigenValues, ortMat); } - - SquareMatrix m(*this); - jacobi(m, eigenValues, ortMat); - return ortMat; - } - /** - * Jacobi iteration routines for computing eigenvalues/eigenvectors of - * real symmetric matrix - * - * @return true if success, otherwise return false - * @param a source matrix - * @param w output eigenvalues - * @param v output eigenvectors - */ - void jacobi(const SquareMatrix& a, - Vector& w, - SquareMatrix& v); + /** + * 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 -#define ROT(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) -#define MAX_ROTATIONS 60 +/*========================================================================= -template -void SquareMatrix::jacobi(SquareMatrix& a, - Vector& w, - SquareMatrix& v) { - const int N = Dim; - int i, j, k, iq, ip; - double tresh, theta, tau, t, sm, s, h, g, c; - double tmp; - Vector b, z; + Program: Visualization Toolkit + Module: $RCSfile: SquareMatrix.hpp,v $ - // initialize - for (ip=0; ip 4 && (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; - for (j=0;j= MAX_ROTATIONS ) - return false; + // 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 + 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; - // sort eigenfunctions - for (j=0; j= tmp) - { - k = i; - tmp = w(k); - } - } - if (k != j) - { - w(k) = w(j); - w(j) = tmp; - for (i=0; i 4) { + b = new Real[n]; + z = new Real[n]; + } - // insure eigenvector consistency (i.e., Jacobi can compute - // vectors that are negative of one another (.707,.707,0) and - // (-.707,-.707,0). This can reek havoc in - // hyperstreamline/other stuff. We will select the most - // positive eigenvector. - int numPos; - for (j=0; j= 0.0 ) numPos++; - if ( numPos < 2 ) for(i=0; i 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 +