# | 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 | < | /** |
81 | < | * Retunrs the inversion of this matrix. |
83 | < | * @todo need implementation |
84 | < | */ |
85 | < | SquareMatrix<Real, Dim> inverse() { |
86 | < | 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 | < | /** |
91 | < | * Returns the determinant of this matrix. |
93 | < | * @todo need implementation |
94 | < | */ |
95 | < | Real determinant() const { |
96 | < | Real det; |
97 | < | return det; |
98 | < | } |
90 | > | return result; |
91 | > | } |
92 | ||
93 | < | /** Returns the trace of this matrix. */ |
94 | < | Real trace() const { |
95 | < | Real tmp = 0; |
96 | < | |
97 | < | for (unsigned int i = 0; i < Dim ; i++) |
98 | < | tmp += data_[i][i]; |
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 | < | return tmp; |
103 | < | } |
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 | < | /** Tests if this matrix is symmetrix. */ |
110 | < | bool isSymmetric() const { |
112 | < | for (unsigned int i = 0; i < Dim - 1; i++) |
113 | < | for (unsigned int j = i; j < Dim; j++) |
114 | < | if (fabs(data_[i][j] - data_[j][i]) > oopse::epsilon) |
115 | < | return false; |
116 | < | |
117 | < | return true; |
118 | < | } |
109 | > | return tmp; |
110 | > | } |
111 | ||
112 | < | /** Tests if this matrix is orthogonal. */ |
113 | < | bool isOrthogonal() { |
114 | < | SquareMatrix<Real, Dim> tmp; |
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 | < | tmp = *this * transpose(); |
122 | > | /** Tests if this matrix is orthogonal. */ |
123 | > | bool isOrthogonal() { |
124 | > | SquareMatrix<Real, Dim> tmp; |
125 | ||
126 | < | return tmp.isDiagonal(); |
127 | < | } |
126 | > | tmp = *this * transpose(); |
127 | ||
128 | < | /** Tests if this matrix is diagonal. */ |
129 | < | bool isDiagonal() const { |
131 | < | for (unsigned int i = 0; i < Dim ; i++) |
132 | < | for (unsigned int j = 0; j < Dim; j++) |
133 | < | if (i !=j && fabs(data_[i][j]) > oopse::epsilon) |
134 | < | return false; |
135 | < | |
136 | < | return true; |
137 | < | } |
128 | > | return tmp.isDiagonal(); |
129 | > | } |
130 | ||
131 | < | /** Tests if this matrix is the unit matrix. */ |
132 | < | bool isUnitMatrix() const { |
133 | < | if (!isDiagonal()) |
134 | < | return false; |
135 | < | |
136 | < | for (unsigned int i = 0; i < Dim ; i++) |
137 | < | if (fabs(data_[i][i] - 1) > oopse::epsilon) |
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()) |
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 | < | } |
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); |
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 | ||
# | Line 197 | Line 199 | namespace oopse { | |
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; |
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]; |
212 | < | z = new Real[n]; |
210 | > | // only allocate memory if the matrix is large |
211 | > | if (n > 4) { |
212 | > | b = new Real[n]; |
213 | > | z = new Real[n]; |
214 | } | |
215 | ||
216 | < | // initialize |
217 | < | for (ip=0; ip<n; ip++) |
218 | < | { |
219 | < | for (iq=0; iq<n; iq++) |
220 | < | { |
221 | < | v(ip, iq) = 0.0; |
221 | < | } |
222 | < | v(ip, ip) = 1.0; |
216 | > | // initialize |
217 | > | for (ip=0; ip<n; ip++) { |
218 | > | for (iq=0; iq<n; iq++) { |
219 | > | v(ip, iq) = 0.0; |
220 | > | } |
221 | > | v(ip, ip) = 1.0; |
222 | } | |
223 | < | for (ip=0; ip<n; ip++) |
224 | < | { |
225 | < | b[ip] = w[ip] = a(ip, ip); |
227 | < | z[ip] = 0.0; |
223 | > | for (ip=0; ip<n; ip++) { |
224 | > | b[ip] = w[ip] = a(ip, ip); |
225 | > | z[ip] = 0.0; |
226 | } | |
227 | ||
228 | < | // begin rotation sequence |
229 | < | for (i=0; i<VTK_MAX_ROTATIONS; i++) |
230 | < | { |
231 | < | sm = 0.0; |
232 | < | for (ip=0; ip<n-1; ip++) |
233 | < | { |
234 | < | for (iq=ip+1; iq<n; iq++) |
237 | < | { |
238 | < | sm += fabs(a(ip, iq)); |
228 | > | // begin rotation sequence |
229 | > | for (i=0; i<VTK_MAX_ROTATIONS; i++) { |
230 | > | sm = 0.0; |
231 | > | for (ip=0; ip<n-1; ip++) { |
232 | > | for (iq=ip+1; iq<n; iq++) { |
233 | > | sm += fabs(a(ip, iq)); |
234 | > | } |
235 | } | |
236 | < | } |
237 | < | if (sm == 0.0) |
238 | < | { |
243 | < | break; |
244 | < | } |
236 | > | if (sm == 0.0) { |
237 | > | break; |
238 | > | } |
239 | ||
240 | < | if (i < 3) // first 3 sweeps |
241 | < | { |
242 | < | tresh = 0.2*sm/(n*n); |
243 | < | } |
244 | < | else |
251 | < | { |
252 | < | tresh = 0.0; |
253 | < | } |
240 | > | if (i < 3) { // first 3 sweeps |
241 | > | tresh = 0.2*sm/(n*n); |
242 | > | } else { |
243 | > | tresh = 0.0; |
244 | > | } |
245 | ||
246 | < | for (ip=0; ip<n-1; ip++) |
247 | < | { |
248 | < | for (iq=ip+1; iq<n; iq++) |
258 | < | { |
259 | < | g = 100.0*fabs(a(ip, iq)); |
246 | > | for (ip=0; ip<n-1; ip++) { |
247 | > | for (iq=ip+1; iq<n; iq++) { |
248 | > | g = 100.0*fabs(a(ip, iq)); |
249 | ||
250 | < | // after 4 sweeps |
251 | < | if (i > 3 && (fabs(w[ip])+g) == fabs(w[ip]) |
252 | < | && (fabs(w[iq])+g) == fabs(w[iq])) |
253 | < | { |
254 | < | a(ip, iq) = 0.0; |
255 | < | } |
256 | < | else if (fabs(a(ip, iq)) > tresh) |
257 | < | { |
258 | < | h = w[iq] - w[ip]; |
259 | < | if ( (fabs(h)+g) == fabs(h)) |
260 | < | { |
261 | < | t = (a(ip, iq)) / h; |
262 | < | } |
263 | < | else |
264 | < | { |
265 | < | theta = 0.5*h / (a(ip, iq)); |
266 | < | t = 1.0 / (fabs(theta)+sqrt(1.0+theta*theta)); |
267 | < | if (theta < 0.0) |
268 | < | { |
269 | < | t = -t; |
270 | < | } |
271 | < | } |
272 | < | c = 1.0 / sqrt(1+t*t); |
273 | < | 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; |
250 | > | // after 4 sweeps |
251 | > | if (i > 3 && (fabs(w[ip])+g) == fabs(w[ip]) |
252 | > | && (fabs(w[iq])+g) == fabs(w[iq])) { |
253 | > | a(ip, iq) = 0.0; |
254 | > | } else if (fabs(a(ip, iq)) > tresh) { |
255 | > | h = w[iq] - w[ip]; |
256 | > | if ( (fabs(h)+g) == fabs(h)) { |
257 | > | t = (a(ip, iq)) / h; |
258 | > | } else { |
259 | > | theta = 0.5*h / (a(ip, iq)); |
260 | > | t = 1.0 / (fabs(theta)+sqrt(1.0+theta*theta)); |
261 | > | if (theta < 0.0) { |
262 | > | t = -t; |
263 | > | } |
264 | > | } |
265 | > | c = 1.0 / sqrt(1+t*t); |
266 | > | s = t*c; |
267 | > | tau = s/(1.0+c); |
268 | > | h = t*a(ip, iq); |
269 | > | z[ip] -= h; |
270 | > | z[iq] += h; |
271 | > | w[ip] -= h; |
272 | > | w[iq] += h; |
273 | > | a(ip, iq)=0.0; |
274 | ||
275 | < | // ip already shifted left by 1 unit |
276 | < | for (j = 0;j <= ip-1;j++) |
277 | < | { |
278 | < | VTK_ROTATE(a,j,ip,j,iq); |
275 | > | // ip already shifted left by 1 unit |
276 | > | for (j = 0;j <= ip-1;j++) { |
277 | > | VTK_ROTATE(a,j,ip,j,iq); |
278 | > | } |
279 | > | // ip and iq already shifted left by 1 unit |
280 | > | for (j = ip+1;j <= iq-1;j++) { |
281 | > | VTK_ROTATE(a,ip,j,j,iq); |
282 | > | } |
283 | > | // iq already shifted left by 1 unit |
284 | > | for (j=iq+1; j<n; j++) { |
285 | > | VTK_ROTATE(a,ip,j,iq,j); |
286 | > | } |
287 | > | for (j=0; j<n; j++) { |
288 | > | VTK_ROTATE(v,j,ip,j,iq); |
289 | > | } |
290 | > | } |
291 | } | |
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 | – | } |
292 | } | |
314 | – | } |
293 | ||
294 | < | for (ip=0; ip<n; ip++) |
295 | < | { |
296 | < | b[ip] += z[ip]; |
297 | < | w[ip] = b[ip]; |
298 | < | z[ip] = 0.0; |
321 | < | } |
294 | > | for (ip=0; ip<n; ip++) { |
295 | > | b[ip] += z[ip]; |
296 | > | w[ip] = b[ip]; |
297 | > | z[ip] = 0.0; |
298 | > | } |
299 | } | |
300 | ||
301 | < | //// this is NEVER called |
302 | < | if ( i >= VTK_MAX_ROTATIONS ) |
303 | < | { |
304 | < | std::cout << "vtkMath::Jacobi: Error extracting eigenfunctions" << std::endl; |
328 | < | return 0; |
301 | > | //// this is NEVER called |
302 | > | if ( i >= VTK_MAX_ROTATIONS ) { |
303 | > | std::cout << "vtkMath::Jacobi: Error extracting eigenfunctions" << std::endl; |
304 | > | return 0; |
305 | } | |
306 | ||
307 | < | // sort eigenfunctions these changes do not affect accuracy |
308 | < | for (j=0; j<n-1; j++) // boundary incorrect |
309 | < | { |
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; |
307 | > | // sort eigenfunctions these changes do not affect accuracy |
308 | > | for (j=0; j<n-1; j++) { // boundary incorrect |
309 | > | k = j; |
310 | tmp = w[k]; | |
311 | + | for (i=j+1; i<n; i++) { // boundary incorrect, shifted already |
312 | + | if (w[i] >= tmp) { // why exchage if same? |
313 | + | k = i; |
314 | + | tmp = w[k]; |
315 | + | } |
316 | } | |
317 | < | } |
318 | < | if (k != j) |
319 | < | { |
320 | < | w[k] = w[j]; |
321 | < | w[j] = tmp; |
322 | < | for (i=0; i<n; i++) |
323 | < | { |
324 | < | tmp = v(i, j); |
351 | < | v(i, j) = v(i, k); |
352 | < | v(i, k) = tmp; |
317 | > | if (k != j) { |
318 | > | w[k] = w[j]; |
319 | > | w[j] = tmp; |
320 | > | for (i=0; i<n; i++) { |
321 | > | tmp = v(i, j); |
322 | > | v(i, j) = v(i, k); |
323 | > | v(i, k) = tmp; |
324 | > | } |
325 | } | |
354 | – | } |
326 | } | |
327 | < | // insure eigenvector consistency (i.e., Jacobi can compute vectors that |
328 | < | // are negative of one another (.707,.707,0) and (-.707,-.707,0). This can |
329 | < | // reek havoc in hyperstreamline/other stuff. We will select the most |
330 | < | // positive eigenvector. |
331 | < | int ceil_half_n = (n >> 1) + (n & 1); |
332 | < | for (j=0; j<n; j++) |
333 | < | { |
334 | < | for (numPos=0, i=0; i<n; i++) |
335 | < | { |
336 | < | if ( v(i, j) >= 0.0 ) |
366 | < | { |
367 | < | numPos++; |
327 | > | // insure eigenvector consistency (i.e., Jacobi can compute vectors that |
328 | > | // are negative of one another (.707,.707,0) and (-.707,-.707,0). This can |
329 | > | // reek havoc in hyperstreamline/other stuff. We will select the most |
330 | > | // positive eigenvector. |
331 | > | int ceil_half_n = (n >> 1) + (n & 1); |
332 | > | for (j=0; j<n; j++) { |
333 | > | for (numPos=0, i=0; i<n; i++) { |
334 | > | if ( v(i, j) >= 0.0 ) { |
335 | > | numPos++; |
336 | > | } |
337 | } | |
338 | < | } |
339 | < | // if ( numPos < ceil(double(n)/double(2.0)) ) |
340 | < | if ( numPos < ceil_half_n) |
341 | < | { |
342 | < | for(i=0; i<n; i++) |
374 | < | { |
375 | < | v(i, j) *= -1.0; |
338 | > | // if ( numPos < ceil(double(n)/double(2.0)) ) |
339 | > | if ( numPos < ceil_half_n) { |
340 | > | for (i=0; i<n; i++) { |
341 | > | v(i, j) *= -1.0; |
342 | > | } |
343 | } | |
377 | – | } |
344 | } | |
345 | ||
346 | < | if (n > 4) |
347 | < | { |
348 | < | delete [] b; |
383 | < | delete [] z; |
346 | > | if (n > 4) { |
347 | > | delete [] b; |
348 | > | delete [] z; |
349 | } | |
350 | < | return 1; |
350 | > | return 1; |
351 | } | |
352 | ||
353 |
– | Removed lines |
+ | Added lines |
< | Changed lines |
> | Changed lines |