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