| 1 |
tim |
70 |
/* |
| 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. |
| 23 |
|
|
* |
| 24 |
|
|
*/ |
| 25 |
|
|
|
| 26 |
|
|
/** |
| 27 |
|
|
* @file SquareMatrix.hpp |
| 28 |
|
|
* @author Teng Lin |
| 29 |
|
|
* @date 10/11/2004 |
| 30 |
|
|
* @version 1.0 |
| 31 |
|
|
*/ |
| 32 |
|
|
#ifndef MATH_SQUAREMATRIX_HPP |
| 33 |
|
|
#define MATH_SQUAREMATRIX_HPP |
| 34 |
|
|
|
| 35 |
tim |
74 |
#include "math/RectMatrix.hpp" |
| 36 |
tim |
70 |
|
| 37 |
|
|
namespace oopse { |
| 38 |
|
|
|
| 39 |
|
|
/** |
| 40 |
|
|
* @class SquareMatrix SquareMatrix.hpp "math/SquareMatrix.hpp" |
| 41 |
|
|
* @brief A square matrix class |
| 42 |
|
|
* @template Real the element type |
| 43 |
|
|
* @template Dim the dimension of the square matrix |
| 44 |
|
|
*/ |
| 45 |
|
|
template<typename Real, int Dim> |
| 46 |
tim |
74 |
class SquareMatrix : public RectMatrix<Real, Dim, Dim> { |
| 47 |
tim |
70 |
public: |
| 48 |
|
|
|
| 49 |
|
|
/** default constructor */ |
| 50 |
|
|
SquareMatrix() { |
| 51 |
|
|
for (unsigned int i = 0; i < Dim; i++) |
| 52 |
|
|
for (unsigned int j = 0; j < Dim; j++) |
| 53 |
|
|
data_[i][j] = 0.0; |
| 54 |
|
|
} |
| 55 |
|
|
|
| 56 |
|
|
/** copy constructor */ |
| 57 |
tim |
74 |
SquareMatrix(const RectMatrix<Real, Dim, Dim>& m) : RectMatrix<Real, Dim, Dim>(m) { |
| 58 |
tim |
70 |
} |
| 59 |
|
|
|
| 60 |
|
|
/** copy assignment operator */ |
| 61 |
tim |
74 |
SquareMatrix<Real, Dim>& operator =(const RectMatrix<Real, Dim, Dim>& m) { |
| 62 |
|
|
RectMatrix<Real, Dim, Dim>::operator=(m); |
| 63 |
|
|
return *this; |
| 64 |
tim |
70 |
} |
| 65 |
tim |
74 |
|
| 66 |
|
|
/** Retunrs an identity matrix*/ |
| 67 |
tim |
70 |
|
| 68 |
tim |
74 |
static SquareMatrix<Real, Dim> identity() { |
| 69 |
|
|
SquareMatrix<Real, Dim> m; |
| 70 |
tim |
70 |
|
| 71 |
|
|
for (unsigned int i = 0; i < Dim; i++) |
| 72 |
tim |
74 |
for (unsigned int j = 0; j < Dim; j++) |
| 73 |
tim |
70 |
if (i == j) |
| 74 |
tim |
74 |
m(i, j) = 1.0; |
| 75 |
tim |
70 |
else |
| 76 |
tim |
74 |
m(i, j) = 0.0; |
| 77 |
|
|
|
| 78 |
|
|
return m; |
| 79 |
tim |
70 |
} |
| 80 |
|
|
|
| 81 |
tim |
74 |
/** Retunrs the inversion of this matrix. */ |
| 82 |
|
|
SquareMatrix<Real, Dim> inverse() { |
| 83 |
|
|
SquareMatrix<Real, Dim> result; |
| 84 |
|
|
|
| 85 |
|
|
return result; |
| 86 |
tim |
70 |
} |
| 87 |
|
|
|
| 88 |
|
|
|
| 89 |
|
|
|
| 90 |
|
|
/** Returns the determinant of this matrix. */ |
| 91 |
|
|
double determinant() const { |
| 92 |
tim |
74 |
double det; |
| 93 |
|
|
return det; |
| 94 |
tim |
70 |
} |
| 95 |
|
|
|
| 96 |
|
|
/** Returns the trace of this matrix. */ |
| 97 |
|
|
double trace() const { |
| 98 |
|
|
double tmp = 0; |
| 99 |
|
|
|
| 100 |
|
|
for (unsigned int i = 0; i < Dim ; i++) |
| 101 |
|
|
tmp += data_[i][i]; |
| 102 |
|
|
|
| 103 |
|
|
return tmp; |
| 104 |
|
|
} |
| 105 |
|
|
|
| 106 |
|
|
/** Tests if this matrix is symmetrix. */ |
| 107 |
|
|
bool isSymmetric() const { |
| 108 |
|
|
for (unsigned int i = 0; i < Dim - 1; i++) |
| 109 |
|
|
for (unsigned int j = i; j < Dim; j++) |
| 110 |
tim |
74 |
if (fabs(data_[i][j] - data_[j][i]) > oopse::epsilon) |
| 111 |
tim |
70 |
return false; |
| 112 |
|
|
|
| 113 |
|
|
return true; |
| 114 |
|
|
} |
| 115 |
|
|
|
| 116 |
|
|
/** Tests if this matrix is orthogona. */ |
| 117 |
tim |
74 |
bool isOrthogonal() { |
| 118 |
|
|
SquareMatrix<Real, Dim> tmp; |
| 119 |
tim |
70 |
|
| 120 |
tim |
74 |
tmp = *this * transpose(); |
| 121 |
tim |
70 |
|
| 122 |
tim |
74 |
return tmp.isUnitMatrix(); |
| 123 |
tim |
70 |
} |
| 124 |
|
|
|
| 125 |
|
|
/** Tests if this matrix is diagonal. */ |
| 126 |
|
|
bool isDiagonal() const { |
| 127 |
|
|
for (unsigned int i = 0; i < Dim ; i++) |
| 128 |
|
|
for (unsigned int j = 0; j < Dim; j++) |
| 129 |
tim |
74 |
if (i !=j && fabs(data_[i][j]) > oopse::epsilon) |
| 130 |
tim |
70 |
return false; |
| 131 |
|
|
|
| 132 |
|
|
return true; |
| 133 |
|
|
} |
| 134 |
|
|
|
| 135 |
|
|
/** Tests if this matrix is the unit matrix. */ |
| 136 |
|
|
bool isUnitMatrix() const { |
| 137 |
|
|
if (!isDiagonal()) |
| 138 |
|
|
return false; |
| 139 |
|
|
|
| 140 |
|
|
for (unsigned int i = 0; i < Dim ; i++) |
| 141 |
tim |
74 |
if (fabs(data_[i][i] - 1) > oopse::epsilon) |
| 142 |
tim |
70 |
return false; |
| 143 |
|
|
|
| 144 |
|
|
return true; |
| 145 |
tim |
74 |
} |
| 146 |
tim |
70 |
|
| 147 |
|
|
};//end SquareMatrix |
| 148 |
|
|
|
| 149 |
|
|
} |
| 150 |
|
|
#endif //MATH_SQUAREMATRIX_HPP |