| 1 |  | /* | 
| 2 | < | * Copyright (C) 2000-2004  Object Oriented Parallel Simulation Engine (OOPSE) project | 
| 3 | < | * | 
| 4 | < | * Contact: oopse@oopse.org | 
| 5 | < | * | 
| 6 | < | * This program is free software; you can redistribute it and/or | 
| 7 | < | * modify it under the terms of the GNU Lesser General Public License | 
| 8 | < | * as published by the Free Software Foundation; either version 2.1 | 
| 9 | < | * of the License, or (at your option) any later version. | 
| 10 | < | * All we ask is that proper credit is given for our work, which includes | 
| 11 | < | * - but is not limited to - adding the above copyright notice to the beginning | 
| 12 | < | * of your source code files, and to any copyright notice that you may distribute | 
| 13 | < | * with programs based on this work. | 
| 14 | < | * | 
| 15 | < | * This program is distributed in the hope that it will be useful, | 
| 16 | < | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
| 17 | < | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
| 18 | < | * GNU Lesser General Public License for more details. | 
| 19 | < | * | 
| 20 | < | * You should have received a copy of the GNU Lesser General Public License | 
| 21 | < | * along with this program; if not, write to the Free Software | 
| 22 | < | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. | 
| 2 | > | * Copyright (c) 2005 The University of Notre Dame. All Rights Reserved. | 
| 3 |  | * | 
| 4 | + | * The University of Notre Dame grants you ("Licensee") a | 
| 5 | + | * non-exclusive, royalty free, license to use, modify and | 
| 6 | + | * redistribute this software in source and binary code form, provided | 
| 7 | + | * that the following conditions are met: | 
| 8 | + | * | 
| 9 | + | * 1. Acknowledgement of the program authors must be made in any | 
| 10 | + | *    publication of scientific results based in part on use of the | 
| 11 | + | *    program.  An acceptable form of acknowledgement is citation of | 
| 12 | + | *    the article in which the program was described (Matthew | 
| 13 | + | *    A. Meineke, Charles F. Vardeman II, Teng Lin, Christopher | 
| 14 | + | *    J. Fennell and J. Daniel Gezelter, "OOPSE: An Object-Oriented | 
| 15 | + | *    Parallel Simulation Engine for Molecular Dynamics," | 
| 16 | + | *    J. Comput. Chem. 26, pp. 252-271 (2005)) | 
| 17 | + | * | 
| 18 | + | * 2. Redistributions of source code must retain the above copyright | 
| 19 | + | *    notice, this list of conditions and the following disclaimer. | 
| 20 | + | * | 
| 21 | + | * 3. Redistributions in binary form must reproduce the above copyright | 
| 22 | + | *    notice, this list of conditions and the following disclaimer in the | 
| 23 | + | *    documentation and/or other materials provided with the | 
| 24 | + | *    distribution. | 
| 25 | + | * | 
| 26 | + | * This software is provided "AS IS," without a warranty of any | 
| 27 | + | * kind. All express or implied conditions, representations and | 
| 28 | + | * warranties, including any implied warranty of merchantability, | 
| 29 | + | * fitness for a particular purpose or non-infringement, are hereby | 
| 30 | + | * excluded.  The University of Notre Dame and its licensors shall not | 
| 31 | + | * be liable for any damages suffered by licensee as a result of | 
| 32 | + | * using, modifying or distributing the software or its | 
| 33 | + | * derivatives. In no event will the University of Notre Dame or its | 
| 34 | + | * licensors be liable for any lost revenue, profit or data, or for | 
| 35 | + | * direct, indirect, special, consequential, incidental or punitive | 
| 36 | + | * damages, however caused and regardless of the theory of liability, | 
| 37 | + | * arising out of the use of or inability to use software, even if the | 
| 38 | + | * University of Notre Dame has been advised of the possibility of | 
| 39 | + | * such damages. | 
| 40 |  | */ | 
| 41 | < |  | 
| 41 | > |  | 
| 42 |  | /** | 
| 43 |  | * @file SquareMatrix.hpp | 
| 44 |  | * @author Teng Lin | 
| 49 |  | #define MATH_SQUAREMATRIX_HPP | 
| 50 |  |  | 
| 51 |  | #include "math/RectMatrix.hpp" | 
| 52 | + | #include "utils/NumericConstant.hpp" | 
| 53 |  |  | 
| 54 |  | namespace oopse { | 
| 55 |  |  | 
| 56 | < | /** | 
| 57 | < | * @class SquareMatrix SquareMatrix.hpp "math/SquareMatrix.hpp" | 
| 58 | < | * @brief A square matrix class | 
| 59 | < | * @template Real the element type | 
| 60 | < | * @template Dim the dimension of the square matrix | 
| 61 | < | */ | 
| 62 | < | template<typename Real, int Dim> | 
| 63 | < | class SquareMatrix : public RectMatrix<Real, Dim, Dim> { | 
| 64 | < | public: | 
| 56 | > | /** | 
| 57 | > | * @class SquareMatrix SquareMatrix.hpp "math/SquareMatrix.hpp" | 
| 58 | > | * @brief A square matrix class | 
| 59 | > | * @template Real the element type | 
| 60 | > | * @template Dim the dimension of the square matrix | 
| 61 | > | */ | 
| 62 | > | template<typename Real, int Dim> | 
| 63 | > | class SquareMatrix : public RectMatrix<Real, Dim, Dim> { | 
| 64 | > | public: | 
| 65 | > | typedef Real ElemType; | 
| 66 | > | typedef Real* ElemPoinerType; | 
| 67 |  |  | 
| 68 | < | /** default constructor */ | 
| 69 | < | SquareMatrix() { | 
| 70 | < | for (unsigned int i = 0; i < Dim; i++) | 
| 71 | < | for (unsigned int j = 0; j < Dim; j++) | 
| 72 | < | data_[i][j] = 0.0; | 
| 73 | < | } | 
| 68 | > | /** default constructor */ | 
| 69 | > | SquareMatrix() { | 
| 70 | > | for (unsigned int i = 0; i < Dim; i++) | 
| 71 | > | for (unsigned int j = 0; j < Dim; j++) | 
| 72 | > | this->data_[i][j] = 0.0; | 
| 73 | > | } | 
| 74 |  |  | 
| 75 | < | /** copy constructor */ | 
| 76 | < | SquareMatrix(const RectMatrix<Real, Dim, Dim>& m)  : RectMatrix<Real, Dim, Dim>(m) { | 
| 77 | < | } | 
| 59 | < |  | 
| 60 | < | /** copy assignment operator */ | 
| 61 | < | SquareMatrix<Real, Dim>& operator =(const RectMatrix<Real, Dim, Dim>& m) { | 
| 62 | < | RectMatrix<Real, Dim, Dim>::operator=(m); | 
| 63 | < | return *this; | 
| 64 | < | } | 
| 65 | < |  | 
| 66 | < | /** Retunrs  an identity matrix*/ | 
| 75 | > | /** Constructs and initializes every element of this matrix to a scalar */ | 
| 76 | > | SquareMatrix(Real s) : RectMatrix<Real, Dim, Dim>(s){ | 
| 77 | > | } | 
| 78 |  |  | 
| 79 | < | static SquareMatrix<Real, Dim> identity() { | 
| 80 | < | SquareMatrix<Real, Dim> m; | 
| 79 | > | /** Constructs and initializes from an array */ | 
| 80 | > | SquareMatrix(Real* array) : RectMatrix<Real, Dim, Dim>(array){ | 
| 81 | > | } | 
| 82 | > |  | 
| 83 | > |  | 
| 84 | > | /** copy constructor */ | 
| 85 | > | SquareMatrix(const RectMatrix<Real, Dim, Dim>& m) : RectMatrix<Real, Dim, Dim>(m) { | 
| 86 | > | } | 
| 87 |  |  | 
| 88 | < | for (unsigned int i = 0; i < Dim; i++) | 
| 89 | < | for (unsigned int j = 0; j < Dim; j++) | 
| 90 | < | if (i == j) | 
| 91 | < | m(i, j) = 1.0; | 
| 92 | < | else | 
| 93 | < | m(i, j) = 0.0; | 
| 88 | > | /** copy assignment operator */ | 
| 89 | > | SquareMatrix<Real, Dim>& operator =(const RectMatrix<Real, Dim, Dim>& m) { | 
| 90 | > | RectMatrix<Real, Dim, Dim>::operator=(m); | 
| 91 | > | return *this; | 
| 92 | > | } | 
| 93 | > |  | 
| 94 | > | /** Retunrs  an identity matrix*/ | 
| 95 |  |  | 
| 96 | < | return m; | 
| 97 | < | } | 
| 96 | > | static SquareMatrix<Real, Dim> identity() { | 
| 97 | > | SquareMatrix<Real, Dim> m; | 
| 98 | > |  | 
| 99 | > | for (unsigned int i = 0; i < Dim; i++) | 
| 100 | > | for (unsigned int j = 0; j < Dim; j++) | 
| 101 | > | if (i == j) | 
| 102 | > | m(i, j) = 1.0; | 
| 103 | > | else | 
| 104 | > | m(i, j) = 0.0; | 
| 105 |  |  | 
| 106 | < | /** Retunrs  the inversion of this matrix. */ | 
| 107 | < | SquareMatrix<Real, Dim>  inverse() { | 
| 83 | < | SquareMatrix<Real, Dim> result; | 
| 106 | > | return m; | 
| 107 | > | } | 
| 108 |  |  | 
| 109 | < | return result; | 
| 110 | < | } | 
| 109 | > | /** | 
| 110 | > | * Retunrs  the inversion of this matrix. | 
| 111 | > | * @todo need implementation | 
| 112 | > | */ | 
| 113 | > | SquareMatrix<Real, Dim>  inverse() { | 
| 114 | > | SquareMatrix<Real, Dim> result; | 
| 115 |  |  | 
| 116 | < | /** Returns the determinant of this matrix. */ | 
| 117 | < | double determinant() const { | 
| 90 | < | double det; | 
| 91 | < | return det; | 
| 92 | < | } | 
| 116 | > | return result; | 
| 117 | > | } | 
| 118 |  |  | 
| 119 | < | /** Returns the trace of this matrix. */ | 
| 120 | < | double trace() const { | 
| 121 | < | double tmp = 0; | 
| 122 | < |  | 
| 123 | < | for (unsigned int i = 0; i < Dim ; i++) | 
| 124 | < | tmp += data_[i][i]; | 
| 119 | > | /** | 
| 120 | > | * Returns the determinant of this matrix. | 
| 121 | > | * @todo need implementation | 
| 122 | > | */ | 
| 123 | > | Real determinant() const { | 
| 124 | > | Real det; | 
| 125 | > | return det; | 
| 126 | > | } | 
| 127 |  |  | 
| 128 | < | return tmp; | 
| 129 | < | } | 
| 128 | > | /** Returns the trace of this matrix. */ | 
| 129 | > | Real trace() const { | 
| 130 | > | Real tmp = 0; | 
| 131 | > |  | 
| 132 | > | for (unsigned int i = 0; i < Dim ; i++) | 
| 133 | > | tmp += this->data_[i][i]; | 
| 134 |  |  | 
| 135 | < | /** Tests if this matrix is symmetrix. */ | 
| 136 | < | bool isSymmetric() const { | 
| 106 | < | for (unsigned int i = 0; i < Dim - 1; i++) | 
| 107 | < | for (unsigned int j = i; j < Dim; j++) | 
| 108 | < | if (fabs(data_[i][j] - data_[j][i]) > oopse::epsilon) | 
| 109 | < | return false; | 
| 110 | < |  | 
| 111 | < | return true; | 
| 112 | < | } | 
| 135 | > | return tmp; | 
| 136 | > | } | 
| 137 |  |  | 
| 138 | < | /** Tests if this matrix is orthogonal. */ | 
| 139 | < | bool isOrthogonal() { | 
| 140 | < | SquareMatrix<Real, Dim> tmp; | 
| 138 | > | /** Tests if this matrix is symmetrix. */ | 
| 139 | > | bool isSymmetric() const { | 
| 140 | > | for (unsigned int i = 0; i < Dim - 1; i++) | 
| 141 | > | for (unsigned int j = i; j < Dim; j++) | 
| 142 | > | if (fabs(this->data_[i][j] - this->data_[j][i]) > epsilon) | 
| 143 | > | return false; | 
| 144 | > |  | 
| 145 | > | return true; | 
| 146 | > | } | 
| 147 |  |  | 
| 148 | < | tmp = *this * transpose(); | 
| 148 | > | /** Tests if this matrix is orthogonal. */ | 
| 149 | > | bool isOrthogonal() { | 
| 150 | > | SquareMatrix<Real, Dim> tmp; | 
| 151 |  |  | 
| 152 | < | return tmp.isDiagonal(); | 
| 121 | < | } | 
| 152 | > | tmp = *this * transpose(); | 
| 153 |  |  | 
| 154 | < | /** Tests if this matrix is diagonal. */ | 
| 155 | < | bool isDiagonal() const { | 
| 156 | < | for (unsigned int i = 0; i < Dim ; i++) | 
| 157 | < | for (unsigned int j = 0; j < Dim; j++) | 
| 158 | < | if (i !=j && fabs(data_[i][j]) > oopse::epsilon) | 
| 159 | < | return false; | 
| 154 | > | return tmp.isDiagonal(); | 
| 155 | > | } | 
| 156 | > |  | 
| 157 | > | /** Tests if this matrix is diagonal. */ | 
| 158 | > | bool isDiagonal() const { | 
| 159 | > | for (unsigned int i = 0; i < Dim ; i++) | 
| 160 | > | for (unsigned int j = 0; j < Dim; j++) | 
| 161 | > | if (i !=j && fabs(this->data_[i][j]) > epsilon) | 
| 162 | > | return false; | 
| 163 | > |  | 
| 164 | > | return true; | 
| 165 | > | } | 
| 166 | > |  | 
| 167 | > | /** Tests if this matrix is the unit matrix. */ | 
| 168 | > | bool isUnitMatrix() const { | 
| 169 | > | if (!isDiagonal()) | 
| 170 | > | return false; | 
| 171 | > |  | 
| 172 | > | for (unsigned int i = 0; i < Dim ; i++) | 
| 173 | > | if (fabs(this->data_[i][i] - 1) > epsilon) | 
| 174 | > | return false; | 
| 175 |  |  | 
| 176 | < | return true; | 
| 177 | < | } | 
| 176 | > | return true; | 
| 177 | > | } | 
| 178 |  |  | 
| 179 | < | /** Tests if this matrix is the unit matrix. */ | 
| 180 | < | bool isUnitMatrix() const { | 
| 181 | < | if (!isDiagonal()) | 
| 136 | < | return false; | 
| 137 | < |  | 
| 138 | < | for (unsigned int i = 0; i < Dim ; i++) | 
| 139 | < | if (fabs(data_[i][i] - 1) > oopse::epsilon) | 
| 140 | < | return false; | 
| 179 | > | /** Return the transpose of this matrix */ | 
| 180 | > | SquareMatrix<Real,  Dim> transpose() const{ | 
| 181 | > | SquareMatrix<Real,  Dim> result; | 
| 182 |  |  | 
| 183 | < | return true; | 
| 184 | < | } | 
| 183 | > | for (unsigned int i = 0; i < Dim; i++) | 
| 184 | > | for (unsigned int j = 0; j < Dim; j++) | 
| 185 | > | result(j, i) = this->data_[i][j]; | 
| 186 |  |  | 
| 187 | < | void diagonalize() { | 
| 188 | < | jacobi(m, eigenValues, ortMat); | 
| 147 | < | } | 
| 148 | < |  | 
| 149 | < | /** | 
| 150 | < | * Finds the eigenvalues and eigenvectors of a symmetric matrix | 
| 151 | < | * @param eigenvals a reference to a vector3 where the | 
| 152 | < | * eigenvalues will be stored. The eigenvalues are ordered so | 
| 153 | < | * that eigenvals[0] <= eigenvals[1] <= eigenvals[2]. | 
| 154 | < | * @return an orthogonal matrix whose ith column is an | 
| 155 | < | * eigenvector for the eigenvalue eigenvals[i] | 
| 156 | < | */ | 
| 157 | < | SquareMatrix<Real, Dim>  findEigenvectors(Vector<Real, Dim>& eigenValues) { | 
| 158 | < | SquareMatrix<Real, Dim> ortMat; | 
| 187 | > | return result; | 
| 188 | > | } | 
| 189 |  |  | 
| 190 | < | if ( !isSymmetric()){ | 
| 191 | < | throw(); | 
| 192 | < | } | 
| 193 | < |  | 
| 164 | < | SquareMatrix<Real, Dim> m(*this); | 
| 165 | < | jacobi(m, eigenValues, ortMat); | 
| 190 | > | /** @todo need implementation */ | 
| 191 | > | void diagonalize() { | 
| 192 | > | //jacobi(m, eigenValues, ortMat); | 
| 193 | > | } | 
| 194 |  |  | 
| 195 | < | return ortMat; | 
| 196 | < | } | 
| 197 | < | /** | 
| 198 | < | * Jacobi iteration routines for computing eigenvalues/eigenvectors of | 
| 199 | < | * real symmetric matrix | 
| 200 | < | * | 
| 201 | < | * @return true if success, otherwise return false | 
| 202 | < | * @param a source matrix | 
| 203 | < | * @param w output eigenvalues | 
| 204 | < | * @param v output eigenvectors | 
| 205 | < | */ | 
| 206 | < | void jacobi(const SquareMatrix<Real, Dim>& a, | 
| 207 | < | Vector<Real, Dim>& w, | 
| 208 | < | SquareMatrix<Real, Dim>& v); | 
| 209 | < | };//end SquareMatrix | 
| 195 | > | /** | 
| 196 | > | * Jacobi iteration routines for computing eigenvalues/eigenvectors of | 
| 197 | > | * real symmetric matrix | 
| 198 | > | * | 
| 199 | > | * @return true if success, otherwise return false | 
| 200 | > | * @param a symmetric matrix whose eigenvectors are to be computed. On return, the matrix is | 
| 201 | > | *     overwritten | 
| 202 | > | * @param w will contain the eigenvalues of the matrix On return of this function | 
| 203 | > | * @param v the columns of this matrix will contain the eigenvectors. The eigenvectors are | 
| 204 | > | *    normalized and mutually orthogonal. | 
| 205 | > | */ | 
| 206 | > |  | 
| 207 | > | static int jacobi(SquareMatrix<Real, Dim>& a, Vector<Real, Dim>& d, | 
| 208 | > | SquareMatrix<Real, Dim>& v); | 
| 209 | > | };//end SquareMatrix | 
| 210 |  |  | 
| 211 |  |  | 
| 212 | < | #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) | 
| 185 | < | #define MAX_ROTATIONS 60 | 
| 212 | > | /*========================================================================= | 
| 213 |  |  | 
| 214 | < | template<Real, int Dim> | 
| 215 | < | void SquareMatrix<Real, int Dim>::jacobi(SquareMatrix<Real, Dim>& a, | 
| 189 | < | Vector<Real, Dim>& w, | 
| 190 | < | SquareMatrix<Real, Dim>& v) { | 
| 191 | < | const int N = Dim; | 
| 192 | < | int i, j, k, iq, ip; | 
| 193 | < | double tresh, theta, tau, t, sm, s, h, g, c; | 
| 194 | < | double tmp; | 
| 195 | < | Vector<Real, Dim> b, z; | 
| 214 | > | Program:   Visualization Toolkit | 
| 215 | > | Module:    $RCSfile: SquareMatrix.hpp,v $ | 
| 216 |  |  | 
| 217 | + | Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen | 
| 218 | + | All rights reserved. | 
| 219 | + | See Copyright.txt or http://www.kitware.com/Copyright.htm for details. | 
| 220 | + |  | 
| 221 | + | This software is distributed WITHOUT ANY WARRANTY; without even | 
| 222 | + | the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR | 
| 223 | + | PURPOSE.  See the above copyright notice for more information. | 
| 224 | + |  | 
| 225 | + | =========================================================================*/ | 
| 226 | + |  | 
| 227 | + | #define VTK_ROTATE(a,i,j,k,l) g=a(i, j);h=a(k, l);a(i, j)=g-s*(h+g*tau); \ | 
| 228 | + | a(k, l)=h+s*(g-h*tau) | 
| 229 | + |  | 
| 230 | + | #define VTK_MAX_ROTATIONS 20 | 
| 231 | + |  | 
| 232 | + | // Jacobi iteration for the solution of eigenvectors/eigenvalues of a nxn | 
| 233 | + | // real symmetric matrix. Square nxn matrix a; size of matrix in n; | 
| 234 | + | // output eigenvalues in w; and output eigenvectors in v. Resulting | 
| 235 | + | // eigenvalues/vectors are sorted in decreasing order; eigenvectors are | 
| 236 | + | // normalized. | 
| 237 | + | template<typename Real, int Dim> | 
| 238 | + | int SquareMatrix<Real, Dim>::jacobi(SquareMatrix<Real, Dim>& a, Vector<Real, Dim>& w, | 
| 239 | + | SquareMatrix<Real, Dim>& v) { | 
| 240 | + | const int n = Dim; | 
| 241 | + | int i, j, k, iq, ip, numPos; | 
| 242 | + | Real tresh, theta, tau, t, sm, s, h, g, c, tmp; | 
| 243 | + | Real bspace[4], zspace[4]; | 
| 244 | + | Real *b = bspace; | 
| 245 | + | Real *z = zspace; | 
| 246 | + |  | 
| 247 | + | // only allocate memory if the matrix is large | 
| 248 | + | if (n > 4) { | 
| 249 | + | b = new Real[n]; | 
| 250 | + | z = new Real[n]; | 
| 251 | + | } | 
| 252 | + |  | 
| 253 |  | // initialize | 
| 254 | < | for (ip=0; ip<N; ip++) | 
| 255 | < | { | 
| 256 | < | for (iq=0; iq<N; iq++) v(ip, iq) = 0.0; | 
| 257 | < | v(ip, ip) = 1.0; | 
| 254 | > | for (ip=0; ip<n; ip++) { | 
| 255 | > | for (iq=0; iq<n; iq++) { | 
| 256 | > | v(ip, iq) = 0.0; | 
| 257 | > | } | 
| 258 | > | v(ip, ip) = 1.0; | 
| 259 |  | } | 
| 260 | < | for (ip=0; ip<N; ip++) | 
| 261 | < | { | 
| 262 | < | b(ip) = w(ip) = a(ip, ip); | 
| 206 | < | z(ip) = 0.0; | 
| 260 | > | for (ip=0; ip<n; ip++) { | 
| 261 | > | b[ip] = w[ip] = a(ip, ip); | 
| 262 | > | z[ip] = 0.0; | 
| 263 |  | } | 
| 264 |  |  | 
| 265 |  | // begin rotation sequence | 
| 266 | < | for (i=0; i<MAX_ROTATIONS; i++) | 
| 267 | < | { | 
| 268 | < | sm = 0.0; | 
| 269 | < | for (ip=0; ip<2; ip++) | 
| 270 | < | { | 
| 215 | < | for (iq=ip+1; iq<N; iq++) sm += fabs(a(ip, iq)); | 
| 266 | > | for (i=0; i<VTK_MAX_ROTATIONS; i++) { | 
| 267 | > | sm = 0.0; | 
| 268 | > | for (ip=0; ip<n-1; ip++) { | 
| 269 | > | for (iq=ip+1; iq<n; iq++) { | 
| 270 | > | sm += fabs(a(ip, iq)); | 
| 271 |  | } | 
| 272 | < | if (sm == 0.0) break; | 
| 272 | > | } | 
| 273 | > | if (sm == 0.0) { | 
| 274 | > | break; | 
| 275 | > | } | 
| 276 |  |  | 
| 277 | < | if (i < 4) tresh = 0.2*sm/(9); | 
| 278 | < | else tresh = 0.0; | 
| 277 | > | if (i < 3) {                                // first 3 sweeps | 
| 278 | > | tresh = 0.2*sm/(n*n); | 
| 279 | > | } else { | 
| 280 | > | tresh = 0.0; | 
| 281 | > | } | 
| 282 |  |  | 
| 283 | < | for (ip=0; ip<2; ip++) | 
| 284 | < | { | 
| 285 | < | for (iq=ip+1; iq<N; iq++) | 
| 286 | < | { | 
| 287 | < | g = 100.0*fabs(a(ip, iq)); | 
| 288 | < | if (i > 4 && (fabs(w(ip))+g) == fabs(w(ip)) | 
| 289 | < | && (fabs(w(iq))+g) == fabs(w(iq))) | 
| 290 | < | { | 
| 291 | < | a(ip, iq) = 0.0; | 
| 292 | < | } | 
| 293 | < | else if (fabs(a(ip, iq)) > tresh) | 
| 294 | < | { | 
| 295 | < | h = w(iq) - w(ip); | 
| 296 | < | if ( (fabs(h)+g) == fabs(h)) t = (a(ip, iq)) / h; | 
| 297 | < | else | 
| 298 | < | { | 
| 299 | < | theta = 0.5*h / (a(ip, iq)); | 
| 300 | < | t = 1.0 / (fabs(theta)+sqrt(1.0+theta*theta)); | 
| 240 | < | if (theta < 0.0) t = -t; | 
| 241 | < | } | 
| 242 | < | c = 1.0 / sqrt(1+t*t); | 
| 243 | < | s = t*c; | 
| 244 | < | tau = s/(1.0+c); | 
| 245 | < | h = t*a(ip, iq); | 
| 246 | < | z(ip) -= h; | 
| 247 | < | z(iq) += h; | 
| 248 | < | w(ip) -= h; | 
| 249 | < | w(iq) += h; | 
| 250 | < | a(ip, iq)=0.0; | 
| 251 | < | for (j=0;j<ip-1;j++) | 
| 252 | < | { | 
| 253 | < | ROT(a,j,ip,j,iq); | 
| 254 | < | } | 
| 255 | < | for (j=ip+1;j<iq-1;j++) | 
| 256 | < | { | 
| 257 | < | ROT(a,ip,j,j,iq); | 
| 258 | < | } | 
| 259 | < | for (j=iq+1; j<N; j++) | 
| 260 | < | { | 
| 261 | < | ROT(a,ip,j,iq,j); | 
| 262 | < | } | 
| 263 | < | for (j=0; j<N; j++) | 
| 264 | < | { | 
| 265 | < | ROT(v,j,ip,j,iq); | 
| 266 | < | } | 
| 267 | < | } | 
| 283 | > | for (ip=0; ip<n-1; ip++) { | 
| 284 | > | for (iq=ip+1; iq<n; iq++) { | 
| 285 | > | g = 100.0*fabs(a(ip, iq)); | 
| 286 | > |  | 
| 287 | > | // after 4 sweeps | 
| 288 | > | if (i > 3 && (fabs(w[ip])+g) == fabs(w[ip]) | 
| 289 | > | && (fabs(w[iq])+g) == fabs(w[iq])) { | 
| 290 | > | a(ip, iq) = 0.0; | 
| 291 | > | } else if (fabs(a(ip, iq)) > tresh) { | 
| 292 | > | h = w[iq] - w[ip]; | 
| 293 | > | if ( (fabs(h)+g) == fabs(h)) { | 
| 294 | > | t = (a(ip, iq)) / h; | 
| 295 | > | } else { | 
| 296 | > | theta = 0.5*h / (a(ip, iq)); | 
| 297 | > | t = 1.0 / (fabs(theta)+sqrt(1.0+theta*theta)); | 
| 298 | > | if (theta < 0.0) { | 
| 299 | > | t = -t; | 
| 300 | > | } | 
| 301 |  | } | 
| 302 | < | } | 
| 302 | > | c = 1.0 / sqrt(1+t*t); | 
| 303 | > | s = t*c; | 
| 304 | > | tau = s/(1.0+c); | 
| 305 | > | h = t*a(ip, iq); | 
| 306 | > | z[ip] -= h; | 
| 307 | > | z[iq] += h; | 
| 308 | > | w[ip] -= h; | 
| 309 | > | w[iq] += h; | 
| 310 | > | a(ip, iq)=0.0; | 
| 311 |  |  | 
| 312 | < | for (ip=0; ip<N; ip++) | 
| 313 | < | { | 
| 314 | < | b(ip) += z(ip); | 
| 315 | < | w(ip) = b(ip); | 
| 316 | < | z(ip) = 0.0; | 
| 312 | > | // ip already shifted left by 1 unit | 
| 313 | > | for (j = 0;j <= ip-1;j++) { | 
| 314 | > | VTK_ROTATE(a,j,ip,j,iq); | 
| 315 | > | } | 
| 316 | > | // ip and iq already shifted left by 1 unit | 
| 317 | > | for (j = ip+1;j <= iq-1;j++) { | 
| 318 | > | VTK_ROTATE(a,ip,j,j,iq); | 
| 319 | > | } | 
| 320 | > | // iq already shifted left by 1 unit | 
| 321 | > | for (j=iq+1; j<n; j++) { | 
| 322 | > | VTK_ROTATE(a,ip,j,iq,j); | 
| 323 | > | } | 
| 324 | > | for (j=0; j<n; j++) { | 
| 325 | > | VTK_ROTATE(v,j,ip,j,iq); | 
| 326 | > | } | 
| 327 | > | } | 
| 328 |  | } | 
| 329 | + | } | 
| 330 | + |  | 
| 331 | + | for (ip=0; ip<n; ip++) { | 
| 332 | + | b[ip] += z[ip]; | 
| 333 | + | w[ip] = b[ip]; | 
| 334 | + | z[ip] = 0.0; | 
| 335 | + | } | 
| 336 |  | } | 
| 337 |  |  | 
| 338 | < | if ( i >= MAX_ROTATIONS ) | 
| 339 | < | return false; | 
| 338 | > | //// this is NEVER called | 
| 339 | > | if ( i >= VTK_MAX_ROTATIONS ) { | 
| 340 | > | std::cout << "vtkMath::Jacobi: Error extracting eigenfunctions" << std::endl; | 
| 341 | > | return 0; | 
| 342 | > | } | 
| 343 |  |  | 
| 344 | < | // sort eigenfunctions | 
| 345 | < | for (j=0; j<N; j++) | 
| 346 | < | { | 
| 347 | < | k = j; | 
| 348 | < | tmp = w(k); | 
| 349 | < | for (i=j; i<N; i++) | 
| 350 | < | { | 
| 351 | < | if (w(i) >= tmp) | 
| 290 | < | { | 
| 291 | < | k = i; | 
| 292 | < | tmp = w(k); | 
| 293 | < | } | 
| 344 | > | // sort eigenfunctions                 these changes do not affect accuracy | 
| 345 | > | for (j=0; j<n-1; j++) {                  // boundary incorrect | 
| 346 | > | k = j; | 
| 347 | > | tmp = w[k]; | 
| 348 | > | for (i=j+1; i<n; i++) {                // boundary incorrect, shifted already | 
| 349 | > | if (w[i] >= tmp) {                   // why exchage if same? | 
| 350 | > | k = i; | 
| 351 | > | tmp = w[k]; | 
| 352 |  | } | 
| 353 | < | if (k != j) | 
| 354 | < | { | 
| 355 | < | w(k) = w(j); | 
| 356 | < | w(j) = tmp; | 
| 357 | < | for (i=0; i<N; i++) | 
| 358 | < | { | 
| 359 | < | tmp = v(i, j); | 
| 360 | < | v(i, j) = v(i, k); | 
| 303 | < | v(i, k) = tmp; | 
| 304 | < | } | 
| 353 | > | } | 
| 354 | > | if (k != j) { | 
| 355 | > | w[k] = w[j]; | 
| 356 | > | w[j] = tmp; | 
| 357 | > | for (i=0; i<n; i++) { | 
| 358 | > | tmp = v(i, j); | 
| 359 | > | v(i, j) = v(i, k); | 
| 360 | > | v(i, k) = tmp; | 
| 361 |  | } | 
| 362 | + | } | 
| 363 |  | } | 
| 364 | + | // insure eigenvector consistency (i.e., Jacobi can compute vectors that | 
| 365 | + | // are negative of one another (.707,.707,0) and (-.707,-.707,0). This can | 
| 366 | + | // reek havoc in hyperstreamline/other stuff. We will select the most | 
| 367 | + | // positive eigenvector. | 
| 368 | + | int ceil_half_n = (n >> 1) + (n & 1); | 
| 369 | + | for (j=0; j<n; j++) { | 
| 370 | + | for (numPos=0, i=0; i<n; i++) { | 
| 371 | + | if ( v(i, j) >= 0.0 ) { | 
| 372 | + | numPos++; | 
| 373 | + | } | 
| 374 | + | } | 
| 375 | + | //    if ( numPos < ceil(RealType(n)/RealType(2.0)) ) | 
| 376 | + | if ( numPos < ceil_half_n) { | 
| 377 | + | for (i=0; i<n; i++) { | 
| 378 | + | v(i, j) *= -1.0; | 
| 379 | + | } | 
| 380 | + | } | 
| 381 | + | } | 
| 382 |  |  | 
| 383 | < | //    insure eigenvector consistency (i.e., Jacobi can compute | 
| 384 | < | //    vectors that are negative of one another (.707,.707,0) and | 
| 385 | < | //    (-.707,-.707,0). This can reek havoc in | 
| 311 | < | //    hyperstreamline/other stuff. We will select the most | 
| 312 | < | //    positive eigenvector. | 
| 313 | < | int numPos; | 
| 314 | < | for (j=0; j<N; j++) | 
| 315 | < | { | 
| 316 | < | for (numPos=0, i=0; i<N; i++) if ( v(i, j) >= 0.0 ) numPos++; | 
| 317 | < | if ( numPos < 2 ) for(i=0; i<N; i++) v(i, j) *= -1.0; | 
| 383 | > | if (n > 4) { | 
| 384 | > | delete [] b; | 
| 385 | > | delete [] z; | 
| 386 |  | } | 
| 387 | + | return 1; | 
| 388 | + | } | 
| 389 |  |  | 
| 320 | – | return true; | 
| 321 | – | } | 
| 390 |  |  | 
| 391 | < | #undef ROT | 
| 324 | < | #undef MAX_ROTATIONS | 
| 325 | < |  | 
| 391 | > | typedef SquareMatrix<RealType, 6> Mat6x6d; | 
| 392 |  | } | 
| 327 | – |  | 
| 328 | – |  | 
| 329 | – | } | 
| 393 |  | #endif //MATH_SQUAREMATRIX_HPP | 
| 394 | + |  |