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