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 "Vector3d.hpp" |
35 |
> |
#include "math/RectMatrix.hpp" |
36 |
|
|
37 |
|
namespace oopse { |
38 |
|
|
43 |
|
* @template Dim the dimension of the square matrix |
44 |
|
*/ |
45 |
|
template<typename Real, int Dim> |
46 |
< |
class SquareMatrix{ |
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 |
< |
/** Constructs and initializes every element of this matrix to a scalar */ |
59 |
< |
SquareMatrix(double s) { |
60 |
< |
for (unsigned int i = 0; i < Dim; i++) |
61 |
< |
for (unsigned int j = 0; j < Dim; j++) |
62 |
< |
data_[i][j] = s; |
63 |
< |
} |
58 |
> |
/** copy constructor */ |
59 |
> |
SquareMatrix(const RectMatrix<Real, Dim, Dim>& m) : RectMatrix<Real, Dim, Dim>(m) { |
60 |
> |
} |
61 |
> |
|
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 |
< |
/** copy constructor */ |
71 |
< |
SquareMatrix(const SquareMatrix<Real, Dim>& m) { |
72 |
< |
*this = m; |
73 |
< |
} |
74 |
< |
|
75 |
< |
/** destructor*/ |
76 |
< |
~SquareMatrix() {} |
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 |
< |
/** copy assignment operator */ |
81 |
< |
SquareMatrix<Real, Dim>& operator =(const SquareMatrix<Real, Dim>& m) { |
73 |
< |
for (unsigned int i = 0; i < Dim; i++) |
74 |
< |
for (unsigned int j = 0; j < Dim; j++) |
75 |
< |
data_[i][j] = m.data_[i][j]; |
76 |
< |
} |
77 |
< |
|
78 |
< |
/** |
79 |
< |
* Return the reference of a single element of this matrix. |
80 |
< |
* @return the reference of a single element of this matrix |
81 |
< |
* @param i row index |
82 |
< |
* @param j colum index |
83 |
< |
*/ |
84 |
< |
double& operator()(unsigned int i, unsigned int j) { |
85 |
< |
return data_[i][j]; |
86 |
< |
} |
80 |
> |
return m; |
81 |
> |
} |
82 |
|
|
83 |
< |
/** |
84 |
< |
* Return the value of a single element of this matrix. |
85 |
< |
* @return the value of a single element of this matrix |
86 |
< |
* @param i row index |
87 |
< |
* @param j colum index |
88 |
< |
*/ |
94 |
< |
double operator()(unsigned int i, unsigned int j) const { |
95 |
< |
return data_[i][j]; |
96 |
< |
} |
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 a row of this matrix as a vector. |
100 |
< |
* @return a row of this matrix as a vector |
101 |
< |
* @param row the row index |
102 |
< |
*/ |
103 |
< |
Vector<Real, Dim> getRow(unsigned int row) { |
104 |
< |
Vector<Real, Dim> v; |
90 |
> |
return result; |
91 |
> |
} |
92 |
|
|
93 |
< |
for (unsigned int i = 0; i < Dim; i++) |
94 |
< |
v[i] = data_[row][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 v; |
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 |
< |
/** |
110 |
< |
* Sets a row of this matrix |
114 |
< |
* @param row the row index |
115 |
< |
* @param v the vector to be set |
116 |
< |
*/ |
117 |
< |
void setRow(unsigned int row, const Vector<Real, Dim>& v) { |
118 |
< |
Vector<Real, Dim> v; |
109 |
> |
return tmp; |
110 |
> |
} |
111 |
|
|
112 |
< |
for (unsigned int i = 0; i < Dim; i++) |
113 |
< |
data_[row][i] = v[i]; |
114 |
< |
} |
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 |
< |
/** |
123 |
< |
* Returns a column of this matrix as a vector. |
124 |
< |
* @return a column of this matrix as a vector |
127 |
< |
* @param col the column index |
128 |
< |
*/ |
129 |
< |
Vector<Real, Dim> getColum(unsigned int col) { |
130 |
< |
Vector<Real, Dim> v; |
122 |
> |
/** Tests if this matrix is orthogonal. */ |
123 |
> |
bool isOrthogonal() { |
124 |
> |
SquareMatrix<Real, Dim> tmp; |
125 |
|
|
126 |
< |
for (unsigned int i = 0; i < Dim; i++) |
133 |
< |
v[i] = data_[i][col]; |
126 |
> |
tmp = *this * transpose(); |
127 |
|
|
128 |
< |
return v; |
129 |
< |
} |
128 |
> |
return tmp.isDiagonal(); |
129 |
> |
} |
130 |
|
|
131 |
< |
/** |
132 |
< |
* Sets a column of this matrix |
133 |
< |
* @param col the column index |
134 |
< |
* @param v the vector to be set |
135 |
< |
*/ |
136 |
< |
void setColum(unsigned int col, const Vector<Real, Dim>& v){ |
137 |
< |
Vector<Real, Dim> v; |
138 |
< |
|
146 |
< |
for (unsigned int i = 0; i < Dim; i++) |
147 |
< |
data_[i][col] = v[i]; |
148 |
< |
} |
149 |
< |
|
150 |
< |
/** Negates the value of this matrix in place. */ |
151 |
< |
inline void negate() { |
152 |
< |
for (unsigned int i = 0; i < Dim; i++) |
153 |
< |
for (unsigned int j = 0; j < Dim; j++) |
154 |
< |
data_[i][j] = -data_[i][j]; |
155 |
< |
} |
156 |
< |
|
157 |
< |
/** |
158 |
< |
* Sets the value of this matrix to the negation of matrix m. |
159 |
< |
* @param m the source matrix |
160 |
< |
*/ |
161 |
< |
inline void negate(const SquareMatrix<Real, Dim>& m) { |
162 |
< |
for (unsigned int i = 0; i < Dim; i++) |
163 |
< |
for (unsigned int j = 0; j < Dim; j++) |
164 |
< |
data_[i][j] = -m.data_[i][j]; |
165 |
< |
} |
166 |
< |
|
167 |
< |
/** |
168 |
< |
* Sets the value of this matrix to the sum of itself and m (*this += m). |
169 |
< |
* @param m the other matrix |
170 |
< |
*/ |
171 |
< |
inline void add( const SquareMatrix<Real, Dim>& m ) { |
172 |
< |
for (unsigned int i = 0; i < Dim; i++) |
173 |
< |
for (unsigned int j = 0; j < Dim; j++) |
174 |
< |
data_[i][j] += m.data_[i][j]; |
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 |
< |
/** |
142 |
< |
* Sets the value of this matrix to the sum of m1 and m2 (*this = m1 + m2). |
143 |
< |
* @param m1 the first matrix |
144 |
< |
* @param m2 the second matrix |
145 |
< |
*/ |
146 |
< |
inline void add( const SquareMatrix<Real, Dim>& m1, const SquareMatrix<Real, Dim>& m2 ) { |
147 |
< |
for (unsigned int i = 0; i < Dim; i++) |
148 |
< |
for (unsigned int j = 0; j < Dim; j++) |
149 |
< |
data_[i][j] = m1.data_[i][j] + m2.data_[i][j]; |
140 |
> |
|
141 |
> |
/** Tests if this matrix is the unit matrix. */ |
142 |
> |
bool isUnitMatrix() const { |
143 |
> |
if (!isDiagonal()) |
144 |
> |
return false; |
145 |
> |
|
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 |
< |
/** |
218 |
< |
* Sets the value of this matrix to the difference of itself and m (*this -= m). |
219 |
< |
* @param m the other matrix |
220 |
< |
*/ |
221 |
< |
inline void sub( const SquareMatrix<Real, Dim>& m ) { |
222 |
< |
for (unsigned int i = 0; i < Dim; i++) |
223 |
< |
for (unsigned int j = 0; j < Dim; j++) |
224 |
< |
data_[i][j] -= m.data_[i][j]; |
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 |
< |
|
227 |
< |
/** |
228 |
< |
* Sets the value of this matrix to the difference of matrix m1 and m2 (*this = m1 - m2). |
229 |
< |
* @param m1 the first matrix |
201 |
< |
* @param m2 the second matrix |
202 |
< |
*/ |
203 |
< |
inline void sub( const SquareMatrix<Real, Dim>& m1, const Vector &m2){ |
204 |
< |
for (unsigned int i = 0; i < Dim; i++) |
205 |
< |
for (unsigned int j = 0; j < Dim; j++) |
206 |
< |
data_[i][j] = m1.data_[i][j] - m2.data_[i][j]; |
226 |
> |
for (ip=0; ip<n; ip++) |
227 |
> |
{ |
228 |
> |
b[ip] = w[ip] = a(ip, ip); |
229 |
> |
z[ip] = 0.0; |
230 |
|
} |
208 |
– |
|
209 |
– |
/** |
210 |
– |
* Sets the value of this matrix to the scalar multiplication of itself (*this *= s). |
211 |
– |
* @param s the scalar value |
212 |
– |
*/ |
213 |
– |
inline void mul( double s ) { |
214 |
– |
for (unsigned int i = 0; i < Dim; i++) |
215 |
– |
for (unsigned int j = 0; j < Dim; j++) |
216 |
– |
data_[i][j] *= s; |
217 |
– |
} |
231 |
|
|
232 |
< |
/** |
233 |
< |
* Sets the value of this matrix to the scalar multiplication of matrix m (*this = s * m). |
234 |
< |
* @param s the scalar value |
235 |
< |
* @param m the matrix |
236 |
< |
*/ |
237 |
< |
inline void mul( double s, const SquareMatrix<Real, Dim>& m ) { |
238 |
< |
for (unsigned int i = 0; i < Dim; i++) |
239 |
< |
for (unsigned int j = 0; j < Dim; j++) |
240 |
< |
data_[i][j] = s * m.data_[i][j]; |
241 |
< |
} |
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 |
< |
/** |
249 |
< |
* Sets the value of this matrix to the multiplication of this matrix and matrix m |
250 |
< |
* (*this = *this * m). |
251 |
< |
* @param m the matrix |
252 |
< |
*/ |
253 |
< |
inline void mul(const SquareMatrix<Real, Dim>& m ) { |
254 |
< |
SquareMatrix<Real, Dim> tmp(*this); |
255 |
< |
|
256 |
< |
for (unsigned int i = 0; i < Dim; i++) |
257 |
< |
for (unsigned int j = 0; j < Dim; j++) { |
258 |
< |
|
259 |
< |
data_[i][j] = 0.0; |
260 |
< |
for (unsigned int k = 0; k < Dim; k++) |
261 |
< |
data_[i][j] = tmp.data_[i][k] * m.data_[k][j] |
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 |
< |
} |
277 |
< |
|
278 |
< |
/** |
279 |
< |
* Sets the value of this matrix to the left multiplication of matrix m into itself |
280 |
< |
* (*this = m * *this). |
281 |
< |
* @param m the matrix |
282 |
< |
*/ |
283 |
< |
inline void leftmul(const SquareMatrix<Real, Dim>& m ) { |
253 |
< |
SquareMatrix<Real, Dim> tmp(*this); |
254 |
< |
|
255 |
< |
for (unsigned int i = 0; i < Dim; i++) |
256 |
< |
for (unsigned int j = 0; j < Dim; j++) { |
257 |
< |
|
258 |
< |
data_[i][j] = 0.0; |
259 |
< |
for (unsigned int k = 0; k < Dim; k++) |
260 |
< |
data_[i][j] = m.data_[i][k] * tmp.data_[k][j] |
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 |
< |
} |
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 |
< |
/** |
296 |
< |
* Sets the value of this matrix to the multiplication of matrix m1 and matrix m2 |
297 |
< |
* (*this = m1 * m2). |
298 |
< |
* @param m1 the first matrix |
268 |
< |
* @param m2 the second matrix |
269 |
< |
*/ |
270 |
< |
inline void mul(const SquareMatrix<Real, Dim>& m1, |
271 |
< |
const SquareMatrix<Real, Dim>& m2 ) { |
272 |
< |
for (unsigned int i = 0; i < Dim; i++) |
273 |
< |
for (unsigned int j = 0; j < Dim; j++) { |
274 |
< |
|
275 |
< |
data_[i][j] = 0.0; |
276 |
< |
for (unsigned int k = 0; k < Dim; k++) |
277 |
< |
data_[i][j] = m1.data_[i][k] * m2.data_[k][j] |
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 |
|
} |
281 |
– |
|
282 |
– |
/** |
283 |
– |
* Sets the value of this matrix to the scalar division of itself (*this /= s ). |
284 |
– |
* @param s the scalar value |
285 |
– |
*/ |
286 |
– |
inline void div( double s) { |
287 |
– |
for (unsigned int i = 0; i < Dim; i++) |
288 |
– |
for (unsigned int j = 0; j < Dim; j++) |
289 |
– |
data_[i][j] /= s; |
290 |
– |
} |
291 |
– |
|
292 |
– |
inline SquareMatrix<Real, Dim>& operator=(const SquareMatrix<Real, Dim>& v) { |
293 |
– |
if (this == &v) |
294 |
– |
return *this; |
295 |
– |
|
296 |
– |
for (unsigned int i = 0; i < Dim; i++) |
297 |
– |
data_[i] = v[i]; |
298 |
– |
|
299 |
– |
return *this; |
300 |
– |
} |
301 |
– |
|
302 |
– |
/** |
303 |
– |
* Sets the value of this matrix to the scalar division of matrix v1 (*this = v1 / s ). |
304 |
– |
* @paran v1 the source matrix |
305 |
– |
* @param s the scalar value |
306 |
– |
*/ |
307 |
– |
inline void div( const SquareMatrix<Real, Dim>& v1, double s ) { |
308 |
– |
for (unsigned int i = 0; i < Dim; i++) |
309 |
– |
data_[i] = v1.data_[i] / s; |
310 |
– |
} |
325 |
|
|
326 |
< |
/** |
327 |
< |
* Multiples a scalar into every element of this matrix. |
328 |
< |
* @param s the scalar value |
329 |
< |
*/ |
330 |
< |
SquareMatrix<Real, Dim>& operator *=(const double s) { |
317 |
< |
this->mul(s); |
318 |
< |
return *this; |
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 |
< |
/** |
334 |
< |
* Divides every element of this matrix by a scalar. |
335 |
< |
* @param s the scalar value |
336 |
< |
*/ |
337 |
< |
SquareMatrix<Real, Dim>& operator /=(const double s) { |
338 |
< |
this->div(s); |
339 |
< |
return *this; |
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 |
< |
|
359 |
< |
/** |
360 |
< |
* Sets the value of this matrix to the sum of the other matrix and itself (*this += m). |
361 |
< |
* @param m the other matrix |
362 |
< |
*/ |
363 |
< |
SquareMatrix<Real, Dim>& operator += (const SquareMatrix<Real, Dim>& m) { |
364 |
< |
add(m); |
365 |
< |
return *this; |
366 |
< |
} |
367 |
< |
|
368 |
< |
/** |
369 |
< |
* Sets the value of this matrix to the differerence of itself and the other matrix (*this -= m) |
341 |
< |
* @param m the other matrix |
342 |
< |
*/ |
343 |
< |
SquareMatrix<Real, Dim>& operator -= (const SquareMatrix<Real, Dim>& m){ |
344 |
< |
sub(m); |
345 |
< |
return *this; |
346 |
< |
} |
347 |
< |
|
348 |
< |
/** set this matrix to an identity matrix*/ |
349 |
< |
|
350 |
< |
void identity() { |
351 |
< |
for (unsigned int i = 0; i < Dim; i++) |
352 |
< |
for (unsigned int i = 0; i < Dim; i++) |
353 |
< |
if (i == j) |
354 |
< |
data_[i][j] = 1.0; |
355 |
< |
else |
356 |
< |
data_[i][j] = 0.0; |
357 |
< |
} |
358 |
< |
|
359 |
< |
/** Sets the value of this matrix to the inversion of itself. */ |
360 |
< |
void inverse() { |
361 |
< |
inverse(*this); |
362 |
< |
} |
363 |
< |
|
364 |
< |
/** |
365 |
< |
* Sets the value of this matrix to the inversion of other matrix. |
366 |
< |
* @ param m the source matrix |
367 |
< |
*/ |
368 |
< |
void inverse(const SquareMatrix<Real, Dim>& m); |
369 |
< |
|
370 |
< |
/** Sets the value of this matrix to the transpose of itself. */ |
371 |
< |
void transpose() { |
372 |
< |
for (unsigned int i = 0; i < Dim - 1; i++) |
373 |
< |
for (unsigned int j = i; j < Dim; j++) |
374 |
< |
std::swap(data_[i][j], data_[j][i]); |
375 |
< |
} |
376 |
< |
|
377 |
< |
/** |
378 |
< |
* Sets the value of this matrix to the transpose of other matrix. |
379 |
< |
* @ param m the source matrix |
380 |
< |
*/ |
381 |
< |
void transpose(const SquareMatrix<Real, Dim>& m) { |
382 |
< |
|
383 |
< |
if (this == &m) { |
384 |
< |
transpose(); |
385 |
< |
} else { |
386 |
< |
for (unsigned int i = 0; i < Dim; i++) |
387 |
< |
for (unsigned int j =0; j < Dim; j++) |
388 |
< |
data_[i][j] = m.data_[i][j]; |
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 |
< |
/** Returns the determinant of this matrix. */ |
383 |
< |
double determinant() const { |
384 |
< |
|
382 |
> |
if (n > 4) |
383 |
> |
{ |
384 |
> |
delete [] b; |
385 |
> |
delete [] z; |
386 |
|
} |
387 |
< |
|
397 |
< |
/** Returns the trace of this matrix. */ |
398 |
< |
double trace() const { |
399 |
< |
double tmp = 0; |
400 |
< |
|
401 |
< |
for (unsigned int i = 0; i < Dim ; i++) |
402 |
< |
tmp += data_[i][i]; |
403 |
< |
|
404 |
< |
return tmp; |
405 |
< |
} |
406 |
< |
|
407 |
< |
/** Tests if this matrix is symmetrix. */ |
408 |
< |
bool isSymmetric() const { |
409 |
< |
for (unsigned int i = 0; i < Dim - 1; i++) |
410 |
< |
for (unsigned int j = i; j < Dim; j++) |
411 |
< |
if (fabs(data_[i][j] - data_[j][i]) > epsilon) |
412 |
< |
return false; |
413 |
< |
|
414 |
< |
return true; |
415 |
< |
} |
416 |
< |
|
417 |
< |
/** Tests if this matrix is orthogona. */ |
418 |
< |
bool isOrthogonal() const { |
419 |
< |
SquareMatrix<Real, Dim> t(*this); |
420 |
< |
|
421 |
< |
t.transpose(); |
422 |
< |
|
423 |
< |
return isUnitMatrix(*this * t); |
424 |
< |
} |
425 |
< |
|
426 |
< |
/** Tests if this matrix is diagonal. */ |
427 |
< |
bool isDiagonal() const { |
428 |
< |
for (unsigned int i = 0; i < Dim ; i++) |
429 |
< |
for (unsigned int j = 0; j < Dim; j++) |
430 |
< |
if (i !=j && fabs(data_[i][j]) > epsilon) |
431 |
< |
return false; |
432 |
< |
|
433 |
< |
return true; |
434 |
< |
} |
435 |
< |
|
436 |
< |
/** Tests if this matrix is the unit matrix. */ |
437 |
< |
bool isUnitMatrix() const { |
438 |
< |
if (!isDiagonal()) |
439 |
< |
return false; |
440 |
< |
|
441 |
< |
for (unsigned int i = 0; i < Dim ; i++) |
442 |
< |
if (fabs(data_[i][i] - 1) > epsilon) |
443 |
< |
return false; |
444 |
< |
|
445 |
< |
return true; |
446 |
< |
} |
447 |
< |
|
448 |
< |
protected: |
449 |
< |
double data_[Dim][Dim]; /**< matrix element */ |
450 |
< |
|
451 |
< |
};//end SquareMatrix |
452 |
< |
|
453 |
< |
|
454 |
< |
/** Negate the value of every element of this matrix. */ |
455 |
< |
template<typename Real, int Dim> |
456 |
< |
inline SquareMatrix<Real, Dim> operator -(const SquareMatrix& m) { |
457 |
< |
SquareMatrix<Real, Dim> result(m); |
458 |
< |
|
459 |
< |
result.negate(); |
460 |
< |
|
461 |
< |
return result; |
387 |
> |
return 1; |
388 |
|
} |
463 |
– |
|
464 |
– |
/** |
465 |
– |
* Return the sum of two matrixes (m1 + m2). |
466 |
– |
* @return the sum of two matrixes |
467 |
– |
* @param m1 the first matrix |
468 |
– |
* @param m2 the second matrix |
469 |
– |
*/ |
470 |
– |
template<typename Real, int Dim> |
471 |
– |
inline SquareMatrix<Real, Dim> operator + (const SquareMatrix<Real, Dim>& m1, |
472 |
– |
const SquareMatrix<Real, Dim>& m2) { |
473 |
– |
SquareMatrix<Real, Dim>result; |
389 |
|
|
475 |
– |
result.add(m1, m2); |
390 |
|
|
477 |
– |
return result; |
478 |
– |
} |
479 |
– |
|
480 |
– |
/** |
481 |
– |
* Return the difference of two matrixes (m1 - m2). |
482 |
– |
* @return the sum of two matrixes |
483 |
– |
* @param m1 the first matrix |
484 |
– |
* @param m2 the second matrix |
485 |
– |
*/ |
486 |
– |
template<typename Real, int Dim> |
487 |
– |
inline SquareMatrix<Real, Dim> operator - (const SquareMatrix<Real, Dim>& m1, |
488 |
– |
const SquareMatrix<Real, Dim>& m2) { |
489 |
– |
SquareMatrix<Real, Dim>result; |
490 |
– |
|
491 |
– |
result.sub(m1, m2); |
492 |
– |
|
493 |
– |
return result; |
494 |
– |
} |
495 |
– |
|
496 |
– |
/** |
497 |
– |
* Return the multiplication of two matrixes (m1 * m2). |
498 |
– |
* @return the multiplication of two matrixes |
499 |
– |
* @param m1 the first matrix |
500 |
– |
* @param m2 the second matrix |
501 |
– |
*/ |
502 |
– |
template<typename Real, int Dim> |
503 |
– |
inline SquareMatrix<Real, Dim> operator *(const SquareMatrix<Real, Dim>& m1, |
504 |
– |
const SquareMatrix<Real, Dim>& m2) { |
505 |
– |
SquareMatrix<Real, Dim> result; |
506 |
– |
|
507 |
– |
result.mul(m1, m2); |
508 |
– |
|
509 |
– |
return result; |
510 |
– |
} |
511 |
– |
|
512 |
– |
/** |
513 |
– |
* Return the multiplication of matrixes m and vector v (m * v). |
514 |
– |
* @return the multiplication of matrixes and vector |
515 |
– |
* @param m the matrix |
516 |
– |
* @param v the vector |
517 |
– |
*/ |
518 |
– |
template<typename Real, int Dim> |
519 |
– |
inline Vector<Real, Dim> operator *(const SquareMatrix<Real, Dim>& m, |
520 |
– |
const SquareMatrix<Real, Dim>& v) { |
521 |
– |
Vector<Real, Dim> result; |
522 |
– |
|
523 |
– |
for (unsigned int i = 0; i < Dim ; i++) |
524 |
– |
for (unsigned int j = 0; j < Dim ; j++) |
525 |
– |
result[i] += m(i, j) * v[j]; |
526 |
– |
|
527 |
– |
return result; |
528 |
– |
} |
391 |
|
} |
392 |
|
#endif //MATH_SQUAREMATRIX_HPP |
393 |
+ |
|