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