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

Comparing trunk/OOPSE-4/src/math/SquareMatrix.hpp (file contents):
Revision 1576 by tim, Fri Oct 15 18:18:12 2004 UTC vs.
Revision 1616 by tim, Wed Oct 20 18:07:08 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 "math/RectMatrix.hpp"
# Line 78 | Line 78 | namespace oopse {
78              return m;
79          }
80  
81 <        /** Retunrs  the inversion of this matrix. */
81 >        /**
82 >         * Retunrs  the inversion of this matrix.
83 >         * @todo need implementation
84 >         */
85           SquareMatrix<Real, Dim>  inverse() {
86               SquareMatrix<Real, Dim> result;
87  
88               return result;
89          }        
90  
91 <        /** Returns the determinant of this matrix. */
92 <        double determinant() const {
93 <            double det;
91 >        /**
92 >         * Returns the determinant of this matrix.
93 >         * @todo need implementation
94 >         */
95 >        Real determinant() const {
96 >            Real det;
97              return det;
98          }
99  
100          /** Returns the trace of this matrix. */
101 <        double trace() const {
102 <           double tmp = 0;
101 >        Real trace() const {
102 >           Real tmp = 0;
103            
104              for (unsigned int i = 0; i < Dim ; i++)
105                  tmp += data_[i][i];
# Line 142 | Line 148 | namespace oopse {
148              return true;
149          }        
150  
151 +        /** @todo need implementation */
152          void diagonalize() {
153 <            jacobi(m, eigenValues, ortMat);
153 >            //jacobi(m, eigenValues, ortMat);
154          }
155  
156          /**
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        /**
157           * Jacobi iteration routines for computing eigenvalues/eigenvectors of
158           * real symmetric matrix
159           *
160           * @return true if success, otherwise return false
161 <         * @param a source matrix
162 <         * @param w output eigenvalues
163 <         * @param v output eigenvectors
161 >         * @param a symmetric matrix whose eigenvectors are to be computed. On return, the matrix is
162 >         *     overwritten
163 >         * @param w will contain the eigenvalues of the matrix On return of this function
164 >         * @param v the columns of this matrix will contain the eigenvectors. The eigenvectors are
165 >         *    normalized and mutually orthogonal.
166           */
167 <        bool jacobi(const SquareMatrix<Real, Dim>& a, Vector<Real, Dim>& w,
167 >      
168 >        static int jacobi(SquareMatrix<Real, Dim>& a, Vector<Real, Dim>& d,
169                                SquareMatrix<Real, Dim>& v);
170      };//end SquareMatrix
171  
172  
173 < #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
173 > /*=========================================================================
174  
175 < template<typename Real, int Dim>
176 < 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;
175 >  Program:   Visualization Toolkit
176 >  Module:    $RCSfile: SquareMatrix.hpp,v $
177  
178 <    // initialize
179 <    for (ip=0; ip<N; ip++)
180 <    {
198 <        for (iq=0; iq<N; iq++) v(ip, iq) = 0.0;
199 <        v(ip, ip) = 1.0;
200 <    }
201 <    for (ip=0; ip<N; ip++)
202 <    {
203 <        b(ip) = w(ip) = a(ip, ip);
204 <        z(ip) = 0.0;
205 <    }
178 >  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
179 >  All rights reserved.
180 >  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
181  
182 <    // begin rotation sequence
183 <    for (i=0; i<MAX_ROTATIONS; i++)
184 <    {
210 <        sm = 0.0;
211 <        for (ip=0; ip<2; ip++)
212 <        {
213 <            for (iq=ip+1; iq<N; iq++) sm += fabs(a(ip, iq));
214 <        }
215 <        if (sm == 0.0) break;
182 >     This software is distributed WITHOUT ANY WARRANTY; without even
183 >     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
184 >     PURPOSE.  See the above copyright notice for more information.
185  
186 <        if (i < 4) tresh = 0.2*sm/(9);
218 <        else tresh = 0.0;
186 > =========================================================================*/
187  
188 <        for (ip=0; ip<2; ip++)
189 <        {
222 <            for (iq=ip+1; iq<N; iq++)
223 <            {
224 <                g = 100.0*fabs(a(ip, iq));
225 <                if (i > 4 && (fabs(w(ip))+g) == fabs(w(ip))
226 <                    && (fabs(w(iq))+g) == fabs(w(iq)))
227 <                {
228 <                    a(ip, iq) = 0.0;
229 <                }
230 <                else if (fabs(a(ip, iq)) > tresh)
231 <                {
232 <                    h = w(iq) - w(ip);
233 <                    if ( (fabs(h)+g) == fabs(h)) t = (a(ip, iq)) / h;
234 <                    else
235 <                    {
236 <                        theta = 0.5*h / (a(ip, iq));
237 <                        t = 1.0 / (fabs(theta)+sqrt(1.0+theta*theta));
238 <                        if (theta < 0.0) t = -t;
239 <                    }
240 <                    c = 1.0 / sqrt(1+t*t);
241 <                    s = t*c;
242 <                    tau = s/(1.0+c);
243 <                    h = t*a(ip, iq);
244 <                    z(ip) -= h;
245 <                    z(iq) += h;
246 <                    w(ip) -= h;
247 <                    w(iq) += h;
248 <                    a(ip, iq)=0.0;
249 <                    for (j=0;j<ip-1;j++)
250 <                    {
251 <                        ROT(a,j,ip,j,iq);
252 <                    }
253 <                    for (j=ip+1;j<iq-1;j++)
254 <                    {
255 <                        ROT(a,ip,j,j,iq);
256 <                    }
257 <                    for (j=iq+1; j<N; j++)
258 <                    {
259 <                        ROT(a,ip,j,iq,j);
260 <                    }
261 <                    for (j=0; j<N; j++)
262 <                    {
263 <                        ROT(v,j,ip,j,iq);
264 <                    }
265 <                }
266 <            }
267 <        }
188 > #define VTK_ROTATE(a,i,j,k,l) g=a(i, j);h=a(k, l);a(i, j)=g-s*(h+g*tau);\
189 >        a(k, l)=h+s*(g-h*tau)
190  
191 <        for (ip=0; ip<N; ip++)
270 <        {
271 <            b(ip) += z(ip);
272 <            w(ip) = b(ip);
273 <            z(ip) = 0.0;
274 <        }
275 <    }
191 > #define VTK_MAX_ROTATIONS 20
192  
193 <    if ( i >= MAX_ROTATIONS )
194 <        return false;
193 >    // Jacobi iteration for the solution of eigenvectors/eigenvalues of a nxn
194 >    // real symmetric matrix. Square nxn matrix a; size of matrix in n;
195 >    // output eigenvalues in w; and output eigenvectors in v. Resulting
196 >    // eigenvalues/vectors are sorted in decreasing order; eigenvectors are
197 >    // normalized.
198 >    template<typename Real, int Dim>
199 >    int SquareMatrix<Real, Dim>::jacobi(SquareMatrix<Real, Dim>& a, Vector<Real, Dim>& w,
200 >                                  SquareMatrix<Real, Dim>& v) {
201 >      const int n = Dim;  
202 >      int i, j, k, iq, ip, numPos;
203 >      Real tresh, theta, tau, t, sm, s, h, g, c, tmp;
204 >      Real bspace[4], zspace[4];
205 >      Real *b = bspace;
206 >      Real *z = zspace;
207  
208 <    // sort eigenfunctions
209 <    for (j=0; j<N; j++)
210 <    {
211 <        k = j;
212 <        tmp = w(k);
213 <        for (i=j; i<N; i++)
286 <        {
287 <            if (w(i) >= tmp)
288 <            {
289 <                k = i;
290 <                tmp = w(k);
291 <            }
292 <        }
293 <        if (k != j)
294 <        {
295 <            w(k) = w(j);
296 <            w(j) = tmp;
297 <            for (i=0; i<N; i++)
298 <            {
299 <                tmp = v(i, j);
300 <                v(i, j) = v(i, k);
301 <                v(i, k) = tmp;
302 <            }
303 <        }
304 <    }
208 >      // only allocate memory if the matrix is large
209 >      if (n > 4)
210 >        {
211 >        b = new Real[n];
212 >        z = new Real[n];
213 >        }
214  
215 <    //    insure eigenvector consistency (i.e., Jacobi can compute
216 <    //    vectors that are negative of one another (.707,.707,0) and
217 <    //    (-.707,-.707,0). This can reek havoc in
218 <    //    hyperstreamline/other stuff. We will select the most
219 <    //    positive eigenvector.
220 <    int numPos;
221 <    for (j=0; j<N; j++)
222 <    {
223 <        for (numPos=0, i=0; i<N; i++) if ( v(i, j) >= 0.0 ) numPos++;
224 <        if ( numPos < 2 ) for(i=0; i<N; i++) v(i, j) *= -1.0;
225 <    }
215 >      // initialize
216 >      for (ip=0; ip<n; ip++)
217 >        {
218 >        for (iq=0; iq<n; iq++)
219 >          {
220 >          v(ip, iq) = 0.0;
221 >          }
222 >        v(ip, ip) = 1.0;
223 >        }
224 >      for (ip=0; ip<n; ip++)
225 >        {
226 >        b[ip] = w[ip] = a(ip, ip);
227 >        z[ip] = 0.0;
228 >        }
229  
230 <    return true;
231 < }
230 >      // begin rotation sequence
231 >      for (i=0; i<VTK_MAX_ROTATIONS; i++)
232 >        {
233 >        sm = 0.0;
234 >        for (ip=0; ip<n-1; ip++)
235 >          {
236 >          for (iq=ip+1; iq<n; iq++)
237 >            {
238 >            sm += fabs(a(ip, iq));
239 >            }
240 >          }
241 >        if (sm == 0.0)
242 >          {
243 >          break;
244 >          }
245  
246 < #undef ROT
247 < #undef MAX_ROTATIONS
246 >        if (i < 3)                                // first 3 sweeps
247 >          {
248 >          tresh = 0.2*sm/(n*n);
249 >          }
250 >        else
251 >          {
252 >          tresh = 0.0;
253 >          }
254  
255 < }
255 >        for (ip=0; ip<n-1; ip++)
256 >          {
257 >          for (iq=ip+1; iq<n; iq++)
258 >            {
259 >            g = 100.0*fabs(a(ip, iq));
260  
261 +            // after 4 sweeps
262 +            if (i > 3 && (fabs(w[ip])+g) == fabs(w[ip])
263 +            && (fabs(w[iq])+g) == fabs(w[iq]))
264 +              {
265 +              a(ip, iq) = 0.0;
266 +              }
267 +            else if (fabs(a(ip, iq)) > tresh)
268 +              {
269 +              h = w[iq] - w[ip];
270 +              if ( (fabs(h)+g) == fabs(h))
271 +                {
272 +                t = (a(ip, iq)) / h;
273 +                }
274 +              else
275 +                {
276 +                theta = 0.5*h / (a(ip, iq));
277 +                t = 1.0 / (fabs(theta)+sqrt(1.0+theta*theta));
278 +                if (theta < 0.0)
279 +                  {
280 +                  t = -t;
281 +                  }
282 +                }
283 +              c = 1.0 / sqrt(1+t*t);
284 +              s = t*c;
285 +              tau = s/(1.0+c);
286 +              h = t*a(ip, iq);
287 +              z[ip] -= h;
288 +              z[iq] += h;
289 +              w[ip] -= h;
290 +              w[iq] += h;
291 +              a(ip, iq)=0.0;
292 +
293 +              // ip already shifted left by 1 unit
294 +              for (j = 0;j <= ip-1;j++)
295 +                {
296 +                VTK_ROTATE(a,j,ip,j,iq);
297 +                }
298 +              // ip and iq already shifted left by 1 unit
299 +              for (j = ip+1;j <= iq-1;j++)
300 +                {
301 +                VTK_ROTATE(a,ip,j,j,iq);
302 +                }
303 +              // iq already shifted left by 1 unit
304 +              for (j=iq+1; j<n; j++)
305 +                {
306 +                VTK_ROTATE(a,ip,j,iq,j);
307 +                }
308 +              for (j=0; j<n; j++)
309 +                {
310 +                VTK_ROTATE(v,j,ip,j,iq);
311 +                }
312 +              }
313 +            }
314 +          }
315 +
316 +        for (ip=0; ip<n; ip++)
317 +          {
318 +          b[ip] += z[ip];
319 +          w[ip] = b[ip];
320 +          z[ip] = 0.0;
321 +          }
322 +        }
323 +
324 +      //// this is NEVER called
325 +      if ( i >= VTK_MAX_ROTATIONS )
326 +        {
327 +           std::cout << "vtkMath::Jacobi: Error extracting eigenfunctions" << std::endl;
328 +           return 0;
329 +        }
330 +
331 +      // sort eigenfunctions                 these changes do not affect accuracy
332 +      for (j=0; j<n-1; j++)                  // boundary incorrect
333 +        {
334 +        k = j;
335 +        tmp = w[k];
336 +        for (i=j+1; i<n; i++)                // boundary incorrect, shifted already
337 +          {
338 +          if (w[i] >= tmp)                   // why exchage if same?
339 +            {
340 +            k = i;
341 +            tmp = w[k];
342 +            }
343 +          }
344 +        if (k != j)
345 +          {
346 +          w[k] = w[j];
347 +          w[j] = tmp;
348 +          for (i=0; i<n; i++)
349 +            {
350 +            tmp = v(i, j);
351 +            v(i, j) = v(i, k);
352 +            v(i, k) = tmp;
353 +            }
354 +          }
355 +        }
356 +      // insure eigenvector consistency (i.e., Jacobi can compute vectors that
357 +      // are negative of one another (.707,.707,0) and (-.707,-.707,0). This can
358 +      // reek havoc in hyperstreamline/other stuff. We will select the most
359 +      // positive eigenvector.
360 +      int ceil_half_n = (n >> 1) + (n & 1);
361 +      for (j=0; j<n; j++)
362 +        {
363 +        for (numPos=0, i=0; i<n; i++)
364 +          {
365 +          if ( v(i, j) >= 0.0 )
366 +            {
367 +            numPos++;
368 +            }
369 +          }
370 +    //    if ( numPos < ceil(double(n)/double(2.0)) )
371 +        if ( numPos < ceil_half_n)
372 +          {
373 +          for(i=0; i<n; i++)
374 +            {
375 +            v(i, j) *= -1.0;
376 +            }
377 +          }
378 +        }
379 +
380 +      if (n > 4)
381 +        {
382 +        delete [] b;
383 +        delete [] z;
384 +        }
385 +      return 1;
386 +    }
387 +
388 +
389 + }
390   #endif //MATH_SQUAREMATRIX_HPP
391 +

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines