1 |
gezelter |
507 |
/* |
2 |
gezelter |
246 |
* Copyright (c) 2005 The University of Notre Dame. All Rights Reserved. |
3 |
tim |
70 |
* |
4 |
gezelter |
246 |
* The University of Notre Dame grants you ("Licensee") a |
5 |
|
|
* non-exclusive, royalty free, license to use, modify and |
6 |
|
|
* redistribute this software in source and binary code form, provided |
7 |
|
|
* that the following conditions are met: |
8 |
|
|
* |
9 |
gezelter |
1390 |
* 1. Redistributions of source code must retain the above copyright |
10 |
gezelter |
246 |
* notice, this list of conditions and the following disclaimer. |
11 |
|
|
* |
12 |
gezelter |
1390 |
* 2. Redistributions in binary form must reproduce the above copyright |
13 |
gezelter |
246 |
* notice, this list of conditions and the following disclaimer in the |
14 |
|
|
* documentation and/or other materials provided with the |
15 |
|
|
* distribution. |
16 |
|
|
* |
17 |
|
|
* This software is provided "AS IS," without a warranty of any |
18 |
|
|
* kind. All express or implied conditions, representations and |
19 |
|
|
* warranties, including any implied warranty of merchantability, |
20 |
|
|
* fitness for a particular purpose or non-infringement, are hereby |
21 |
|
|
* excluded. The University of Notre Dame and its licensors shall not |
22 |
|
|
* be liable for any damages suffered by licensee as a result of |
23 |
|
|
* using, modifying or distributing the software or its |
24 |
|
|
* derivatives. In no event will the University of Notre Dame or its |
25 |
|
|
* licensors be liable for any lost revenue, profit or data, or for |
26 |
|
|
* direct, indirect, special, consequential, incidental or punitive |
27 |
|
|
* damages, however caused and regardless of the theory of liability, |
28 |
|
|
* arising out of the use of or inability to use software, even if the |
29 |
|
|
* University of Notre Dame has been advised of the possibility of |
30 |
|
|
* such damages. |
31 |
gezelter |
1390 |
* |
32 |
|
|
* SUPPORT OPEN SCIENCE! If you use OpenMD or its source code in your |
33 |
|
|
* research, please cite the appropriate papers when you publish your |
34 |
|
|
* work. Good starting points are: |
35 |
|
|
* |
36 |
|
|
* [1] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005). |
37 |
|
|
* [2] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006). |
38 |
|
|
* [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 24107 (2008). |
39 |
|
|
* [4] Vardeman & Gezelter, in progress (2009). |
40 |
tim |
70 |
*/ |
41 |
gezelter |
246 |
|
42 |
tim |
70 |
/** |
43 |
|
|
* @file SquareMatrix.hpp |
44 |
|
|
* @author Teng Lin |
45 |
|
|
* @date 10/11/2004 |
46 |
|
|
* @version 1.0 |
47 |
|
|
*/ |
48 |
gezelter |
507 |
#ifndef MATH_SQUAREMATRIX_HPP |
49 |
tim |
70 |
#define MATH_SQUAREMATRIX_HPP |
50 |
|
|
|
51 |
tim |
74 |
#include "math/RectMatrix.hpp" |
52 |
gezelter |
956 |
#include "utils/NumericConstant.hpp" |
53 |
tim |
70 |
|
54 |
gezelter |
1390 |
namespace OpenMD { |
55 |
tim |
70 |
|
56 |
gezelter |
507 |
/** |
57 |
|
|
* @class SquareMatrix SquareMatrix.hpp "math/SquareMatrix.hpp" |
58 |
|
|
* @brief A square matrix class |
59 |
|
|
* @template Real the element type |
60 |
|
|
* @template Dim the dimension of the square matrix |
61 |
|
|
*/ |
62 |
|
|
template<typename Real, int Dim> |
63 |
|
|
class SquareMatrix : public RectMatrix<Real, Dim, Dim> { |
64 |
|
|
public: |
65 |
|
|
typedef Real ElemType; |
66 |
|
|
typedef Real* ElemPoinerType; |
67 |
tim |
70 |
|
68 |
gezelter |
507 |
/** default constructor */ |
69 |
|
|
SquareMatrix() { |
70 |
|
|
for (unsigned int i = 0; i < Dim; i++) |
71 |
|
|
for (unsigned int j = 0; j < Dim; j++) |
72 |
|
|
this->data_[i][j] = 0.0; |
73 |
|
|
} |
74 |
tim |
70 |
|
75 |
gezelter |
507 |
/** Constructs and initializes every element of this matrix to a scalar */ |
76 |
|
|
SquareMatrix(Real s) : RectMatrix<Real, Dim, Dim>(s){ |
77 |
|
|
} |
78 |
tim |
151 |
|
79 |
gezelter |
507 |
/** Constructs and initializes from an array */ |
80 |
|
|
SquareMatrix(Real* array) : RectMatrix<Real, Dim, Dim>(array){ |
81 |
|
|
} |
82 |
tim |
151 |
|
83 |
|
|
|
84 |
gezelter |
507 |
/** copy constructor */ |
85 |
|
|
SquareMatrix(const RectMatrix<Real, Dim, Dim>& m) : RectMatrix<Real, Dim, Dim>(m) { |
86 |
|
|
} |
87 |
tim |
70 |
|
88 |
gezelter |
507 |
/** copy assignment operator */ |
89 |
|
|
SquareMatrix<Real, Dim>& operator =(const RectMatrix<Real, Dim, Dim>& m) { |
90 |
|
|
RectMatrix<Real, Dim, Dim>::operator=(m); |
91 |
|
|
return *this; |
92 |
|
|
} |
93 |
tim |
137 |
|
94 |
gezelter |
507 |
/** Retunrs an identity matrix*/ |
95 |
tim |
74 |
|
96 |
gezelter |
507 |
static SquareMatrix<Real, Dim> identity() { |
97 |
|
|
SquareMatrix<Real, Dim> m; |
98 |
tim |
137 |
|
99 |
gezelter |
507 |
for (unsigned int i = 0; i < Dim; i++) |
100 |
|
|
for (unsigned int j = 0; j < Dim; j++) |
101 |
|
|
if (i == j) |
102 |
|
|
m(i, j) = 1.0; |
103 |
|
|
else |
104 |
|
|
m(i, j) = 0.0; |
105 |
tim |
70 |
|
106 |
gezelter |
507 |
return m; |
107 |
|
|
} |
108 |
tim |
74 |
|
109 |
gezelter |
507 |
/** |
110 |
|
|
* Retunrs the inversion of this matrix. |
111 |
|
|
* @todo need implementation |
112 |
|
|
*/ |
113 |
|
|
SquareMatrix<Real, Dim> inverse() { |
114 |
|
|
SquareMatrix<Real, Dim> result; |
115 |
tim |
70 |
|
116 |
gezelter |
507 |
return result; |
117 |
|
|
} |
118 |
tim |
70 |
|
119 |
gezelter |
507 |
/** |
120 |
|
|
* Returns the determinant of this matrix. |
121 |
|
|
* @todo need implementation |
122 |
|
|
*/ |
123 |
|
|
Real determinant() const { |
124 |
|
|
Real det; |
125 |
|
|
return det; |
126 |
|
|
} |
127 |
tim |
70 |
|
128 |
gezelter |
507 |
/** Returns the trace of this matrix. */ |
129 |
|
|
Real trace() const { |
130 |
|
|
Real tmp = 0; |
131 |
tim |
137 |
|
132 |
gezelter |
507 |
for (unsigned int i = 0; i < Dim ; i++) |
133 |
|
|
tmp += this->data_[i][i]; |
134 |
tim |
70 |
|
135 |
gezelter |
507 |
return tmp; |
136 |
|
|
} |
137 |
tim |
70 |
|
138 |
gezelter |
507 |
/** Tests if this matrix is symmetrix. */ |
139 |
|
|
bool isSymmetric() const { |
140 |
|
|
for (unsigned int i = 0; i < Dim - 1; i++) |
141 |
|
|
for (unsigned int j = i; j < Dim; j++) |
142 |
gezelter |
956 |
if (fabs(this->data_[i][j] - this->data_[j][i]) > epsilon) |
143 |
gezelter |
507 |
return false; |
144 |
tim |
137 |
|
145 |
gezelter |
507 |
return true; |
146 |
|
|
} |
147 |
tim |
70 |
|
148 |
gezelter |
507 |
/** Tests if this matrix is orthogonal. */ |
149 |
|
|
bool isOrthogonal() { |
150 |
|
|
SquareMatrix<Real, Dim> tmp; |
151 |
tim |
70 |
|
152 |
gezelter |
507 |
tmp = *this * transpose(); |
153 |
tim |
70 |
|
154 |
gezelter |
507 |
return tmp.isDiagonal(); |
155 |
|
|
} |
156 |
tim |
70 |
|
157 |
gezelter |
507 |
/** Tests if this matrix is diagonal. */ |
158 |
|
|
bool isDiagonal() const { |
159 |
|
|
for (unsigned int i = 0; i < Dim ; i++) |
160 |
|
|
for (unsigned int j = 0; j < Dim; j++) |
161 |
gezelter |
956 |
if (i !=j && fabs(this->data_[i][j]) > epsilon) |
162 |
gezelter |
507 |
return false; |
163 |
tim |
137 |
|
164 |
gezelter |
507 |
return true; |
165 |
|
|
} |
166 |
tim |
137 |
|
167 |
gezelter |
507 |
/** Tests if this matrix is the unit matrix. */ |
168 |
|
|
bool isUnitMatrix() const { |
169 |
|
|
if (!isDiagonal()) |
170 |
|
|
return false; |
171 |
tim |
70 |
|
172 |
gezelter |
507 |
for (unsigned int i = 0; i < Dim ; i++) |
173 |
gezelter |
956 |
if (fabs(this->data_[i][i] - 1) > epsilon) |
174 |
gezelter |
507 |
return false; |
175 |
tim |
137 |
|
176 |
gezelter |
507 |
return true; |
177 |
|
|
} |
178 |
tim |
70 |
|
179 |
gezelter |
507 |
/** Return the transpose of this matrix */ |
180 |
|
|
SquareMatrix<Real, Dim> transpose() const{ |
181 |
|
|
SquareMatrix<Real, Dim> result; |
182 |
tim |
273 |
|
183 |
gezelter |
507 |
for (unsigned int i = 0; i < Dim; i++) |
184 |
|
|
for (unsigned int j = 0; j < Dim; j++) |
185 |
|
|
result(j, i) = this->data_[i][j]; |
186 |
tim |
273 |
|
187 |
gezelter |
507 |
return result; |
188 |
|
|
} |
189 |
tim |
273 |
|
190 |
gezelter |
507 |
/** @todo need implementation */ |
191 |
|
|
void diagonalize() { |
192 |
|
|
//jacobi(m, eigenValues, ortMat); |
193 |
|
|
} |
194 |
tim |
76 |
|
195 |
gezelter |
507 |
/** |
196 |
|
|
* Jacobi iteration routines for computing eigenvalues/eigenvectors of |
197 |
|
|
* real symmetric matrix |
198 |
|
|
* |
199 |
|
|
* @return true if success, otherwise return false |
200 |
|
|
* @param a symmetric matrix whose eigenvectors are to be computed. On return, the matrix is |
201 |
|
|
* overwritten |
202 |
|
|
* @param w will contain the eigenvalues of the matrix On return of this function |
203 |
|
|
* @param v the columns of this matrix will contain the eigenvectors. The eigenvectors are |
204 |
|
|
* normalized and mutually orthogonal. |
205 |
|
|
*/ |
206 |
tim |
137 |
|
207 |
gezelter |
507 |
static int jacobi(SquareMatrix<Real, Dim>& a, Vector<Real, Dim>& d, |
208 |
|
|
SquareMatrix<Real, Dim>& v); |
209 |
|
|
};//end SquareMatrix |
210 |
tim |
70 |
|
211 |
tim |
76 |
|
212 |
gezelter |
507 |
/*========================================================================= |
213 |
tim |
76 |
|
214 |
tim |
123 |
Program: Visualization Toolkit |
215 |
|
|
Module: $RCSfile: SquareMatrix.hpp,v $ |
216 |
tim |
76 |
|
217 |
tim |
123 |
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen |
218 |
|
|
All rights reserved. |
219 |
|
|
See Copyright.txt or http://www.kitware.com/Copyright.htm for details. |
220 |
|
|
|
221 |
gezelter |
507 |
This software is distributed WITHOUT ANY WARRANTY; without even |
222 |
|
|
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
223 |
|
|
PURPOSE. See the above copyright notice for more information. |
224 |
tim |
123 |
|
225 |
gezelter |
507 |
=========================================================================*/ |
226 |
tim |
123 |
|
227 |
gezelter |
507 |
#define VTK_ROTATE(a,i,j,k,l) g=a(i, j);h=a(k, l);a(i, j)=g-s*(h+g*tau); \ |
228 |
|
|
a(k, l)=h+s*(g-h*tau) |
229 |
tim |
123 |
|
230 |
|
|
#define VTK_MAX_ROTATIONS 20 |
231 |
|
|
|
232 |
gezelter |
507 |
// Jacobi iteration for the solution of eigenvectors/eigenvalues of a nxn |
233 |
|
|
// real symmetric matrix. Square nxn matrix a; size of matrix in n; |
234 |
|
|
// output eigenvalues in w; and output eigenvectors in v. Resulting |
235 |
|
|
// eigenvalues/vectors are sorted in decreasing order; eigenvectors are |
236 |
|
|
// normalized. |
237 |
|
|
template<typename Real, int Dim> |
238 |
|
|
int SquareMatrix<Real, Dim>::jacobi(SquareMatrix<Real, Dim>& a, Vector<Real, Dim>& w, |
239 |
|
|
SquareMatrix<Real, Dim>& v) { |
240 |
|
|
const int n = Dim; |
241 |
|
|
int i, j, k, iq, ip, numPos; |
242 |
|
|
Real tresh, theta, tau, t, sm, s, h, g, c, tmp; |
243 |
|
|
Real bspace[4], zspace[4]; |
244 |
|
|
Real *b = bspace; |
245 |
|
|
Real *z = zspace; |
246 |
tim |
123 |
|
247 |
gezelter |
507 |
// only allocate memory if the matrix is large |
248 |
|
|
if (n > 4) { |
249 |
|
|
b = new Real[n]; |
250 |
|
|
z = new Real[n]; |
251 |
|
|
} |
252 |
tim |
123 |
|
253 |
gezelter |
507 |
// initialize |
254 |
|
|
for (ip=0; ip<n; ip++) { |
255 |
|
|
for (iq=0; iq<n; iq++) { |
256 |
|
|
v(ip, iq) = 0.0; |
257 |
|
|
} |
258 |
|
|
v(ip, ip) = 1.0; |
259 |
|
|
} |
260 |
|
|
for (ip=0; ip<n; ip++) { |
261 |
|
|
b[ip] = w[ip] = a(ip, ip); |
262 |
|
|
z[ip] = 0.0; |
263 |
|
|
} |
264 |
tim |
76 |
|
265 |
gezelter |
507 |
// begin rotation sequence |
266 |
|
|
for (i=0; i<VTK_MAX_ROTATIONS; i++) { |
267 |
|
|
sm = 0.0; |
268 |
|
|
for (ip=0; ip<n-1; ip++) { |
269 |
|
|
for (iq=ip+1; iq<n; iq++) { |
270 |
|
|
sm += fabs(a(ip, iq)); |
271 |
|
|
} |
272 |
|
|
} |
273 |
|
|
if (sm == 0.0) { |
274 |
|
|
break; |
275 |
|
|
} |
276 |
tim |
76 |
|
277 |
gezelter |
507 |
if (i < 3) { // first 3 sweeps |
278 |
|
|
tresh = 0.2*sm/(n*n); |
279 |
|
|
} else { |
280 |
|
|
tresh = 0.0; |
281 |
|
|
} |
282 |
tim |
76 |
|
283 |
gezelter |
507 |
for (ip=0; ip<n-1; ip++) { |
284 |
|
|
for (iq=ip+1; iq<n; iq++) { |
285 |
|
|
g = 100.0*fabs(a(ip, iq)); |
286 |
tim |
76 |
|
287 |
gezelter |
507 |
// after 4 sweeps |
288 |
|
|
if (i > 3 && (fabs(w[ip])+g) == fabs(w[ip]) |
289 |
|
|
&& (fabs(w[iq])+g) == fabs(w[iq])) { |
290 |
|
|
a(ip, iq) = 0.0; |
291 |
|
|
} else if (fabs(a(ip, iq)) > tresh) { |
292 |
|
|
h = w[iq] - w[ip]; |
293 |
|
|
if ( (fabs(h)+g) == fabs(h)) { |
294 |
|
|
t = (a(ip, iq)) / h; |
295 |
|
|
} else { |
296 |
|
|
theta = 0.5*h / (a(ip, iq)); |
297 |
|
|
t = 1.0 / (fabs(theta)+sqrt(1.0+theta*theta)); |
298 |
|
|
if (theta < 0.0) { |
299 |
|
|
t = -t; |
300 |
|
|
} |
301 |
|
|
} |
302 |
|
|
c = 1.0 / sqrt(1+t*t); |
303 |
|
|
s = t*c; |
304 |
|
|
tau = s/(1.0+c); |
305 |
|
|
h = t*a(ip, iq); |
306 |
|
|
z[ip] -= h; |
307 |
|
|
z[iq] += h; |
308 |
|
|
w[ip] -= h; |
309 |
|
|
w[iq] += h; |
310 |
|
|
a(ip, iq)=0.0; |
311 |
tim |
76 |
|
312 |
gezelter |
507 |
// ip already shifted left by 1 unit |
313 |
|
|
for (j = 0;j <= ip-1;j++) { |
314 |
|
|
VTK_ROTATE(a,j,ip,j,iq); |
315 |
|
|
} |
316 |
|
|
// ip and iq already shifted left by 1 unit |
317 |
|
|
for (j = ip+1;j <= iq-1;j++) { |
318 |
|
|
VTK_ROTATE(a,ip,j,j,iq); |
319 |
|
|
} |
320 |
|
|
// iq already shifted left by 1 unit |
321 |
|
|
for (j=iq+1; j<n; j++) { |
322 |
|
|
VTK_ROTATE(a,ip,j,iq,j); |
323 |
|
|
} |
324 |
|
|
for (j=0; j<n; j++) { |
325 |
|
|
VTK_ROTATE(v,j,ip,j,iq); |
326 |
|
|
} |
327 |
|
|
} |
328 |
|
|
} |
329 |
|
|
} |
330 |
tim |
93 |
|
331 |
gezelter |
507 |
for (ip=0; ip<n; ip++) { |
332 |
|
|
b[ip] += z[ip]; |
333 |
|
|
w[ip] = b[ip]; |
334 |
|
|
z[ip] = 0.0; |
335 |
|
|
} |
336 |
|
|
} |
337 |
tim |
93 |
|
338 |
gezelter |
507 |
//// this is NEVER called |
339 |
|
|
if ( i >= VTK_MAX_ROTATIONS ) { |
340 |
|
|
std::cout << "vtkMath::Jacobi: Error extracting eigenfunctions" << std::endl; |
341 |
|
|
return 0; |
342 |
|
|
} |
343 |
tim |
76 |
|
344 |
gezelter |
507 |
// sort eigenfunctions these changes do not affect accuracy |
345 |
|
|
for (j=0; j<n-1; j++) { // boundary incorrect |
346 |
|
|
k = j; |
347 |
|
|
tmp = w[k]; |
348 |
|
|
for (i=j+1; i<n; i++) { // boundary incorrect, shifted already |
349 |
|
|
if (w[i] >= tmp) { // why exchage if same? |
350 |
|
|
k = i; |
351 |
|
|
tmp = w[k]; |
352 |
|
|
} |
353 |
|
|
} |
354 |
|
|
if (k != j) { |
355 |
|
|
w[k] = w[j]; |
356 |
|
|
w[j] = tmp; |
357 |
|
|
for (i=0; i<n; i++) { |
358 |
|
|
tmp = v(i, j); |
359 |
|
|
v(i, j) = v(i, k); |
360 |
|
|
v(i, k) = tmp; |
361 |
|
|
} |
362 |
|
|
} |
363 |
|
|
} |
364 |
|
|
// insure eigenvector consistency (i.e., Jacobi can compute vectors that |
365 |
|
|
// are negative of one another (.707,.707,0) and (-.707,-.707,0). This can |
366 |
|
|
// reek havoc in hyperstreamline/other stuff. We will select the most |
367 |
|
|
// positive eigenvector. |
368 |
|
|
int ceil_half_n = (n >> 1) + (n & 1); |
369 |
|
|
for (j=0; j<n; j++) { |
370 |
|
|
for (numPos=0, i=0; i<n; i++) { |
371 |
|
|
if ( v(i, j) >= 0.0 ) { |
372 |
|
|
numPos++; |
373 |
|
|
} |
374 |
|
|
} |
375 |
tim |
963 |
// if ( numPos < ceil(RealType(n)/RealType(2.0)) ) |
376 |
gezelter |
507 |
if ( numPos < ceil_half_n) { |
377 |
|
|
for (i=0; i<n; i++) { |
378 |
|
|
v(i, j) *= -1.0; |
379 |
|
|
} |
380 |
|
|
} |
381 |
|
|
} |
382 |
tim |
76 |
|
383 |
gezelter |
507 |
if (n > 4) { |
384 |
|
|
delete [] b; |
385 |
|
|
delete [] z; |
386 |
tim |
76 |
} |
387 |
gezelter |
507 |
return 1; |
388 |
|
|
} |
389 |
tim |
76 |
|
390 |
|
|
|
391 |
tim |
963 |
typedef SquareMatrix<RealType, 6> Mat6x6d; |
392 |
tim |
76 |
} |
393 |
tim |
123 |
#endif //MATH_SQUAREMATRIX_HPP |
394 |
tim |
76 |
|