ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-2.0/src/math/SquareMatrix.hpp
Revision: 1616
Committed: Wed Oct 20 18:07:08 2004 UTC (19 years, 8 months ago) by tim
File size: 11587 byte(s)
Log Message:
Math library pass the unit test

File Contents

# User Rev Content
1 tim 1563 /*
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.
23     *
24     */
25    
26     /**
27     * @file SquareMatrix.hpp
28     * @author Teng Lin
29     * @date 10/11/2004
30     * @version 1.0
31     */
32 tim 1616 #ifndef MATH_SQUAREMATRIX_HPP
33 tim 1563 #define MATH_SQUAREMATRIX_HPP
34    
35 tim 1567 #include "math/RectMatrix.hpp"
36 tim 1563
37     namespace oopse {
38    
39     /**
40     * @class SquareMatrix SquareMatrix.hpp "math/SquareMatrix.hpp"
41     * @brief A square matrix class
42     * @template Real the element type
43     * @template Dim the dimension of the square matrix
44     */
45     template<typename Real, int Dim>
46 tim 1567 class SquareMatrix : public RectMatrix<Real, Dim, Dim> {
47 tim 1563 public:
48    
49     /** default constructor */
50     SquareMatrix() {
51     for (unsigned int i = 0; i < Dim; i++)
52     for (unsigned int j = 0; j < Dim; j++)
53     data_[i][j] = 0.0;
54     }
55    
56     /** copy constructor */
57 tim 1567 SquareMatrix(const RectMatrix<Real, Dim, Dim>& m) : RectMatrix<Real, Dim, Dim>(m) {
58 tim 1563 }
59    
60     /** copy assignment operator */
61 tim 1567 SquareMatrix<Real, Dim>& operator =(const RectMatrix<Real, Dim, Dim>& m) {
62     RectMatrix<Real, Dim, Dim>::operator=(m);
63     return *this;
64 tim 1563 }
65 tim 1567
66     /** Retunrs an identity matrix*/
67 tim 1563
68 tim 1567 static SquareMatrix<Real, Dim> identity() {
69     SquareMatrix<Real, Dim> m;
70 tim 1563
71     for (unsigned int i = 0; i < Dim; i++)
72 tim 1567 for (unsigned int j = 0; j < Dim; j++)
73 tim 1563 if (i == j)
74 tim 1567 m(i, j) = 1.0;
75 tim 1563 else
76 tim 1567 m(i, j) = 0.0;
77    
78     return m;
79 tim 1563 }
80    
81 tim 1594 /**
82     * Retunrs the inversion of this matrix.
83 tim 1603 * @todo need implementation
84 tim 1594 */
85 tim 1567 SquareMatrix<Real, Dim> inverse() {
86     SquareMatrix<Real, Dim> result;
87    
88     return result;
89 tim 1569 }
90 tim 1563
91 tim 1594 /**
92     * Returns the determinant of this matrix.
93 tim 1603 * @todo need implementation
94 tim 1594 */
95 tim 1606 Real determinant() const {
96     Real det;
97 tim 1567 return det;
98 tim 1563 }
99    
100     /** Returns the trace of this matrix. */
101 tim 1606 Real trace() const {
102     Real tmp = 0;
103 tim 1563
104     for (unsigned int i = 0; i < Dim ; i++)
105     tmp += data_[i][i];
106    
107     return tmp;
108     }
109    
110     /** Tests if this matrix is symmetrix. */
111     bool isSymmetric() const {
112     for (unsigned int i = 0; i < Dim - 1; i++)
113     for (unsigned int j = i; j < Dim; j++)
114 tim 1567 if (fabs(data_[i][j] - data_[j][i]) > oopse::epsilon)
115 tim 1563 return false;
116    
117     return true;
118     }
119    
120 tim 1569 /** Tests if this matrix is orthogonal. */
121 tim 1567 bool isOrthogonal() {
122     SquareMatrix<Real, Dim> tmp;
123 tim 1563
124 tim 1567 tmp = *this * transpose();
125 tim 1563
126 tim 1569 return tmp.isDiagonal();
127 tim 1563 }
128    
129     /** Tests if this matrix is diagonal. */
130     bool isDiagonal() const {
131     for (unsigned int i = 0; i < Dim ; i++)
132     for (unsigned int j = 0; j < Dim; j++)
133 tim 1567 if (i !=j && fabs(data_[i][j]) > oopse::epsilon)
134 tim 1563 return false;
135    
136     return true;
137     }
138    
139     /** Tests if this matrix is the unit matrix. */
140     bool isUnitMatrix() const {
141     if (!isDiagonal())
142     return false;
143    
144     for (unsigned int i = 0; i < Dim ; i++)
145 tim 1567 if (fabs(data_[i][i] - 1) > oopse::epsilon)
146 tim 1563 return false;
147    
148     return true;
149 tim 1567 }
150 tim 1563
151 tim 1603 /** @todo need implementation */
152 tim 1569 void diagonalize() {
153 tim 1594 //jacobi(m, eigenValues, ortMat);
154 tim 1569 }
155    
156     /**
157     * Jacobi iteration routines for computing eigenvalues/eigenvectors of
158     * real symmetric matrix
159     *
160     * @return true if success, otherwise return false
161 tim 1616 * @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 tim 1569 */
167 tim 1616
168     static int jacobi(SquareMatrix<Real, Dim>& a, Vector<Real, Dim>& d,
169 tim 1569 SquareMatrix<Real, Dim>& v);
170 tim 1563 };//end SquareMatrix
171    
172 tim 1569
173 tim 1616 /*=========================================================================
174 tim 1569
175 tim 1616 Program: Visualization Toolkit
176     Module: $RCSfile: SquareMatrix.hpp,v $
177 tim 1569
178 tim 1616 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     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     =========================================================================*/
187    
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     #define VTK_MAX_ROTATIONS 20
192    
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     // 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     // 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 tim 1586 v(ip, ip) = 1.0;
223 tim 1616 }
224     for (ip=0; ip<n; ip++)
225     {
226     b[ip] = w[ip] = a(ip, ip);
227     z[ip] = 0.0;
228     }
229 tim 1569
230 tim 1616 // begin rotation sequence
231     for (i=0; i<VTK_MAX_ROTATIONS; i++)
232     {
233 tim 1586 sm = 0.0;
234 tim 1616 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 tim 1586 if (sm == 0.0)
242 tim 1616 {
243     break;
244     }
245 tim 1569
246 tim 1616 if (i < 3) // first 3 sweeps
247     {
248     tresh = 0.2*sm/(n*n);
249     }
250 tim 1586 else
251 tim 1616 {
252     tresh = 0.0;
253     }
254 tim 1569
255 tim 1616 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 tim 1569
261 tim 1616 // 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 tim 1569
293 tim 1616 // 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 tim 1586 }
298 tim 1616 // 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 tim 1586 }
314 tim 1616 }
315 tim 1586
316 tim 1616 for (ip=0; ip<n; ip++)
317     {
318     b[ip] += z[ip];
319     w[ip] = b[ip];
320     z[ip] = 0.0;
321     }
322 tim 1586 }
323    
324 tim 1616 //// 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 tim 1569
331 tim 1616 // sort eigenfunctions these changes do not affect accuracy
332     for (j=0; j<n-1; j++) // boundary incorrect
333     {
334 tim 1586 k = j;
335 tim 1616 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 tim 1586 k = i;
341 tim 1616 tmp = w[k];
342 tim 1586 }
343 tim 1616 }
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 tim 1586 }
356 tim 1616 // 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 tim 1586 }
369 tim 1616 }
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 tim 1586 }
379 tim 1569
380 tim 1616 if (n > 4)
381     {
382     delete [] b;
383     delete [] z;
384     }
385     return 1;
386 tim 1569 }
387    
388    
389     }
390 tim 1616 #endif //MATH_SQUAREMATRIX_HPP
391 tim 1569