ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/math/SquareMatrix.hpp
(Generate patch)

Comparing:
trunk/src/math/SquareMatrix.hpp (file contents), Revision 70 by tim, Wed Oct 13 06:51:09 2004 UTC vs.
branches/development/src/math/SquareMatrix.hpp (file contents), Revision 1665 by gezelter, Tue Nov 22 20:38:56 2011 UTC

# Line 1 | Line 1
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
# Line 32 | Line 49
49   #ifndef MATH_SQUAREMATRIX_HPP
50   #define MATH_SQUAREMATRIX_HPP
51  
52 < #include "Vector3d.hpp"
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{
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 <        /** Constructs and initializes every element of this matrix to a scalar */
77 <        SquareMatrix(double s) {
78 <            for (unsigned int i = 0; i < Dim; i++)
59 <                for (unsigned int j = 0; j < Dim; j++)
60 <                    data_[i][j] = s;
61 <        }
76 >    /** Constructs and initializes every element of this matrix to a scalar */
77 >    SquareMatrix(Real s) : RectMatrix<Real, Dim, Dim>(s){
78 >    }
79  
80 <        /** copy constructor */
81 <        SquareMatrix(const SquareMatrix<Real, Dim>& m) {
82 <            *this = m;
66 <        }
67 <        
68 <        /** destructor*/
69 <        ~SquareMatrix() {}
80 >    /** Constructs and initializes from an array */
81 >    SquareMatrix(Real* array) : RectMatrix<Real, Dim, Dim>(array){
82 >    }
83  
71        /** copy assignment operator */
72        SquareMatrix<Real, Dim>& operator =(const SquareMatrix<Real, Dim>& m) {
73            for (unsigned int i = 0; i < Dim; i++)
74                for (unsigned int j = 0; j < Dim; j++)
75                    data_[i][j] = m.data_[i][j];
76        }
77        
78        /**
79         * Return the reference of a single element of this matrix.
80         * @return the reference of a single element of this matrix
81         * @param i row index
82         * @param j colum index
83         */
84        double& operator()(unsigned int i, unsigned int j) {
85            return data_[i][j];
86        }
84  
85 <        /**
86 <         * Return the value of a single element of this matrix.
87 <         * @return the value of a single element of this matrix
88 <         * @param i row index
89 <         * @param j colum index
90 <         */        
91 <        double operator()(unsigned int i, unsigned int j) const  {
92 <            return data_[i][j];  
93 <        }
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 <        /**
98 <         * Returns a row of  this matrix as a vector.
99 <         * @return a row of  this matrix as a vector
100 <         * @param row the row index
101 <         */                
102 <        Vector<Real, Dim> getRow(unsigned int row) {
103 <            Vector<Real, Dim> v;
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 <            for (unsigned int i = 0; i < Dim; i++)
108 <                v[i] = data_[row][i];
107 >      return m;
108 >    }
109  
110 <            return v;
111 <        }
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 <        /**
118 <         * Sets a row of  this matrix
114 <         * @param row the row index
115 <         * @param v the vector to be set
116 <         */                
117 <         void setRow(unsigned int row, const Vector<Real, Dim>& v) {
118 <            Vector<Real, Dim> v;
117 >      return result;
118 >    }        
119  
120 <            for (unsigned int i = 0; i < Dim; i++)
121 <                data_[row][i] = v[i];
122 <         }
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 <        /**
130 <         * Returns a column of  this matrix as a vector.
131 <         * @return a column of  this matrix as a vector
132 <         * @param col the column index
133 <         */                
134 <        Vector<Real, Dim> getColum(unsigned int col) {
130 <            Vector<Real, Dim> v;
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 <            for (unsigned int i = 0; i < Dim; i++)
137 <                v[i] = data_[i][col];
136 >      return tmp;
137 >    }
138  
139 <            return v;
140 <        }
139 >    /** Tests if this matrix is symmetrix. */            
140 >    bool isSymmetric() const {
141 >      for (unsigned int i = 0; i < Dim - 1; i++)
142 >        for (unsigned int j = i; j < Dim; j++)
143 >          if (fabs(this->data_[i][j] - this->data_[j][i]) > epsilon)
144 >            return false;
145 >                        
146 >      return true;
147 >    }
148  
149 <        /**
150 <         * Sets a column of  this matrix
151 <         * @param col the column index
141 <         * @param v the vector to be set
142 <         */                
143 <         void setColum(unsigned int col, const Vector<Real, Dim>& v){
144 <            Vector<Real, Dim> v;
149 >    /** Tests if this matrix is orthogonal. */            
150 >    bool isOrthogonal() {
151 >      SquareMatrix<Real, Dim> tmp;
152  
153 <            for (unsigned int i = 0; i < Dim; i++)
147 <                data_[i][col] = v[i];
148 <         }        
153 >      tmp = *this * transpose();
154  
155 <        /** Negates the value of this matrix in place. */          
156 <        inline void negate() {
152 <            for (unsigned int i = 0; i < Dim; i++)
153 <                for (unsigned int j = 0; j < Dim; j++)
154 <                    data_[i][j] = -data_[i][j];
155 <        }
156 <        
157 <        /**
158 <        * Sets the value of this matrix to the negation of matrix m.
159 <        * @param m the source matrix
160 <        */
161 <        inline void negate(const SquareMatrix<Real, Dim>& m) {
162 <            for (unsigned int i = 0; i < Dim; i++)
163 <                for (unsigned int j = 0; j < Dim; j++)
164 <                    data_[i][j] = -m.data_[i][j];        
165 <        }
166 <        
167 <        /**
168 <        * Sets the value of this matrix to the sum of itself and m (*this += m).
169 <        * @param m the other matrix
170 <        */
171 <        inline void add( const SquareMatrix<Real, Dim>& m ) {
172 <            for (unsigned int i = 0; i < Dim; i++)
173 <                for (unsigned int j = 0; j < Dim; j++)        
174 <                data_[i][j] += m.data_[i][j];
175 <            }
176 <        
177 <        /**
178 <        * Sets the value of this matrix to the sum of m1 and m2 (*this = m1 + m2).
179 <        * @param m1 the first matrix
180 <        * @param m2 the second matrix
181 <        */
182 <        inline void add( const SquareMatrix<Real, Dim>& m1, const SquareMatrix<Real, Dim>& m2 ) {
183 <            for (unsigned int i = 0; i < Dim; i++)
184 <                for (unsigned int j = 0; j < Dim; j++)        
185 <                data_[i][j] = m1.data_[i][j] + m2.data_[i][j];
186 <        }
187 <        
188 <        /**
189 <        * Sets the value of this matrix to the difference  of itself and m (*this -= m).
190 <        * @param m the other matrix
191 <        */
192 <        inline void sub( const SquareMatrix<Real, Dim>& m ) {
193 <            for (unsigned int i = 0; i < Dim; i++)
194 <                for (unsigned int j = 0; j < Dim; j++)        
195 <                data_[i][j] -= m.data_[i][j];
196 <        }
197 <        
198 <        /**
199 <        * Sets the value of this matrix to the difference of matrix m1 and m2 (*this = m1 - m2).
200 <        * @param m1 the first matrix
201 <        * @param m2 the second matrix
202 <        */
203 <        inline void sub( const SquareMatrix<Real, Dim>& m1, const Vector  &m2){
204 <            for (unsigned int i = 0; i < Dim; i++)
205 <                for (unsigned int j = 0; j < Dim; j++)        
206 <                data_[i][j] = m1.data_[i][j] - m2.data_[i][j];
207 <        }
208 <        
209 <        /**
210 <        * Sets the value of this matrix to the scalar multiplication of itself (*this *= s).
211 <        * @param s the scalar value
212 <        */
213 <        inline void mul( double s ) {
214 <            for (unsigned int i = 0; i < Dim; i++)
215 <                for (unsigned int j = 0; j < Dim; j++)  
216 <                    data_[i][j] *= s;
217 <        }
155 >      return tmp.isDiagonal();
156 >    }
157  
158 <        /**
159 <        * Sets the value of this matrix to the scalar multiplication of matrix m  (*this = s * m).
160 <        * @param s the scalar value
161 <        * @param m the matrix
162 <        */
163 <        inline void mul( double s, const SquareMatrix<Real, Dim>& m ) {
164 <            for (unsigned int i = 0; i < Dim; i++)
165 <                for (unsigned int j = 0; j < Dim; j++)  
166 <                    data_[i][j] = s * m.data_[i][j];
228 <        }
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(this->data_[i][j]) > epsilon)
163 >            return false;
164 >                        
165 >      return true;
166 >    }
167  
168 <        /**
169 <        * Sets the value of this matrix to the  multiplication of this matrix and matrix m
170 <        * (*this = *this * m).
171 <        * @param m the matrix
172 <        */
173 <        inline void mul(const SquareMatrix<Real, Dim>& m ) {
174 <            SquareMatrix<Real, Dim> tmp(*this);
175 <            
238 <            for (unsigned int i = 0; i < Dim; i++)
239 <                for (unsigned int j = 0; j < Dim; j++) {  
168 >    /** Tests if this matrix is the unit matrix. */
169 >    bool isUnitMatrix() const {
170 >      if (!isDiagonal())
171 >        return false;
172 >                
173 >      for (unsigned int i = 0; i < Dim ; i++)
174 >        if (fabs(this->data_[i][i] - 1) > epsilon)
175 >          return false;
176                      
177 <                    data_[i][j] = 0.0;
178 <                    for (unsigned int k = 0; k < Dim; k++)
243 <                        data_[i][j]  = tmp.data_[i][k] * m.data_[k][j]
244 <                }
245 <        }
246 <        
247 <        /**
248 <        * Sets the value of this matrix to the  left multiplication of matrix m into itself
249 <        * (*this = m *  *this).
250 <        * @param m the matrix
251 <        */
252 <        inline void leftmul(const SquareMatrix<Real, Dim>& m ) {
253 <            SquareMatrix<Real, Dim> tmp(*this);
254 <            
255 <            for (unsigned int i = 0; i < Dim; i++)
256 <                for (unsigned int j = 0; j < Dim; j++) {  
257 <                    
258 <                    data_[i][j] = 0.0;
259 <                    for (unsigned int k = 0; k < Dim; k++)
260 <                        data_[i][j]  = m.data_[i][k] * tmp.data_[k][j]
261 <                }
262 <        }
177 >      return true;
178 >    }        
179  
180 <        /**
181 <        * Sets the value of this matrix to the  multiplication of matrix m1 and matrix m2
182 <        * (*this = m1 * m2).
183 <        * @param m1 the first  matrix
184 <        * @param m2 the second matrix
185 <        */
186 <        inline void mul(const SquareMatrix<Real, Dim>& m1,
271 <                                  const SquareMatrix<Real, Dim>& m2 ) {
272 <            for (unsigned int i = 0; i < Dim; i++)
273 <                for (unsigned int j = 0; j < Dim; j++) {  
274 <                    
275 <                    data_[i][j] = 0.0;
276 <                    for (unsigned int k = 0; k < Dim; k++)
277 <                        data_[i][j]  = m1.data_[i][k] * m2.data_[k][j]
278 <                }
180 >    /** Return the transpose of this matrix */
181 >    SquareMatrix<Real,  Dim> transpose() const{
182 >      SquareMatrix<Real,  Dim> result;
183 >                
184 >      for (unsigned int i = 0; i < Dim; i++)
185 >        for (unsigned int j = 0; j < Dim; j++)              
186 >          result(j, i) = this->data_[i][j];
187  
188 <        }
189 <        
282 <        /**
283 <        * Sets the value of this matrix to the scalar division of itself  (*this /= s ).
284 <        * @param s the scalar value
285 <        */            
286 <        inline void div( double s) {
287 <            for (unsigned int i = 0; i < Dim; i++)
288 <                for (unsigned int j = 0; j < Dim; j++)  
289 <                    data_[i][j] /= s;
290 <        }
291 <        
292 <        inline SquareMatrix<Real, Dim>& operator=(const SquareMatrix<Real, Dim>& v) {
293 <            if (this == &v)
294 <                return *this;
188 >      return result;
189 >    }
190              
191 <            for (unsigned int i = 0; i < Dim; i++)            
192 <                data_[i] = v[i];
193 <            
194 <            return *this;
300 <        }
301 <        
302 <        /**
303 <        * Sets the value of this matrix to the scalar division of matrix v1  (*this = v1 / s ).
304 <        * @paran v1 the source matrix
305 <        * @param s the scalar value
306 <        */                        
307 <        inline void div( const SquareMatrix<Real, Dim>& v1, double s ) {
308 <            for (unsigned int i = 0; i < Dim; i++)
309 <                data_[i] = v1.data_[i] / s;
310 <        }
191 >    /** @todo need implementation */
192 >    void diagonalize() {
193 >      //jacobi(m, eigenValues, ortMat);
194 >    }
195  
196 <        /**
197 <         *  Multiples a scalar into every element of this matrix.
198 <         * @param s the scalar value
199 <         */
200 <        SquareMatrix<Real, Dim>& operator *=(const double s) {
201 <            this->mul(s);
202 <            return *this;
203 <        }
196 >    /**
197 >     * Jacobi iteration routines for computing eigenvalues/eigenvectors of
198 >     * real symmetric matrix
199 >     *
200 >     * @return true if success, otherwise return false
201 >     * @param a symmetric matrix whose eigenvectors are to be computed. On return, the matrix is
202 >     *     overwritten
203 >     * @param w will contain the eigenvalues of the matrix On return of this function
204 >     * @param v the columns of this matrix will contain the eigenvectors. The eigenvectors are
205 >     *    normalized and mutually orthogonal.
206 >     */
207 >          
208 >    static int jacobi(SquareMatrix<Real, Dim>& a, Vector<Real, Dim>& d,
209 >                      SquareMatrix<Real, Dim>& v);
210 >  };//end SquareMatrix
211  
321        /**
322         *  Divides every element of this matrix by a scalar.
323         * @param s the scalar value
324         */
325        SquareMatrix<Real, Dim>& operator /=(const double s) {
326            this->div(s);
327            return *this;
328        }
212  
213 <        /**
331 <         * Sets the value of this matrix to the sum of the other matrix and itself (*this += m).
332 <         * @param m the other matrix
333 <         */
334 <        SquareMatrix<Real, Dim>& operator += (const SquareMatrix<Real, Dim>& m) {
335 <            add(m);
336 <            return *this;
337 <         }
213 >  /*=========================================================================
214  
215 <        /**
216 <         * Sets the value of this matrix to the differerence of itself and the other matrix (*this -= m)
341 <         * @param m the other matrix
342 <         */
343 <        SquareMatrix<Real, Dim>& operator -= (const SquareMatrix<Real, Dim>& m){
344 <            sub(m);
345 <            return *this;
346 <        }
215 >  Program:   Visualization Toolkit
216 >  Module:    $RCSfile: SquareMatrix.hpp,v $
217  
218 <        /** set this matrix to an identity matrix*/
218 >  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
219 >  All rights reserved.
220 >  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
221  
222 <       void identity() {
223 <            for (unsigned int i = 0; i < Dim; i++)
224 <                for (unsigned int i = 0; i < Dim; i++)
353 <                    if (i == j)
354 <                        data_[i][j] = 1.0;
355 <                    else
356 <                        data_[i][j] = 0.0;
357 <        }
222 >  This software is distributed WITHOUT ANY WARRANTY; without even
223 >  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
224 >  PURPOSE.  See the above copyright notice for more information.
225  
226 <        /** Sets the value of this matrix to  the inversion of itself. */
360 <        void  inverse() {
361 <            inverse(*this);
362 <        }
226 >  =========================================================================*/
227  
228 <        /**
229 <         * Sets the value of this matrix to  the inversion of other matrix.
366 <         * @ param m the source matrix
367 <         */        
368 <        void inverse(const SquareMatrix<Real, Dim>& m);
369 <        
370 <        /** Sets the value of this matrix to  the transpose of itself. */
371 <        void transpose() {
372 <            for (unsigned int i = 0; i < Dim - 1; i++)
373 <                for (unsigned int j = i; j < Dim; j++)
374 <                    std::swap(data_[i][j], data_[j][i]);
375 <        }
228 > #define VTK_ROTATE(a,i,j,k,l) g=a(i, j);h=a(k, l);a(i, j)=g-s*(h+g*tau); \
229 >    a(k, l)=h+s*(g-h*tau)
230  
231 <        /**
378 <         * Sets the value of this matrix to  the transpose of other matrix.
379 <         * @ param m the source matrix
380 <         */        
381 <        void transpose(const SquareMatrix<Real, Dim>& m) {
382 <            
383 <            if (this == &m) {
384 <                transpose();
385 <            } else {
386 <                for (unsigned int i = 0; i < Dim; i++)
387 <                    for (unsigned int j =0; j < Dim; j++)
388 <                        data_[i][j] = m.data_[i][j];
389 <            }
390 <        }
231 > #define VTK_MAX_ROTATIONS 20
232  
233 <        /** Returns the determinant of this matrix. */
234 <        double determinant() const {
233 >  // Jacobi iteration for the solution of eigenvectors/eigenvalues of a nxn
234 >  // real symmetric matrix. Square nxn matrix a; size of matrix in n;
235 >  // output eigenvalues in w; and output eigenvectors in v. Resulting
236 >  // eigenvalues/vectors are sorted in decreasing order; eigenvectors are
237 >  // normalized.
238 >  template<typename Real, int Dim>
239 >  int SquareMatrix<Real, Dim>::jacobi(SquareMatrix<Real, Dim>& a, Vector<Real, Dim>& w,
240 >                                      SquareMatrix<Real, Dim>& v) {
241 >    const int n = Dim;  
242 >    int i, j, k, iq, ip, numPos;
243 >    Real tresh, theta, tau, t, sm, s, h, g, c, tmp;
244 >    Real bspace[4], zspace[4];
245 >    Real *b = bspace;
246 >    Real *z = zspace;
247  
248 <        }
248 >    // only allocate memory if the matrix is large
249 >    if (n > 4) {
250 >      b = new Real[n];
251 >      z = new Real[n];
252 >    }
253  
254 <        /** Returns the trace of this matrix. */
255 <        double trace() const {
256 <           double tmp = 0;
257 <          
258 <            for (unsigned int i = 0; i < Dim ; i++)
259 <                tmp += data_[i][i];
254 >    // initialize
255 >    for (ip=0; ip<n; ip++) {
256 >      for (iq=0; iq<n; iq++) {
257 >        v(ip, iq) = 0.0;
258 >      }
259 >      v(ip, ip) = 1.0;
260 >    }
261 >    for (ip=0; ip<n; ip++) {
262 >      b[ip] = w[ip] = a(ip, ip);
263 >      z[ip] = 0.0;
264 >    }
265  
266 <            return tmp;
267 <        }
266 >    // begin rotation sequence
267 >    for (i=0; i<VTK_MAX_ROTATIONS; i++) {
268 >      sm = 0.0;
269 >      for (ip=0; ip<n-1; ip++) {
270 >        for (iq=ip+1; iq<n; iq++) {
271 >          sm += fabs(a(ip, iq));
272 >        }
273 >      }
274 >      if (sm == 0.0) {
275 >        break;
276 >      }
277  
278 <        /** Tests if this matrix is symmetrix. */            
279 <        bool isSymmetric() const {
280 <            for (unsigned int i = 0; i < Dim - 1; i++)
281 <                for (unsigned int j = i; j < Dim; j++)
282 <                    if (fabs(data_[i][j] - data_[j][i]) > epsilon)
412 <                        return false;
413 <                    
414 <            return true;
415 <        }
278 >      if (i < 3) {                                // first 3 sweeps
279 >        tresh = 0.2*sm/(n*n);
280 >      } else {
281 >        tresh = 0.0;
282 >      }
283  
284 <        /** Tests if this matrix is orthogona. */            
285 <        bool isOrthogonal() const {
286 <            SquareMatrix<Real, Dim> t(*this);
284 >      for (ip=0; ip<n-1; ip++) {
285 >        for (iq=ip+1; iq<n; iq++) {
286 >          g = 100.0*fabs(a(ip, iq));
287  
288 <            t.transpose();
288 >          // after 4 sweeps
289 >          if (i > 3 && (fabs(w[ip])+g) == fabs(w[ip])
290 >              && (fabs(w[iq])+g) == fabs(w[iq])) {
291 >            a(ip, iq) = 0.0;
292 >          } else if (fabs(a(ip, iq)) > tresh) {
293 >            h = w[iq] - w[ip];
294 >            if ( (fabs(h)+g) == fabs(h)) {
295 >              t = (a(ip, iq)) / h;
296 >            } else {
297 >              theta = 0.5*h / (a(ip, iq));
298 >              t = 1.0 / (fabs(theta)+sqrt(1.0+theta*theta));
299 >              if (theta < 0.0) {
300 >                t = -t;
301 >              }
302 >            }
303 >            c = 1.0 / sqrt(1+t*t);
304 >            s = t*c;
305 >            tau = s/(1.0+c);
306 >            h = t*a(ip, iq);
307 >            z[ip] -= h;
308 >            z[iq] += h;
309 >            w[ip] -= h;
310 >            w[iq] += h;
311 >            a(ip, iq)=0.0;
312  
313 <            return isUnitMatrix(*this * t);
314 <        }
313 >            // ip already shifted left by 1 unit
314 >            for (j = 0;j <= ip-1;j++) {
315 >              VTK_ROTATE(a,j,ip,j,iq);
316 >            }
317 >            // ip and iq already shifted left by 1 unit
318 >            for (j = ip+1;j <= iq-1;j++) {
319 >              VTK_ROTATE(a,ip,j,j,iq);
320 >            }
321 >            // iq already shifted left by 1 unit
322 >            for (j=iq+1; j<n; j++) {
323 >              VTK_ROTATE(a,ip,j,iq,j);
324 >            }
325 >            for (j=0; j<n; j++) {
326 >              VTK_ROTATE(v,j,ip,j,iq);
327 >            }
328 >          }
329 >        }
330 >      }
331  
332 <        /** Tests if this matrix is diagonal. */
333 <        bool isDiagonal() const {
334 <            for (unsigned int i = 0; i < Dim ; i++)
335 <                for (unsigned int j = 0; j < Dim; j++)
336 <                    if (i !=j && fabs(data_[i][j]) > epsilon)
431 <                        return false;
432 <                    
433 <            return true;
434 <        }
435 <
436 <        /** Tests if this matrix is the unit matrix. */
437 <        bool isUnitMatrix() const {
438 <            if (!isDiagonal())
439 <                return false;
440 <            
441 <            for (unsigned int i = 0; i < Dim ; i++)
442 <                if (fabs(data_[i][i] - 1) > epsilon)
443 <                    return false;
444 <                
445 <            return true;
446 <        }
447 <        
448 <        protected:
449 <            double data_[Dim][Dim]; /**< matrix element */            
450 <
451 <    };//end SquareMatrix
452 <
453 <    
454 <    /** Negate the value of every element of this matrix. */
455 <    template<typename Real, int Dim>
456 <    inline SquareMatrix<Real, Dim> operator -(const SquareMatrix& m) {
457 <        SquareMatrix<Real, Dim> result(m);
458 <
459 <        result.negate();
460 <
461 <        return result;
332 >      for (ip=0; ip<n; ip++) {
333 >        b[ip] += z[ip];
334 >        w[ip] = b[ip];
335 >        z[ip] = 0.0;
336 >      }
337      }
463    
464    /**
465    * Return the sum of two matrixes  (m1 + m2).
466    * @return the sum of two matrixes
467    * @param m1 the first matrix
468    * @param m2 the second matrix
469    */
470    template<typename Real, int Dim>
471    inline SquareMatrix<Real, Dim> operator + (const SquareMatrix<Real, Dim>& m1,
472                                                                                         const SquareMatrix<Real, Dim>& m2) {
473        SquareMatrix<Real, Dim>result;
338  
339 <        result.add(m1, m2);
340 <
341 <        return result;
339 >    //// this is NEVER called
340 >    if ( i >= VTK_MAX_ROTATIONS ) {
341 >      std::cout << "vtkMath::Jacobi: Error extracting eigenfunctions" << std::endl;
342 >      return 0;
343      }
479    
480    /**
481    * Return the difference of two matrixes  (m1 - m2).
482    * @return the sum of two matrixes
483    * @param m1 the first matrix
484    * @param m2 the second matrix
485    */
486    template<typename Real, int Dim>
487    inline SquareMatrix<Real, Dim> operator - (const SquareMatrix<Real, Dim>& m1,
488                                                                                        const SquareMatrix<Real, Dim>& m2) {
489        SquareMatrix<Real, Dim>result;
344  
345 <        result.sub(m1, m2);
346 <
347 <        return result;
345 >    // sort eigenfunctions                 these changes do not affect accuracy
346 >    for (j=0; j<n-1; j++) {                  // boundary incorrect
347 >      k = j;
348 >      tmp = w[k];
349 >      for (i=j+1; i<n; i++) {                // boundary incorrect, shifted already
350 >        if (w[i] >= tmp) {                   // why exchage if same?
351 >          k = i;
352 >          tmp = w[k];
353 >        }
354 >      }
355 >      if (k != j) {
356 >        w[k] = w[j];
357 >        w[j] = tmp;
358 >        for (i=0; i<n; i++) {
359 >          tmp = v(i, j);
360 >          v(i, j) = v(i, k);
361 >          v(i, k) = tmp;
362 >        }
363 >      }
364      }
365 <    
366 <    /**
367 <    * Return the multiplication of two matrixes  (m1 * m2).
368 <    * @return the multiplication of two matrixes
369 <    * @param m1 the first matrix
370 <    * @param m2 the second matrix
371 <    */
372 <    template<typename Real, int Dim>
373 <    inline SquareMatrix<Real, Dim> operator *(const SquareMatrix<Real, Dim>& m1,
374 <                                                                                       const SquareMatrix<Real, Dim>& m2) {
375 <        SquareMatrix<Real, Dim> result;
376 <
377 <        result.mul(m1, m2);
378 <
379 <        return result;
365 >    // insure eigenvector consistency (i.e., Jacobi can compute vectors that
366 >    // are negative of one another (.707,.707,0) and (-.707,-.707,0). This can
367 >    // reek havoc in hyperstreamline/other stuff. We will select the most
368 >    // positive eigenvector.
369 >    int ceil_half_n = (n >> 1) + (n & 1);
370 >    for (j=0; j<n; j++) {
371 >      for (numPos=0, i=0; i<n; i++) {
372 >        if ( v(i, j) >= 0.0 ) {
373 >          numPos++;
374 >        }
375 >      }
376 >      //    if ( numPos < ceil(RealType(n)/RealType(2.0)) )
377 >      if ( numPos < ceil_half_n) {
378 >        for (i=0; i<n; i++) {
379 >          v(i, j) *= -1.0;
380 >        }
381 >      }
382      }
511    
512    /**
513    * Return the multiplication of  matrixes m  and vector v (m * v).
514    * @return the multiplication of matrixes and vector
515    * @param m the matrix
516    * @param v the vector
517    */
518    template<typename Real, int Dim>
519    inline Vector<Real, Dim> operator *(const SquareMatrix<Real, Dim>& m,
520                                                                 const SquareMatrix<Real, Dim>& v) {
521        Vector<Real, Dim> result;
383  
384 <        for (unsigned int i = 0; i < Dim ; i++)
385 <            for (unsigned int j = 0; j < Dim ; j++)            
386 <                result[i] += m(i, j) * v[j];
526 <            
527 <        return result;                                                                
384 >    if (n > 4) {
385 >      delete [] b;
386 >      delete [] z;
387      }
388 +    return 1;
389 +  }
390 +
391 +
392 +  typedef SquareMatrix<RealType, 6> Mat6x6d;
393   }
394   #endif //MATH_SQUAREMATRIX_HPP
395 +

Comparing:
trunk/src/math/SquareMatrix.hpp (property svn:keywords), Revision 70 by tim, Wed Oct 13 06:51:09 2004 UTC vs.
branches/development/src/math/SquareMatrix.hpp (property svn:keywords), Revision 1665 by gezelter, Tue Nov 22 20:38:56 2011 UTC

# Line 0 | Line 1
1 + Author Id Revision Date

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines