ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-2.0/src/math/SquareMatrix.hpp
(Generate patch)

Comparing trunk/OOPSE-2.0/src/math/SquareMatrix.hpp (file contents):
Revision 1563 by tim, Wed Oct 13 06:51:09 2004 UTC vs.
Revision 1644 by tim, Mon Oct 25 22:46:19 2004 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines