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