| 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. |
| 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 |
| 48 |
|
#ifndef MATH_SQUAREMATRIX_HPP |
| 49 |
|
#define MATH_SQUAREMATRIX_HPP |
| 50 |
|
|
| 51 |
< |
#include "Vector3d.hpp" |
| 51 |
> |
#include "math/RectMatrix.hpp" |
| 52 |
> |
#include "utils/NumericConstant.hpp" |
| 53 |
|
|
| 54 |
|
namespace oopse { |
| 55 |
|
|
| 56 |
< |
/** |
| 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{ |
| 64 |
< |
public: |
| 56 |
> |
/** |
| 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 |
|
|
| 68 |
< |
/** default constructor */ |
| 69 |
< |
SquareMatrix() { |
| 70 |
< |
for (unsigned int i = 0; i < Dim; i++) |
| 71 |
< |
for (unsigned int j = 0; j < Dim; j++) |
| 72 |
< |
data_[i][j] = 0.0; |
| 73 |
< |
} |
| 68 |
> |
/** 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 |
|
|
| 75 |
< |
/** Constructs and initializes every element of this matrix to a scalar */ |
| 76 |
< |
SquareMatrix(double s) { |
| 77 |
< |
for (unsigned int i = 0; i < Dim; i++) |
| 59 |
< |
for (unsigned int j = 0; j < Dim; j++) |
| 60 |
< |
data_[i][j] = s; |
| 61 |
< |
} |
| 75 |
> |
/** Constructs and initializes every element of this matrix to a scalar */ |
| 76 |
> |
SquareMatrix(Real s) : RectMatrix<Real, Dim, Dim>(s){ |
| 77 |
> |
} |
| 78 |
|
|
| 79 |
< |
/** copy constructor */ |
| 80 |
< |
SquareMatrix(const SquareMatrix<Real, Dim>& m) { |
| 81 |
< |
*this = m; |
| 66 |
< |
} |
| 67 |
< |
|
| 68 |
< |
/** destructor*/ |
| 69 |
< |
~SquareMatrix() {} |
| 79 |
> |
/** Constructs and initializes from an array */ |
| 80 |
> |
SquareMatrix(Real* array) : RectMatrix<Real, Dim, Dim>(array){ |
| 81 |
> |
} |
| 82 |
|
|
| 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 |
– |
} |
| 83 |
|
|
| 84 |
< |
/** |
| 85 |
< |
* Return the value of a single element of this matrix. |
| 86 |
< |
* @return the value of a single element of this matrix |
| 87 |
< |
* @param i row index |
| 88 |
< |
* @param j colum index |
| 89 |
< |
*/ |
| 90 |
< |
double operator()(unsigned int i, unsigned int j) const { |
| 91 |
< |
return data_[i][j]; |
| 92 |
< |
} |
| 84 |
> |
/** copy constructor */ |
| 85 |
> |
SquareMatrix(const RectMatrix<Real, Dim, Dim>& m) : RectMatrix<Real, Dim, Dim>(m) { |
| 86 |
> |
} |
| 87 |
> |
|
| 88 |
> |
/** 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 |
> |
|
| 94 |
> |
/** Retunrs an identity matrix*/ |
| 95 |
|
|
| 96 |
< |
/** |
| 97 |
< |
* Returns a row of this matrix as a vector. |
| 98 |
< |
* @return a row of this matrix as a vector |
| 99 |
< |
* @param row the row index |
| 100 |
< |
*/ |
| 101 |
< |
Vector<Real, Dim> getRow(unsigned int row) { |
| 102 |
< |
Vector<Real, Dim> v; |
| 96 |
> |
static SquareMatrix<Real, Dim> identity() { |
| 97 |
> |
SquareMatrix<Real, Dim> m; |
| 98 |
> |
|
| 99 |
> |
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 |
|
|
| 106 |
< |
for (unsigned int i = 0; i < Dim; i++) |
| 107 |
< |
v[i] = data_[row][i]; |
| 106 |
> |
return m; |
| 107 |
> |
} |
| 108 |
|
|
| 109 |
< |
return v; |
| 110 |
< |
} |
| 109 |
> |
/** |
| 110 |
> |
* Retunrs the inversion of this matrix. |
| 111 |
> |
* @todo need implementation |
| 112 |
> |
*/ |
| 113 |
> |
SquareMatrix<Real, Dim> inverse() { |
| 114 |
> |
SquareMatrix<Real, Dim> result; |
| 115 |
|
|
| 116 |
< |
/** |
| 117 |
< |
* 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; |
| 116 |
> |
return result; |
| 117 |
> |
} |
| 118 |
|
|
| 119 |
< |
for (unsigned int i = 0; i < Dim; i++) |
| 120 |
< |
data_[row][i] = v[i]; |
| 121 |
< |
} |
| 119 |
> |
/** |
| 120 |
> |
* Returns the determinant of this matrix. |
| 121 |
> |
* @todo need implementation |
| 122 |
> |
*/ |
| 123 |
> |
Real determinant() const { |
| 124 |
> |
Real det; |
| 125 |
> |
return det; |
| 126 |
> |
} |
| 127 |
|
|
| 128 |
< |
/** |
| 129 |
< |
* Returns a column of this matrix as a vector. |
| 130 |
< |
* @return a column of this matrix as a vector |
| 131 |
< |
* @param col the column index |
| 132 |
< |
*/ |
| 133 |
< |
Vector<Real, Dim> getColum(unsigned int col) { |
| 130 |
< |
Vector<Real, Dim> v; |
| 128 |
> |
/** Returns the trace of this matrix. */ |
| 129 |
> |
Real trace() const { |
| 130 |
> |
Real tmp = 0; |
| 131 |
> |
|
| 132 |
> |
for (unsigned int i = 0; i < Dim ; i++) |
| 133 |
> |
tmp += this->data_[i][i]; |
| 134 |
|
|
| 135 |
< |
for (unsigned int i = 0; i < Dim; i++) |
| 136 |
< |
v[i] = data_[i][col]; |
| 135 |
> |
return tmp; |
| 136 |
> |
} |
| 137 |
|
|
| 138 |
< |
return v; |
| 139 |
< |
} |
| 138 |
> |
/** 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 |
> |
if (fabs(this->data_[i][j] - this->data_[j][i]) > epsilon) |
| 143 |
> |
return false; |
| 144 |
> |
|
| 145 |
> |
return true; |
| 146 |
> |
} |
| 147 |
|
|
| 148 |
< |
/** |
| 149 |
< |
* Sets a column of this matrix |
| 150 |
< |
* @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; |
| 148 |
> |
/** Tests if this matrix is orthogonal. */ |
| 149 |
> |
bool isOrthogonal() { |
| 150 |
> |
SquareMatrix<Real, Dim> tmp; |
| 151 |
|
|
| 152 |
< |
for (unsigned int i = 0; i < Dim; i++) |
| 147 |
< |
data_[i][col] = v[i]; |
| 148 |
< |
} |
| 152 |
> |
tmp = *this * transpose(); |
| 153 |
|
|
| 154 |
< |
/** Negates the value of this matrix in place. */ |
| 155 |
< |
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]; |
| 175 |
< |
} |
| 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 |
< |
} |
| 154 |
> |
return tmp.isDiagonal(); |
| 155 |
> |
} |
| 156 |
|
|
| 157 |
< |
/** |
| 158 |
< |
* Sets the value of this matrix to the scalar multiplication of matrix m (*this = s * m). |
| 159 |
< |
* @param s the scalar value |
| 160 |
< |
* @param m the matrix |
| 161 |
< |
*/ |
| 162 |
< |
inline void mul( double s, const SquareMatrix<Real, Dim>& m ) { |
| 163 |
< |
for (unsigned int i = 0; i < Dim; i++) |
| 164 |
< |
for (unsigned int j = 0; j < Dim; j++) |
| 165 |
< |
data_[i][j] = s * m.data_[i][j]; |
| 228 |
< |
} |
| 157 |
> |
/** 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 |
> |
if (i !=j && fabs(this->data_[i][j]) > epsilon) |
| 162 |
> |
return false; |
| 163 |
> |
|
| 164 |
> |
return true; |
| 165 |
> |
} |
| 166 |
|
|
| 167 |
< |
/** |
| 168 |
< |
* Sets the value of this matrix to the multiplication of this matrix and matrix m |
| 169 |
< |
* (*this = *this * m). |
| 170 |
< |
* @param m the matrix |
| 171 |
< |
*/ |
| 172 |
< |
inline void mul(const SquareMatrix<Real, Dim>& m ) { |
| 173 |
< |
SquareMatrix<Real, Dim> tmp(*this); |
| 174 |
< |
|
| 238 |
< |
for (unsigned int i = 0; i < Dim; i++) |
| 239 |
< |
for (unsigned int j = 0; j < Dim; j++) { |
| 167 |
> |
/** Tests if this matrix is the unit matrix. */ |
| 168 |
> |
bool isUnitMatrix() const { |
| 169 |
> |
if (!isDiagonal()) |
| 170 |
> |
return false; |
| 171 |
> |
|
| 172 |
> |
for (unsigned int i = 0; i < Dim ; i++) |
| 173 |
> |
if (fabs(this->data_[i][i] - 1) > epsilon) |
| 174 |
> |
return false; |
| 175 |
|
|
| 176 |
< |
data_[i][j] = 0.0; |
| 177 |
< |
for (unsigned int k = 0; k < Dim; k++) |
| 243 |
< |
data_[i][j] = tmp.data_[i][k] * m.data_[k][j] |
| 244 |
< |
} |
| 245 |
< |
} |
| 246 |
< |
|
| 247 |
< |
/** |
| 248 |
< |
* Sets the value of this matrix to the left multiplication of matrix m into itself |
| 249 |
< |
* (*this = m * *this). |
| 250 |
< |
* @param m the matrix |
| 251 |
< |
*/ |
| 252 |
< |
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] |
| 261 |
< |
} |
| 262 |
< |
} |
| 176 |
> |
return true; |
| 177 |
> |
} |
| 178 |
|
|
| 179 |
< |
/** |
| 180 |
< |
* Sets the value of this matrix to the multiplication of matrix m1 and matrix m2 |
| 181 |
< |
* (*this = m1 * m2). |
| 182 |
< |
* @param m1 the first matrix |
| 183 |
< |
* @param m2 the second matrix |
| 184 |
< |
*/ |
| 185 |
< |
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] |
| 278 |
< |
} |
| 179 |
> |
/** Return the transpose of this matrix */ |
| 180 |
> |
SquareMatrix<Real, Dim> transpose() const{ |
| 181 |
> |
SquareMatrix<Real, Dim> result; |
| 182 |
> |
|
| 183 |
> |
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 |
|
|
| 187 |
< |
} |
| 188 |
< |
|
| 189 |
< |
/** |
| 190 |
< |
* Sets the value of this matrix to the scalar division of itself (*this /= s ). |
| 191 |
< |
* @param s the scalar value |
| 192 |
< |
*/ |
| 193 |
< |
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 |
< |
} |
| 187 |
> |
return result; |
| 188 |
> |
} |
| 189 |
> |
|
| 190 |
> |
/** @todo need implementation */ |
| 191 |
> |
void diagonalize() { |
| 192 |
> |
//jacobi(m, eigenValues, ortMat); |
| 193 |
> |
} |
| 194 |
|
|
| 195 |
< |
/** |
| 196 |
< |
* Multiples a scalar into every element of this matrix. |
| 197 |
< |
* @param s the scalar value |
| 198 |
< |
*/ |
| 199 |
< |
SquareMatrix<Real, Dim>& operator *=(const double s) { |
| 200 |
< |
this->mul(s); |
| 201 |
< |
return *this; |
| 202 |
< |
} |
| 195 |
> |
/** |
| 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 |
> |
|
| 207 |
> |
static int jacobi(SquareMatrix<Real, Dim>& a, Vector<Real, Dim>& d, |
| 208 |
> |
SquareMatrix<Real, Dim>& v); |
| 209 |
> |
};//end SquareMatrix |
| 210 |
|
|
| 321 |
– |
/** |
| 322 |
– |
* 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 |
– |
} |
| 211 |
|
|
| 212 |
< |
/** |
| 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 |
< |
} |
| 212 |
> |
/*========================================================================= |
| 213 |
|
|
| 214 |
< |
/** |
| 215 |
< |
* 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 |
< |
} |
| 214 |
> |
Program: Visualization Toolkit |
| 215 |
> |
Module: $RCSfile: SquareMatrix.hpp,v $ |
| 216 |
|
|
| 217 |
< |
/** set this matrix to an identity matrix*/ |
| 217 |
> |
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 |
< |
void identity() { |
| 222 |
< |
for (unsigned int i = 0; i < Dim; i++) |
| 223 |
< |
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 |
< |
} |
| 221 |
> |
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 |
|
|
| 225 |
< |
/** Sets the value of this matrix to the inversion of itself. */ |
| 360 |
< |
void inverse() { |
| 361 |
< |
inverse(*this); |
| 362 |
< |
} |
| 225 |
> |
=========================================================================*/ |
| 226 |
|
|
| 227 |
< |
/** |
| 228 |
< |
* 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 |
< |
} |
| 227 |
> |
#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 |
|
|
| 230 |
< |
/** |
| 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]; |
| 389 |
< |
} |
| 390 |
< |
} |
| 230 |
> |
#define VTK_MAX_ROTATIONS 20 |
| 231 |
|
|
| 232 |
< |
/** Returns the determinant of this matrix. */ |
| 233 |
< |
double determinant() const { |
| 232 |
> |
// 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 |
|
|
| 247 |
< |
} |
| 247 |
> |
// only allocate memory if the matrix is large |
| 248 |
> |
if (n > 4) { |
| 249 |
> |
b = new Real[n]; |
| 250 |
> |
z = new Real[n]; |
| 251 |
> |
} |
| 252 |
|
|
| 253 |
< |
/** Returns the trace of this matrix. */ |
| 254 |
< |
double trace() const { |
| 255 |
< |
double tmp = 0; |
| 256 |
< |
|
| 257 |
< |
for (unsigned int i = 0; i < Dim ; i++) |
| 258 |
< |
tmp += data_[i][i]; |
| 253 |
> |
// 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 |
|
|
| 265 |
< |
return tmp; |
| 266 |
< |
} |
| 265 |
> |
// 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 |
|
|
| 277 |
< |
/** Tests if this matrix is symmetrix. */ |
| 278 |
< |
bool isSymmetric() const { |
| 279 |
< |
for (unsigned int i = 0; i < Dim - 1; i++) |
| 280 |
< |
for (unsigned int j = i; j < Dim; j++) |
| 281 |
< |
if (fabs(data_[i][j] - data_[j][i]) > epsilon) |
| 412 |
< |
return false; |
| 413 |
< |
|
| 414 |
< |
return true; |
| 415 |
< |
} |
| 277 |
> |
if (i < 3) { // first 3 sweeps |
| 278 |
> |
tresh = 0.2*sm/(n*n); |
| 279 |
> |
} else { |
| 280 |
> |
tresh = 0.0; |
| 281 |
> |
} |
| 282 |
|
|
| 283 |
< |
/** Tests if this matrix is orthogona. */ |
| 284 |
< |
bool isOrthogonal() const { |
| 285 |
< |
SquareMatrix<Real, Dim> t(*this); |
| 283 |
> |
for (ip=0; ip<n-1; ip++) { |
| 284 |
> |
for (iq=ip+1; iq<n; iq++) { |
| 285 |
> |
g = 100.0*fabs(a(ip, iq)); |
| 286 |
|
|
| 287 |
< |
t.transpose(); |
| 287 |
> |
// 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 |
|
|
| 312 |
< |
return isUnitMatrix(*this * t); |
| 313 |
< |
} |
| 312 |
> |
// 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 |
|
|
| 331 |
< |
/** Tests if this matrix is diagonal. */ |
| 332 |
< |
bool isDiagonal() const { |
| 333 |
< |
for (unsigned int i = 0; i < Dim ; i++) |
| 334 |
< |
for (unsigned int j = 0; j < Dim; j++) |
| 335 |
< |
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; |
| 331 |
> |
for (ip=0; ip<n; ip++) { |
| 332 |
> |
b[ip] += z[ip]; |
| 333 |
> |
w[ip] = b[ip]; |
| 334 |
> |
z[ip] = 0.0; |
| 335 |
> |
} |
| 336 |
|
} |
| 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; |
| 337 |
|
|
| 338 |
< |
result.add(m1, m2); |
| 339 |
< |
|
| 340 |
< |
return result; |
| 338 |
> |
//// this is NEVER called |
| 339 |
> |
if ( i >= VTK_MAX_ROTATIONS ) { |
| 340 |
> |
std::cout << "vtkMath::Jacobi: Error extracting eigenfunctions" << std::endl; |
| 341 |
> |
return 0; |
| 342 |
|
} |
| 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; |
| 343 |
|
|
| 344 |
< |
result.sub(m1, m2); |
| 345 |
< |
|
| 346 |
< |
return result; |
| 344 |
> |
// 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 |
< |
|
| 365 |
< |
/** |
| 366 |
< |
* Return the multiplication of two matrixes (m1 * m2). |
| 367 |
< |
* @return the multiplication of two matrixes |
| 368 |
< |
* @param m1 the first matrix |
| 369 |
< |
* @param m2 the second matrix |
| 370 |
< |
*/ |
| 371 |
< |
template<typename Real, int Dim> |
| 372 |
< |
inline SquareMatrix<Real, Dim> operator *(const SquareMatrix<Real, Dim>& m1, |
| 373 |
< |
const SquareMatrix<Real, Dim>& m2) { |
| 374 |
< |
SquareMatrix<Real, Dim> result; |
| 375 |
< |
|
| 376 |
< |
result.mul(m1, m2); |
| 377 |
< |
|
| 378 |
< |
return result; |
| 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 |
> |
// if ( numPos < ceil(double(n)/double(2.0)) ) |
| 376 |
> |
if ( numPos < ceil_half_n) { |
| 377 |
> |
for (i=0; i<n; i++) { |
| 378 |
> |
v(i, j) *= -1.0; |
| 379 |
> |
} |
| 380 |
> |
} |
| 381 |
|
} |
| 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; |
| 382 |
|
|
| 383 |
< |
for (unsigned int i = 0; i < Dim ; i++) |
| 384 |
< |
for (unsigned int j = 0; j < Dim ; j++) |
| 385 |
< |
result[i] += m(i, j) * v[j]; |
| 526 |
< |
|
| 527 |
< |
return result; |
| 383 |
> |
if (n > 4) { |
| 384 |
> |
delete [] b; |
| 385 |
> |
delete [] z; |
| 386 |
|
} |
| 387 |
+ |
return 1; |
| 388 |
+ |
} |
| 389 |
+ |
|
| 390 |
+ |
|
| 391 |
+ |
typedef SquareMatrix<double, 6> Mat6x6d; |
| 392 |
|
} |
| 393 |
|
#endif //MATH_SQUAREMATRIX_HPP |
| 394 |
+ |
|