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

Comparing trunk/OOPSE-3.0/src/math/SquareMatrix.hpp (file contents):
Revision 1567 by tim, Wed Oct 13 23:53:40 2004 UTC vs.
Revision 1586 by tim, Sun Oct 17 01:19:11 2004 UTC

# Line 83 | Line 83 | namespace oopse {
83               SquareMatrix<Real, Dim> result;
84  
85               return result;
86 <        }
86 >        }        
87  
88        
89
88          /** Returns the determinant of this matrix. */
89          double determinant() const {
90              double det;
# Line 113 | Line 111 | namespace oopse {
111              return true;
112          }
113  
114 <        /** Tests if this matrix is orthogona. */            
114 >        /** Tests if this matrix is orthogonal. */            
115          bool isOrthogonal() {
116              SquareMatrix<Real, Dim> tmp;
117  
118              tmp = *this * transpose();
119  
120 <            return tmp.isUnitMatrix();
120 >            return tmp.isDiagonal();
121          }
122  
123          /** Tests if this matrix is diagonal. */
# Line 144 | Line 142 | namespace oopse {
142              return true;
143          }        
144  
145 +        void diagonalize() {
146 +            jacobi(m, eigenValues, ortMat);
147 +        }
148 +
149 +        /**
150 +         * Finds the eigenvalues and eigenvectors of a symmetric matrix
151 +         * @param eigenvals a reference to a vector3 where the
152 +         * eigenvalues will be stored. The eigenvalues are ordered so
153 +         * that eigenvals[0] <= eigenvals[1] <= eigenvals[2].
154 +         * @return an orthogonal matrix whose ith column is an
155 +         * eigenvector for the eigenvalue eigenvals[i]
156 +         */
157 +        SquareMatrix<Real, Dim>  findEigenvectors(Vector<Real, Dim>& eigenValues) {
158 +            SquareMatrix<Real, Dim> ortMat;
159 +            
160 +            if ( !isSymmetric()){
161 +                throw();
162 +            }
163 +            
164 +            SquareMatrix<Real, Dim> m(*this);
165 +            jacobi(m, eigenValues, ortMat);
166 +
167 +            return ortMat;
168 +        }
169 +        /**
170 +         * Jacobi iteration routines for computing eigenvalues/eigenvectors of
171 +         * real symmetric matrix
172 +         *
173 +         * @return true if success, otherwise return false
174 +         * @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;
200 +    }
201 +    
202 +    for (ip=0; ip<N; ip++) {
203 +        b(ip) = w(ip) = a(ip, ip);
204 +        z(ip) = 0.0;
205 +    }
206 +
207 +    // begin rotation sequence
208 +    for (i=0; i<MAX_ROTATIONS; i++) {
209 +        sm = 0.0;
210 +        for (ip=0; ip<2; ip++) {
211 +            for (iq=ip+1; iq<N; iq++)
212 +                sm += fabs(a(ip, iq));
213 +        }
214 +        
215 +        if (sm == 0.0)
216 +            break;
217 +
218 +        if (i < 4)
219 +            tresh = 0.2*sm/(9);
220 +        else
221 +            tresh = 0.0;
222 +
223 +        for (ip=0; ip<2; ip++) {
224 +            for (iq=ip+1; iq<N; iq++) {
225 +                g = 100.0*fabs(a(ip, iq));
226 +                if (i > 4 && (fabs(w(ip))+g) == fabs(w(ip))
227 +                    && (fabs(w(iq))+g) == fabs(w(iq))) {
228 +                    a(ip, iq) = 0.0;
229 +                } 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));
236 +
237 +                        if (theta < 0.0)
238 +                            t = -t;
239 +                    }
240 +
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);
253 +
254 +                    for (j=ip+1;j<iq-1;j++)
255 +                        ROT(a,ip,j,j,iq);
256 +
257 +                    for (j=iq+1; j<N; j++)
258 +                        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++)
264 +
265 +        for (ip=0; ip<N; ip++) {
266 +            b(ip) += z(ip);
267 +            w(ip) = b(ip);
268 +            z(ip) = 0.0;
269 +        }
270 +        
271 +    } // end for (i=0; i<MAX_ROTATIONS; i++)
272 +
273 +    if ( i >= MAX_ROTATIONS )
274 +        return false;
275 +
276 +    // sort eigenfunctions
277 +    for (j=0; j<N; j++) {
278 +        k = j;
279 +        tmp = w(k);
280 +        for (i=j; i<N; i++) {
281 +            if (w(i) >= tmp) {
282 +            k = i;
283 +            tmp = w(k);
284 +            }
285 +        }
286 +    
287 +        if (k != j) {
288 +            w(k) = w(j);
289 +            w(j) = tmp;
290 +            for (i=0; i<N; i++)  {
291 +                tmp = v(i, j);
292 +                v(i, j) = v(i, k);
293 +                v(i, k) = tmp;
294 +            }
295 +        }
296 +    }
297 +
298 +    //    insure eigenvector consistency (i.e., Jacobi can compute
299 +    //    vectors that are negative of one another (.707,.707,0) and
300 +    //    (-.707,-.707,0). This can reek havoc in
301 +    //    hyperstreamline/other stuff. We will select the most
302 +    //    positive eigenvector.
303 +    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;
307 +    }
308 +
309 +    return true;
310   }
311 +
312 + #undef ROT
313 + #undef MAX_ROTATIONS
314 +
315 + }
316 +
317   #endif //MATH_SQUAREMATRIX_HPP

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines