ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-2.0/src/math/SquareMatrix.hpp
Revision: 1576
Committed: Fri Oct 15 18:18:12 2004 UTC (19 years, 8 months ago) by tim
File size: 8860 byte(s)
Log Message:
fix incompatible declaration in jacobi

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     #ifndef MATH_SQUAREMATRIX_HPP
33     #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 1567 /** Retunrs the inversion of this matrix. */
82     SquareMatrix<Real, Dim> inverse() {
83     SquareMatrix<Real, Dim> result;
84    
85     return result;
86 tim 1569 }
87 tim 1563
88     /** Returns the determinant of this matrix. */
89     double determinant() const {
90 tim 1567 double det;
91     return det;
92 tim 1563 }
93    
94     /** Returns the trace of this matrix. */
95     double trace() const {
96     double tmp = 0;
97    
98     for (unsigned int i = 0; i < Dim ; i++)
99     tmp += data_[i][i];
100    
101     return tmp;
102     }
103    
104     /** Tests if this matrix is symmetrix. */
105     bool isSymmetric() const {
106     for (unsigned int i = 0; i < Dim - 1; i++)
107     for (unsigned int j = i; j < Dim; j++)
108 tim 1567 if (fabs(data_[i][j] - data_[j][i]) > oopse::epsilon)
109 tim 1563 return false;
110    
111     return true;
112     }
113    
114 tim 1569 /** Tests if this matrix is orthogonal. */
115 tim 1567 bool isOrthogonal() {
116     SquareMatrix<Real, Dim> tmp;
117 tim 1563
118 tim 1567 tmp = *this * transpose();
119 tim 1563
120 tim 1569 return tmp.isDiagonal();
121 tim 1563 }
122    
123     /** Tests if this matrix is diagonal. */
124     bool isDiagonal() const {
125     for (unsigned int i = 0; i < Dim ; i++)
126     for (unsigned int j = 0; j < Dim; j++)
127 tim 1567 if (i !=j && fabs(data_[i][j]) > oopse::epsilon)
128 tim 1563 return false;
129    
130     return true;
131     }
132    
133     /** Tests if this matrix is the unit matrix. */
134     bool isUnitMatrix() const {
135     if (!isDiagonal())
136     return false;
137    
138     for (unsigned int i = 0; i < Dim ; i++)
139 tim 1567 if (fabs(data_[i][i] - 1) > oopse::epsilon)
140 tim 1563 return false;
141    
142     return true;
143 tim 1567 }
144 tim 1563
145 tim 1569 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 tim 1576 bool jacobi(const SquareMatrix<Real, Dim>& a, Vector<Real, Dim>& w,
179 tim 1569 SquareMatrix<Real, Dim>& v);
180 tim 1563 };//end SquareMatrix
181    
182 tim 1569
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 tim 1576 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 tim 1569 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     {
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     }
206    
207     // begin rotation sequence
208     for (i=0; i<MAX_ROTATIONS; i++)
209     {
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;
216    
217     if (i < 4) tresh = 0.2*sm/(9);
218     else tresh = 0.0;
219    
220     for (ip=0; ip<2; ip++)
221     {
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     }
268    
269     for (ip=0; ip<N; ip++)
270     {
271     b(ip) += z(ip);
272     w(ip) = b(ip);
273     z(ip) = 0.0;
274     }
275     }
276    
277     if ( i >= MAX_ROTATIONS )
278     return false;
279    
280     // sort eigenfunctions
281     for (j=0; j<N; j++)
282     {
283     k = j;
284     tmp = w(k);
285     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     }
305    
306     // insure eigenvector consistency (i.e., Jacobi can compute
307     // vectors that are negative of one another (.707,.707,0) and
308     // (-.707,-.707,0). This can reek havoc in
309     // hyperstreamline/other stuff. We will select the most
310     // positive eigenvector.
311     int numPos;
312     for (j=0; j<N; j++)
313     {
314     for (numPos=0, i=0; i<N; i++) if ( v(i, j) >= 0.0 ) numPos++;
315     if ( numPos < 2 ) for(i=0; i<N; i++) v(i, j) *= -1.0;
316     }
317    
318     return true;
319 tim 1563 }
320 tim 1569
321     #undef ROT
322     #undef MAX_ROTATIONS
323    
324     }
325    
326 tim 1563 #endif //MATH_SQUAREMATRIX_HPP