| 1 | 
< | 
 /* | 
| 1 | 
> | 
/* | 
| 2 | 
  | 
 * Copyright (c) 2005 The University of Notre Dame. All Rights Reserved. | 
| 3 | 
  | 
 * | 
| 4 | 
  | 
 * The University of Notre Dame grants you ("Licensee") a | 
| 45 | 
  | 
 * @date 10/11/2004 | 
| 46 | 
  | 
 * @version 1.0 | 
| 47 | 
  | 
 */ | 
| 48 | 
< | 
 #ifndef MATH_SQUAREMATRIX_HPP  | 
| 48 | 
> | 
#ifndef MATH_SQUAREMATRIX_HPP  | 
| 49 | 
  | 
#define MATH_SQUAREMATRIX_HPP  | 
| 50 | 
  | 
 | 
| 51 | 
  | 
#include "math/RectMatrix.hpp" | 
| 52 | 
  | 
 | 
| 53 | 
  | 
namespace oopse { | 
| 54 | 
  | 
 | 
| 55 | 
< | 
    /** | 
| 56 | 
< | 
     * @class SquareMatrix SquareMatrix.hpp "math/SquareMatrix.hpp" | 
| 57 | 
< | 
     * @brief A square matrix class | 
| 58 | 
< | 
     * @template Real the element type | 
| 59 | 
< | 
     * @template Dim the dimension of the square matrix | 
| 60 | 
< | 
     */ | 
| 61 | 
< | 
    template<typename Real, int Dim> | 
| 62 | 
< | 
    class SquareMatrix : public RectMatrix<Real, Dim, Dim> { | 
| 63 | 
< | 
        public: | 
| 64 | 
< | 
            typedef Real ElemType; | 
| 65 | 
< | 
            typedef Real* ElemPoinerType; | 
| 55 | 
> | 
  /** | 
| 56 | 
> | 
   * @class SquareMatrix SquareMatrix.hpp "math/SquareMatrix.hpp" | 
| 57 | 
> | 
   * @brief A square matrix class | 
| 58 | 
> | 
   * @template Real the element type | 
| 59 | 
> | 
   * @template Dim the dimension of the square matrix | 
| 60 | 
> | 
   */ | 
| 61 | 
> | 
  template<typename Real, int Dim> | 
| 62 | 
> | 
  class SquareMatrix : public RectMatrix<Real, Dim, Dim> { | 
| 63 | 
> | 
  public: | 
| 64 | 
> | 
    typedef Real ElemType; | 
| 65 | 
> | 
    typedef Real* ElemPoinerType; | 
| 66 | 
  | 
 | 
| 67 | 
< | 
            /** default constructor */ | 
| 68 | 
< | 
            SquareMatrix() { | 
| 69 | 
< | 
                for (unsigned int i = 0; i < Dim; i++) | 
| 70 | 
< | 
                    for (unsigned int j = 0; j < Dim; j++) | 
| 71 | 
< | 
                        data_[i][j] = 0.0; | 
| 72 | 
< | 
             } | 
| 67 | 
> | 
    /** default constructor */ | 
| 68 | 
> | 
    SquareMatrix() { | 
| 69 | 
> | 
      for (unsigned int i = 0; i < Dim; i++) | 
| 70 | 
> | 
        for (unsigned int j = 0; j < Dim; j++) | 
| 71 | 
> | 
          this->data_[i][j] = 0.0; | 
| 72 | 
> | 
    } | 
| 73 | 
  | 
 | 
| 74 | 
< | 
            /** Constructs and initializes every element of this matrix to a scalar */  | 
| 75 | 
< | 
            SquareMatrix(Real s) : RectMatrix<Real, Dim, Dim>(s){ | 
| 76 | 
< | 
            } | 
| 74 | 
> | 
    /** Constructs and initializes every element of this matrix to a scalar */  | 
| 75 | 
> | 
    SquareMatrix(Real s) : RectMatrix<Real, Dim, Dim>(s){ | 
| 76 | 
> | 
    } | 
| 77 | 
  | 
 | 
| 78 | 
< | 
            /** Constructs and initializes from an array */  | 
| 79 | 
< | 
            SquareMatrix(Real* array) : RectMatrix<Real, Dim, Dim>(array){ | 
| 80 | 
< | 
            } | 
| 78 | 
> | 
    /** Constructs and initializes from an array */  | 
| 79 | 
> | 
    SquareMatrix(Real* array) : RectMatrix<Real, Dim, Dim>(array){ | 
| 80 | 
> | 
    } | 
| 81 | 
  | 
 | 
| 82 | 
  | 
 | 
| 83 | 
< | 
            /** copy constructor */ | 
| 84 | 
< | 
            SquareMatrix(const RectMatrix<Real, Dim, Dim>& m) : RectMatrix<Real, Dim, Dim>(m) { | 
| 85 | 
< | 
            } | 
| 83 | 
> | 
    /** copy constructor */ | 
| 84 | 
> | 
    SquareMatrix(const RectMatrix<Real, Dim, Dim>& m) : RectMatrix<Real, Dim, Dim>(m) { | 
| 85 | 
> | 
    } | 
| 86 | 
  | 
             | 
| 87 | 
< | 
            /** copy assignment operator */ | 
| 88 | 
< | 
            SquareMatrix<Real, Dim>& operator =(const RectMatrix<Real, Dim, Dim>& m) { | 
| 89 | 
< | 
                RectMatrix<Real, Dim, Dim>::operator=(m); | 
| 90 | 
< | 
                return *this; | 
| 91 | 
< | 
            } | 
| 87 | 
> | 
    /** copy assignment operator */ | 
| 88 | 
> | 
    SquareMatrix<Real, Dim>& operator =(const RectMatrix<Real, Dim, Dim>& m) { | 
| 89 | 
> | 
      RectMatrix<Real, Dim, Dim>::operator=(m); | 
| 90 | 
> | 
      return *this; | 
| 91 | 
> | 
    } | 
| 92 | 
  | 
                                    | 
| 93 | 
< | 
            /** Retunrs  an identity matrix*/ | 
| 93 | 
> | 
    /** Retunrs  an identity matrix*/ | 
| 94 | 
  | 
 | 
| 95 | 
< | 
           static SquareMatrix<Real, Dim> identity() { | 
| 96 | 
< | 
                SquareMatrix<Real, Dim> m; | 
| 95 | 
> | 
    static SquareMatrix<Real, Dim> identity() { | 
| 96 | 
> | 
      SquareMatrix<Real, Dim> m; | 
| 97 | 
  | 
                 | 
| 98 | 
< | 
                for (unsigned int i = 0; i < Dim; i++)  | 
| 99 | 
< | 
                    for (unsigned int j = 0; j < Dim; j++)  | 
| 100 | 
< | 
                        if (i == j) | 
| 101 | 
< | 
                            m(i, j) = 1.0; | 
| 102 | 
< | 
                        else | 
| 103 | 
< | 
                            m(i, j) = 0.0; | 
| 98 | 
> | 
      for (unsigned int i = 0; i < Dim; i++)  | 
| 99 | 
> | 
        for (unsigned int j = 0; j < Dim; j++)  | 
| 100 | 
> | 
          if (i == j) | 
| 101 | 
> | 
            m(i, j) = 1.0; | 
| 102 | 
> | 
          else | 
| 103 | 
> | 
            m(i, j) = 0.0; | 
| 104 | 
  | 
 | 
| 105 | 
< | 
                return m; | 
| 106 | 
< | 
            } | 
| 105 | 
> | 
      return m; | 
| 106 | 
> | 
    } | 
| 107 | 
  | 
 | 
| 108 | 
< | 
            /**  | 
| 109 | 
< | 
             * Retunrs  the inversion of this matrix.  | 
| 110 | 
< | 
             * @todo need implementation | 
| 111 | 
< | 
             */ | 
| 112 | 
< | 
             SquareMatrix<Real, Dim>  inverse() { | 
| 113 | 
< | 
                 SquareMatrix<Real, Dim> result; | 
| 108 | 
> | 
    /**  | 
| 109 | 
> | 
     * Retunrs  the inversion of this matrix.  | 
| 110 | 
> | 
     * @todo need implementation | 
| 111 | 
> | 
     */ | 
| 112 | 
> | 
    SquareMatrix<Real, Dim>  inverse() { | 
| 113 | 
> | 
      SquareMatrix<Real, Dim> result; | 
| 114 | 
  | 
 | 
| 115 | 
< | 
                 return result; | 
| 116 | 
< | 
            }         | 
| 115 | 
> | 
      return result; | 
| 116 | 
> | 
    }         | 
| 117 | 
  | 
 | 
| 118 | 
< | 
            /** | 
| 119 | 
< | 
             * Returns the determinant of this matrix. | 
| 120 | 
< | 
             * @todo need implementation | 
| 121 | 
< | 
             */ | 
| 122 | 
< | 
            Real determinant() const { | 
| 123 | 
< | 
                Real det; | 
| 124 | 
< | 
                return det; | 
| 125 | 
< | 
            } | 
| 118 | 
> | 
    /** | 
| 119 | 
> | 
     * Returns the determinant of this matrix. | 
| 120 | 
> | 
     * @todo need implementation | 
| 121 | 
> | 
     */ | 
| 122 | 
> | 
    Real determinant() const { | 
| 123 | 
> | 
      Real det; | 
| 124 | 
> | 
      return det; | 
| 125 | 
> | 
    } | 
| 126 | 
  | 
 | 
| 127 | 
< | 
            /** Returns the trace of this matrix. */ | 
| 128 | 
< | 
            Real trace() const { | 
| 129 | 
< | 
               Real tmp = 0; | 
| 127 | 
> | 
    /** Returns the trace of this matrix. */ | 
| 128 | 
> | 
    Real trace() const { | 
| 129 | 
> | 
      Real tmp = 0; | 
| 130 | 
  | 
                | 
| 131 | 
< | 
                for (unsigned int i = 0; i < Dim ; i++) | 
| 132 | 
< | 
                    tmp += data_[i][i]; | 
| 131 | 
> | 
      for (unsigned int i = 0; i < Dim ; i++) | 
| 132 | 
> | 
        tmp += this->data_[i][i]; | 
| 133 | 
  | 
 | 
| 134 | 
< | 
                return tmp; | 
| 135 | 
< | 
            } | 
| 134 | 
> | 
      return tmp; | 
| 135 | 
> | 
    } | 
| 136 | 
  | 
 | 
| 137 | 
< | 
            /** Tests if this matrix is symmetrix. */             | 
| 138 | 
< | 
            bool isSymmetric() const { | 
| 139 | 
< | 
                for (unsigned int i = 0; i < Dim - 1; i++) | 
| 140 | 
< | 
                    for (unsigned int j = i; j < Dim; j++) | 
| 141 | 
< | 
                        if (fabs(data_[i][j] - data_[j][i]) > oopse::epsilon)  | 
| 142 | 
< | 
                            return false; | 
| 137 | 
> | 
    /** Tests if this matrix is symmetrix. */             | 
| 138 | 
> | 
    bool isSymmetric() const { | 
| 139 | 
> | 
      for (unsigned int i = 0; i < Dim - 1; i++) | 
| 140 | 
> | 
        for (unsigned int j = i; j < Dim; j++) | 
| 141 | 
> | 
          if (fabs(this->data_[i][j] - this->data_[j][i]) > oopse::epsilon)  | 
| 142 | 
> | 
            return false; | 
| 143 | 
  | 
                         | 
| 144 | 
< | 
                return true; | 
| 145 | 
< | 
            } | 
| 144 | 
> | 
      return true; | 
| 145 | 
> | 
    } | 
| 146 | 
  | 
 | 
| 147 | 
< | 
            /** Tests if this matrix is orthogonal. */             | 
| 148 | 
< | 
            bool isOrthogonal() { | 
| 149 | 
< | 
                SquareMatrix<Real, Dim> tmp; | 
| 147 | 
> | 
    /** Tests if this matrix is orthogonal. */             | 
| 148 | 
> | 
    bool isOrthogonal() { | 
| 149 | 
> | 
      SquareMatrix<Real, Dim> tmp; | 
| 150 | 
  | 
 | 
| 151 | 
< | 
                tmp = *this * transpose(); | 
| 151 | 
> | 
      tmp = *this * transpose(); | 
| 152 | 
  | 
 | 
| 153 | 
< | 
                return tmp.isDiagonal(); | 
| 154 | 
< | 
            } | 
| 153 | 
> | 
      return tmp.isDiagonal(); | 
| 154 | 
> | 
    } | 
| 155 | 
  | 
 | 
| 156 | 
< | 
            /** Tests if this matrix is diagonal. */ | 
| 157 | 
< | 
            bool isDiagonal() const { | 
| 158 | 
< | 
                for (unsigned int i = 0; i < Dim ; i++) | 
| 159 | 
< | 
                    for (unsigned int j = 0; j < Dim; j++) | 
| 160 | 
< | 
                        if (i !=j && fabs(data_[i][j]) > oopse::epsilon)  | 
| 161 | 
< | 
                            return false; | 
| 156 | 
> | 
    /** Tests if this matrix is diagonal. */ | 
| 157 | 
> | 
    bool isDiagonal() const { | 
| 158 | 
> | 
      for (unsigned int i = 0; i < Dim ; i++) | 
| 159 | 
> | 
        for (unsigned int j = 0; j < Dim; j++) | 
| 160 | 
> | 
          if (i !=j && fabs(this->data_[i][j]) > oopse::epsilon)  | 
| 161 | 
> | 
            return false; | 
| 162 | 
  | 
                         | 
| 163 | 
< | 
                return true; | 
| 164 | 
< | 
            } | 
| 163 | 
> | 
      return true; | 
| 164 | 
> | 
    } | 
| 165 | 
  | 
 | 
| 166 | 
< | 
            /** Tests if this matrix is the unit matrix. */ | 
| 167 | 
< | 
            bool isUnitMatrix() const { | 
| 168 | 
< | 
                if (!isDiagonal()) | 
| 169 | 
< | 
                    return false; | 
| 166 | 
> | 
    /** Tests if this matrix is the unit matrix. */ | 
| 167 | 
> | 
    bool isUnitMatrix() const { | 
| 168 | 
> | 
      if (!isDiagonal()) | 
| 169 | 
> | 
        return false; | 
| 170 | 
  | 
                 | 
| 171 | 
< | 
                for (unsigned int i = 0; i < Dim ; i++) | 
| 172 | 
< | 
                    if (fabs(data_[i][i] - 1) > oopse::epsilon) | 
| 173 | 
< | 
                        return false; | 
| 171 | 
> | 
      for (unsigned int i = 0; i < Dim ; i++) | 
| 172 | 
> | 
        if (fabs(this->data_[i][i] - 1) > oopse::epsilon) | 
| 173 | 
> | 
          return false; | 
| 174 | 
  | 
                     | 
| 175 | 
< | 
                return true; | 
| 176 | 
< | 
            }          | 
| 175 | 
> | 
      return true; | 
| 176 | 
> | 
    }          | 
| 177 | 
  | 
 | 
| 178 | 
< | 
            /** @todo need implementation */ | 
| 179 | 
< | 
            void diagonalize() { | 
| 180 | 
< | 
                //jacobi(m, eigenValues, ortMat); | 
| 181 | 
< | 
            } | 
| 178 | 
> | 
    /** Return the transpose of this matrix */ | 
| 179 | 
> | 
    SquareMatrix<Real,  Dim> transpose() const{ | 
| 180 | 
> | 
      SquareMatrix<Real,  Dim> result; | 
| 181 | 
> | 
                 | 
| 182 | 
> | 
      for (unsigned int i = 0; i < Dim; i++) | 
| 183 | 
> | 
        for (unsigned int j = 0; j < Dim; j++)               | 
| 184 | 
> | 
          result(j, i) = this->data_[i][j]; | 
| 185 | 
  | 
 | 
| 186 | 
< | 
            /** | 
| 187 | 
< | 
             * Jacobi iteration routines for computing eigenvalues/eigenvectors of  | 
| 188 | 
< | 
             * real symmetric matrix | 
| 189 | 
< | 
             * | 
| 190 | 
< | 
             * @return true if success, otherwise return false | 
| 191 | 
< | 
             * @param a symmetric matrix whose eigenvectors are to be computed. On return, the matrix is | 
| 192 | 
< | 
             *     overwritten | 
| 193 | 
< | 
             * @param w will contain the eigenvalues of the matrix On return of this function | 
| 194 | 
< | 
             * @param v the columns of this matrix will contain the eigenvectors. The eigenvectors are  | 
| 195 | 
< | 
             *    normalized and mutually orthogonal.  | 
| 196 | 
< | 
             */ | 
| 186 | 
> | 
      return result; | 
| 187 | 
> | 
    } | 
| 188 | 
> | 
             | 
| 189 | 
> | 
    /** @todo need implementation */ | 
| 190 | 
> | 
    void diagonalize() { | 
| 191 | 
> | 
      //jacobi(m, eigenValues, ortMat); | 
| 192 | 
> | 
    } | 
| 193 | 
> | 
 | 
| 194 | 
> | 
    /** | 
| 195 | 
> | 
     * Jacobi iteration routines for computing eigenvalues/eigenvectors of  | 
| 196 | 
> | 
     * real symmetric matrix | 
| 197 | 
> | 
     * | 
| 198 | 
> | 
     * @return true if success, otherwise return false | 
| 199 | 
> | 
     * @param a symmetric matrix whose eigenvectors are to be computed. On return, the matrix is | 
| 200 | 
> | 
     *     overwritten | 
| 201 | 
> | 
     * @param w will contain the eigenvalues of the matrix On return of this function | 
| 202 | 
> | 
     * @param v the columns of this matrix will contain the eigenvectors. The eigenvectors are  | 
| 203 | 
> | 
     *    normalized and mutually orthogonal.  | 
| 204 | 
> | 
     */ | 
| 205 | 
  | 
            | 
| 206 | 
< | 
            static int jacobi(SquareMatrix<Real, Dim>& a, Vector<Real, Dim>& d,  | 
| 207 | 
< | 
                                  SquareMatrix<Real, Dim>& v); | 
| 208 | 
< | 
    };//end SquareMatrix | 
| 206 | 
> | 
    static int jacobi(SquareMatrix<Real, Dim>& a, Vector<Real, Dim>& d,  | 
| 207 | 
> | 
                      SquareMatrix<Real, Dim>& v); | 
| 208 | 
> | 
  };//end SquareMatrix | 
| 209 | 
  | 
 | 
| 210 | 
  | 
 | 
| 211 | 
< | 
/*========================================================================= | 
| 211 | 
> | 
  /*========================================================================= | 
| 212 | 
  | 
 | 
| 213 | 
  | 
  Program:   Visualization Toolkit | 
| 214 | 
  | 
  Module:    $RCSfile: SquareMatrix.hpp,v $ | 
| 217 | 
  | 
  All rights reserved. | 
| 218 | 
  | 
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details. | 
| 219 | 
  | 
 | 
| 220 | 
< | 
     This software is distributed WITHOUT ANY WARRANTY; without even | 
| 221 | 
< | 
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR | 
| 222 | 
< | 
     PURPOSE.  See the above copyright notice for more information. | 
| 220 | 
> | 
  This software is distributed WITHOUT ANY WARRANTY; without even | 
| 221 | 
> | 
  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR | 
| 222 | 
> | 
  PURPOSE.  See the above copyright notice for more information. | 
| 223 | 
  | 
 | 
| 224 | 
< | 
=========================================================================*/ | 
| 224 | 
> | 
  =========================================================================*/ | 
| 225 | 
  | 
 | 
| 226 | 
< | 
#define VTK_ROTATE(a,i,j,k,l) g=a(i, j);h=a(k, l);a(i, j)=g-s*(h+g*tau);\ | 
| 227 | 
< | 
        a(k, l)=h+s*(g-h*tau) | 
| 226 | 
> | 
#define VTK_ROTATE(a,i,j,k,l) g=a(i, j);h=a(k, l);a(i, j)=g-s*(h+g*tau); \ | 
| 227 | 
> | 
    a(k, l)=h+s*(g-h*tau) | 
| 228 | 
  | 
 | 
| 229 | 
  | 
#define VTK_MAX_ROTATIONS 20 | 
| 230 | 
  | 
 | 
| 231 | 
< | 
    // Jacobi iteration for the solution of eigenvectors/eigenvalues of a nxn | 
| 232 | 
< | 
    // real symmetric matrix. Square nxn matrix a; size of matrix in n; | 
| 233 | 
< | 
    // output eigenvalues in w; and output eigenvectors in v. Resulting | 
| 234 | 
< | 
    // eigenvalues/vectors are sorted in decreasing order; eigenvectors are | 
| 235 | 
< | 
    // normalized. | 
| 236 | 
< | 
    template<typename Real, int Dim> | 
| 237 | 
< | 
    int SquareMatrix<Real, Dim>::jacobi(SquareMatrix<Real, Dim>& a, Vector<Real, Dim>& w,  | 
| 238 | 
< | 
                                        SquareMatrix<Real, Dim>& v) { | 
| 239 | 
< | 
        const int n = Dim;   | 
| 240 | 
< | 
        int i, j, k, iq, ip, numPos; | 
| 241 | 
< | 
        Real tresh, theta, tau, t, sm, s, h, g, c, tmp; | 
| 242 | 
< | 
        Real bspace[4], zspace[4]; | 
| 243 | 
< | 
        Real *b = bspace; | 
| 244 | 
< | 
        Real *z = zspace; | 
| 234 | 
< | 
 | 
| 235 | 
< | 
        // only allocate memory if the matrix is large | 
| 236 | 
< | 
        if (n > 4) { | 
| 237 | 
< | 
            b = new Real[n]; | 
| 238 | 
< | 
            z = new Real[n];  | 
| 239 | 
< | 
        } | 
| 231 | 
> | 
  // Jacobi iteration for the solution of eigenvectors/eigenvalues of a nxn | 
| 232 | 
> | 
  // real symmetric matrix. Square nxn matrix a; size of matrix in n; | 
| 233 | 
> | 
  // output eigenvalues in w; and output eigenvectors in v. Resulting | 
| 234 | 
> | 
  // eigenvalues/vectors are sorted in decreasing order; eigenvectors are | 
| 235 | 
> | 
  // normalized. | 
| 236 | 
> | 
  template<typename Real, int Dim> | 
| 237 | 
> | 
  int SquareMatrix<Real, Dim>::jacobi(SquareMatrix<Real, Dim>& a, Vector<Real, Dim>& w,  | 
| 238 | 
> | 
                                      SquareMatrix<Real, Dim>& v) { | 
| 239 | 
> | 
    const int n = Dim;   | 
| 240 | 
> | 
    int i, j, k, iq, ip, numPos; | 
| 241 | 
> | 
    Real tresh, theta, tau, t, sm, s, h, g, c, tmp; | 
| 242 | 
> | 
    Real bspace[4], zspace[4]; | 
| 243 | 
> | 
    Real *b = bspace; | 
| 244 | 
> | 
    Real *z = zspace; | 
| 245 | 
  | 
 | 
| 246 | 
< | 
        // initialize | 
| 247 | 
< | 
        for (ip=0; ip<n; ip++) { | 
| 248 | 
< | 
            for (iq=0; iq<n; iq++) { | 
| 249 | 
< | 
                v(ip, iq) = 0.0; | 
| 250 | 
< | 
            } | 
| 246 | 
< | 
            v(ip, ip) = 1.0; | 
| 247 | 
< | 
        } | 
| 248 | 
< | 
        for (ip=0; ip<n; ip++) { | 
| 249 | 
< | 
            b[ip] = w[ip] = a(ip, ip); | 
| 250 | 
< | 
            z[ip] = 0.0; | 
| 251 | 
< | 
        } | 
| 246 | 
> | 
    // only allocate memory if the matrix is large | 
| 247 | 
> | 
    if (n > 4) { | 
| 248 | 
> | 
      b = new Real[n]; | 
| 249 | 
> | 
      z = new Real[n];  | 
| 250 | 
> | 
    } | 
| 251 | 
  | 
 | 
| 252 | 
< | 
        // begin rotation sequence | 
| 253 | 
< | 
        for (i=0; i<VTK_MAX_ROTATIONS; i++) { | 
| 254 | 
< | 
            sm = 0.0; | 
| 255 | 
< | 
            for (ip=0; ip<n-1; ip++) { | 
| 256 | 
< | 
                for (iq=ip+1; iq<n; iq++) { | 
| 257 | 
< | 
                    sm += fabs(a(ip, iq)); | 
| 258 | 
< | 
                } | 
| 259 | 
< | 
            } | 
| 260 | 
< | 
            if (sm == 0.0) { | 
| 261 | 
< | 
                break; | 
| 262 | 
< | 
            } | 
| 252 | 
> | 
    // initialize | 
| 253 | 
> | 
    for (ip=0; ip<n; ip++) { | 
| 254 | 
> | 
      for (iq=0; iq<n; iq++) { | 
| 255 | 
> | 
        v(ip, iq) = 0.0; | 
| 256 | 
> | 
      } | 
| 257 | 
> | 
      v(ip, ip) = 1.0; | 
| 258 | 
> | 
    } | 
| 259 | 
> | 
    for (ip=0; ip<n; ip++) { | 
| 260 | 
> | 
      b[ip] = w[ip] = a(ip, ip); | 
| 261 | 
> | 
      z[ip] = 0.0; | 
| 262 | 
> | 
    } | 
| 263 | 
  | 
 | 
| 264 | 
< | 
            if (i < 3) {                                // first 3 sweeps | 
| 265 | 
< | 
                tresh = 0.2*sm/(n*n); | 
| 266 | 
< | 
            } else { | 
| 267 | 
< | 
                tresh = 0.0; | 
| 268 | 
< | 
            } | 
| 264 | 
> | 
    // begin rotation sequence | 
| 265 | 
> | 
    for (i=0; i<VTK_MAX_ROTATIONS; i++) { | 
| 266 | 
> | 
      sm = 0.0; | 
| 267 | 
> | 
      for (ip=0; ip<n-1; ip++) { | 
| 268 | 
> | 
        for (iq=ip+1; iq<n; iq++) { | 
| 269 | 
> | 
          sm += fabs(a(ip, iq)); | 
| 270 | 
> | 
        } | 
| 271 | 
> | 
      } | 
| 272 | 
> | 
      if (sm == 0.0) { | 
| 273 | 
> | 
        break; | 
| 274 | 
> | 
      } | 
| 275 | 
  | 
 | 
| 276 | 
< | 
            for (ip=0; ip<n-1; ip++) { | 
| 277 | 
< | 
                for (iq=ip+1; iq<n; iq++) { | 
| 278 | 
< | 
                    g = 100.0*fabs(a(ip, iq)); | 
| 276 | 
> | 
      if (i < 3) {                                // first 3 sweeps | 
| 277 | 
> | 
        tresh = 0.2*sm/(n*n); | 
| 278 | 
> | 
      } else { | 
| 279 | 
> | 
        tresh = 0.0; | 
| 280 | 
> | 
      } | 
| 281 | 
  | 
 | 
| 282 | 
< | 
                    // after 4 sweeps | 
| 283 | 
< | 
                    if (i > 3 && (fabs(w[ip])+g) == fabs(w[ip]) | 
| 284 | 
< | 
                        && (fabs(w[iq])+g) == fabs(w[iq])) { | 
| 278 | 
< | 
                        a(ip, iq) = 0.0; | 
| 279 | 
< | 
                    } else if (fabs(a(ip, iq)) > tresh) { | 
| 280 | 
< | 
                        h = w[iq] - w[ip]; | 
| 281 | 
< | 
                        if ( (fabs(h)+g) == fabs(h)) { | 
| 282 | 
< | 
                            t = (a(ip, iq)) / h; | 
| 283 | 
< | 
                        } else { | 
| 284 | 
< | 
                            theta = 0.5*h / (a(ip, iq)); | 
| 285 | 
< | 
                            t = 1.0 / (fabs(theta)+sqrt(1.0+theta*theta)); | 
| 286 | 
< | 
                            if (theta < 0.0) { | 
| 287 | 
< | 
                                t = -t; | 
| 288 | 
< | 
                            } | 
| 289 | 
< | 
                        } | 
| 290 | 
< | 
                        c = 1.0 / sqrt(1+t*t); | 
| 291 | 
< | 
                        s = t*c; | 
| 292 | 
< | 
                        tau = s/(1.0+c); | 
| 293 | 
< | 
                        h = t*a(ip, iq); | 
| 294 | 
< | 
                        z[ip] -= h; | 
| 295 | 
< | 
                        z[iq] += h; | 
| 296 | 
< | 
                        w[ip] -= h; | 
| 297 | 
< | 
                        w[iq] += h; | 
| 298 | 
< | 
                        a(ip, iq)=0.0; | 
| 282 | 
> | 
      for (ip=0; ip<n-1; ip++) { | 
| 283 | 
> | 
        for (iq=ip+1; iq<n; iq++) { | 
| 284 | 
> | 
          g = 100.0*fabs(a(ip, iq)); | 
| 285 | 
  | 
 | 
| 286 | 
< | 
                        // ip already shifted left by 1 unit | 
| 287 | 
< | 
                        for (j = 0;j <= ip-1;j++) { | 
| 288 | 
< | 
                            VTK_ROTATE(a,j,ip,j,iq); | 
| 289 | 
< | 
                        } | 
| 290 | 
< | 
                        // ip and iq already shifted left by 1 unit | 
| 291 | 
< | 
                        for (j = ip+1;j <= iq-1;j++) { | 
| 292 | 
< | 
                            VTK_ROTATE(a,ip,j,j,iq); | 
| 293 | 
< | 
                        } | 
| 294 | 
< | 
                        // iq already shifted left by 1 unit | 
| 295 | 
< | 
                        for (j=iq+1; j<n; j++) { | 
| 296 | 
< | 
                            VTK_ROTATE(a,ip,j,iq,j); | 
| 297 | 
< | 
                        } | 
| 298 | 
< | 
                        for (j=0; j<n; j++) { | 
| 299 | 
< | 
                            VTK_ROTATE(v,j,ip,j,iq); | 
| 300 | 
< | 
                        } | 
| 301 | 
< | 
                    } | 
| 302 | 
< | 
                } | 
| 303 | 
< | 
            } | 
| 286 | 
> | 
          // after 4 sweeps | 
| 287 | 
> | 
          if (i > 3 && (fabs(w[ip])+g) == fabs(w[ip]) | 
| 288 | 
> | 
              && (fabs(w[iq])+g) == fabs(w[iq])) { | 
| 289 | 
> | 
            a(ip, iq) = 0.0; | 
| 290 | 
> | 
          } else if (fabs(a(ip, iq)) > tresh) { | 
| 291 | 
> | 
            h = w[iq] - w[ip]; | 
| 292 | 
> | 
            if ( (fabs(h)+g) == fabs(h)) { | 
| 293 | 
> | 
              t = (a(ip, iq)) / h; | 
| 294 | 
> | 
            } else { | 
| 295 | 
> | 
              theta = 0.5*h / (a(ip, iq)); | 
| 296 | 
> | 
              t = 1.0 / (fabs(theta)+sqrt(1.0+theta*theta)); | 
| 297 | 
> | 
              if (theta < 0.0) { | 
| 298 | 
> | 
                t = -t; | 
| 299 | 
> | 
              } | 
| 300 | 
> | 
            } | 
| 301 | 
> | 
            c = 1.0 / sqrt(1+t*t); | 
| 302 | 
> | 
            s = t*c; | 
| 303 | 
> | 
            tau = s/(1.0+c); | 
| 304 | 
> | 
            h = t*a(ip, iq); | 
| 305 | 
> | 
            z[ip] -= h; | 
| 306 | 
> | 
            z[iq] += h; | 
| 307 | 
> | 
            w[ip] -= h; | 
| 308 | 
> | 
            w[iq] += h; | 
| 309 | 
> | 
            a(ip, iq)=0.0; | 
| 310 | 
  | 
 | 
| 311 | 
< | 
            for (ip=0; ip<n; ip++) { | 
| 312 | 
< | 
                b[ip] += z[ip]; | 
| 313 | 
< | 
                w[ip] = b[ip]; | 
| 314 | 
< | 
                z[ip] = 0.0; | 
| 315 | 
< | 
            } | 
| 316 | 
< | 
        } | 
| 311 | 
> | 
            // ip already shifted left by 1 unit | 
| 312 | 
> | 
            for (j = 0;j <= ip-1;j++) { | 
| 313 | 
> | 
              VTK_ROTATE(a,j,ip,j,iq); | 
| 314 | 
> | 
            } | 
| 315 | 
> | 
            // ip and iq already shifted left by 1 unit | 
| 316 | 
> | 
            for (j = ip+1;j <= iq-1;j++) { | 
| 317 | 
> | 
              VTK_ROTATE(a,ip,j,j,iq); | 
| 318 | 
> | 
            } | 
| 319 | 
> | 
            // iq already shifted left by 1 unit | 
| 320 | 
> | 
            for (j=iq+1; j<n; j++) { | 
| 321 | 
> | 
              VTK_ROTATE(a,ip,j,iq,j); | 
| 322 | 
> | 
            } | 
| 323 | 
> | 
            for (j=0; j<n; j++) { | 
| 324 | 
> | 
              VTK_ROTATE(v,j,ip,j,iq); | 
| 325 | 
> | 
            } | 
| 326 | 
> | 
          } | 
| 327 | 
> | 
        } | 
| 328 | 
> | 
      } | 
| 329 | 
  | 
 | 
| 330 | 
< | 
        //// this is NEVER called | 
| 331 | 
< | 
        if ( i >= VTK_MAX_ROTATIONS ) { | 
| 332 | 
< | 
            std::cout << "vtkMath::Jacobi: Error extracting eigenfunctions" << std::endl; | 
| 333 | 
< | 
            return 0; | 
| 334 | 
< | 
        } | 
| 330 | 
> | 
      for (ip=0; ip<n; ip++) { | 
| 331 | 
> | 
        b[ip] += z[ip]; | 
| 332 | 
> | 
        w[ip] = b[ip]; | 
| 333 | 
> | 
        z[ip] = 0.0; | 
| 334 | 
> | 
      } | 
| 335 | 
> | 
    } | 
| 336 | 
  | 
 | 
| 337 | 
< | 
        // sort eigenfunctions                 these changes do not affect accuracy  | 
| 338 | 
< | 
        for (j=0; j<n-1; j++) {                  // boundary incorrect | 
| 339 | 
< | 
            k = j; | 
| 340 | 
< | 
            tmp = w[k]; | 
| 341 | 
< | 
            for (i=j+1; i<n; i++) {                // boundary incorrect, shifted already | 
| 337 | 
< | 
                if (w[i] >= tmp) {                   // why exchage if same? | 
| 338 | 
< | 
                    k = i; | 
| 339 | 
< | 
                    tmp = w[k]; | 
| 340 | 
< | 
                } | 
| 341 | 
< | 
            } | 
| 342 | 
< | 
            if (k != j) { | 
| 343 | 
< | 
                w[k] = w[j]; | 
| 344 | 
< | 
                w[j] = tmp; | 
| 345 | 
< | 
                for (i=0; i<n; i++) { | 
| 346 | 
< | 
                    tmp = v(i, j); | 
| 347 | 
< | 
                    v(i, j) = v(i, k); | 
| 348 | 
< | 
                    v(i, k) = tmp; | 
| 349 | 
< | 
                } | 
| 350 | 
< | 
            } | 
| 351 | 
< | 
        } | 
| 352 | 
< | 
        // insure eigenvector consistency (i.e., Jacobi can compute vectors that | 
| 353 | 
< | 
        // are negative of one another (.707,.707,0) and (-.707,-.707,0). This can | 
| 354 | 
< | 
        // reek havoc in hyperstreamline/other stuff. We will select the most | 
| 355 | 
< | 
        // positive eigenvector. | 
| 356 | 
< | 
        int ceil_half_n = (n >> 1) + (n & 1); | 
| 357 | 
< | 
        for (j=0; j<n; j++) { | 
| 358 | 
< | 
            for (numPos=0, i=0; i<n; i++) { | 
| 359 | 
< | 
                if ( v(i, j) >= 0.0 ) { | 
| 360 | 
< | 
                    numPos++; | 
| 361 | 
< | 
                } | 
| 362 | 
< | 
            } | 
| 363 | 
< | 
            //    if ( numPos < ceil(double(n)/double(2.0)) ) | 
| 364 | 
< | 
            if ( numPos < ceil_half_n) { | 
| 365 | 
< | 
                for (i=0; i<n; i++) { | 
| 366 | 
< | 
                    v(i, j) *= -1.0; | 
| 367 | 
< | 
                } | 
| 368 | 
< | 
            } | 
| 369 | 
< | 
        } | 
| 337 | 
> | 
    //// this is NEVER called | 
| 338 | 
> | 
    if ( i >= VTK_MAX_ROTATIONS ) { | 
| 339 | 
> | 
      std::cout << "vtkMath::Jacobi: Error extracting eigenfunctions" << std::endl; | 
| 340 | 
> | 
      return 0; | 
| 341 | 
> | 
    } | 
| 342 | 
  | 
 | 
| 343 | 
< | 
        if (n > 4) { | 
| 344 | 
< | 
            delete [] b; | 
| 345 | 
< | 
            delete [] z; | 
| 346 | 
< | 
        } | 
| 347 | 
< | 
        return 1; | 
| 343 | 
> | 
    // sort eigenfunctions                 these changes do not affect accuracy  | 
| 344 | 
> | 
    for (j=0; j<n-1; j++) {                  // boundary incorrect | 
| 345 | 
> | 
      k = j; | 
| 346 | 
> | 
      tmp = w[k]; | 
| 347 | 
> | 
      for (i=j+1; i<n; i++) {                // boundary incorrect, shifted already | 
| 348 | 
> | 
        if (w[i] >= tmp) {                   // why exchage if same? | 
| 349 | 
> | 
          k = i; | 
| 350 | 
> | 
          tmp = w[k]; | 
| 351 | 
> | 
        } | 
| 352 | 
> | 
      } | 
| 353 | 
> | 
      if (k != j) { | 
| 354 | 
> | 
        w[k] = w[j]; | 
| 355 | 
> | 
        w[j] = tmp; | 
| 356 | 
> | 
        for (i=0; i<n; i++) { | 
| 357 | 
> | 
          tmp = v(i, j); | 
| 358 | 
> | 
          v(i, j) = v(i, k); | 
| 359 | 
> | 
          v(i, k) = tmp; | 
| 360 | 
> | 
        } | 
| 361 | 
> | 
      } | 
| 362 | 
  | 
    } | 
| 363 | 
+ | 
    // insure eigenvector consistency (i.e., Jacobi can compute vectors that | 
| 364 | 
+ | 
    // are negative of one another (.707,.707,0) and (-.707,-.707,0). This can | 
| 365 | 
+ | 
    // reek havoc in hyperstreamline/other stuff. We will select the most | 
| 366 | 
+ | 
    // positive eigenvector. | 
| 367 | 
+ | 
    int ceil_half_n = (n >> 1) + (n & 1); | 
| 368 | 
+ | 
    for (j=0; j<n; j++) { | 
| 369 | 
+ | 
      for (numPos=0, i=0; i<n; i++) { | 
| 370 | 
+ | 
        if ( v(i, j) >= 0.0 ) { | 
| 371 | 
+ | 
          numPos++; | 
| 372 | 
+ | 
        } | 
| 373 | 
+ | 
      } | 
| 374 | 
+ | 
      //    if ( numPos < ceil(double(n)/double(2.0)) ) | 
| 375 | 
+ | 
      if ( numPos < ceil_half_n) { | 
| 376 | 
+ | 
        for (i=0; i<n; i++) { | 
| 377 | 
+ | 
          v(i, j) *= -1.0; | 
| 378 | 
+ | 
        } | 
| 379 | 
+ | 
      } | 
| 380 | 
+ | 
    } | 
| 381 | 
  | 
 | 
| 382 | 
+ | 
    if (n > 4) { | 
| 383 | 
+ | 
      delete [] b; | 
| 384 | 
+ | 
      delete [] z; | 
| 385 | 
+ | 
    } | 
| 386 | 
+ | 
    return 1; | 
| 387 | 
+ | 
  } | 
| 388 | 
  | 
 | 
| 389 | 
+ | 
 | 
| 390 | 
+ | 
  typedef SquareMatrix<double, 6> Mat6x6d; | 
| 391 | 
  | 
} | 
| 392 | 
  | 
#endif //MATH_SQUAREMATRIX_HPP  | 
| 393 | 
  | 
 |