24 template<
typename Obj>
25 class Matrix<Obj,1>:
public Array<Vector<Obj,1>,1> {
31 Matrix<Obj,1>& inverse() {
32 (*this)[0][0] = 1.0 / (*this)[0][0];
35 Matrix<Obj,1>& transpose() {
38 Matrix<Obj,1>& operator*=(
const Matrix<Obj,1> &mat) {
39 (*this)[0][0] *= mat[0][0];
43 Matrix<Obj,1> cofactor() {
51 template<
typename Obj,
unsigned int SIZE>
52 MATRIX::Matrix(
const std::vector<std::vector<Obj> > &covarianceMatrix) {
53 for(
unsigned int i=0; i < SIZE; i++)
54 for(
unsigned int j=0; j < SIZE; j++)
55 (*
this)[i][j] = covarianceMatrix[i][j] / (sqrt(covarianceMatrix[i][i] * covarianceMatrix[j][j]));
59 template<
typename Obj,
unsigned int SIZE>
60 MATRIX & MATRIX::fill(Obj obj) {
61 for (
unsigned int i=0; i<SIZE; ++i)
67 template<
typename Obj,
unsigned int SIZE>
68 MATRIX& MATRIX::operator+=(
const MATRIX &mat) {
69 for (
unsigned int i=0; i<SIZE; ++i)
75 template<
typename Obj,
unsigned int SIZE>
76 MATRIX& MATRIX::operator-=(
const MATRIX &mat) {
77 for (
unsigned int i=0; i<SIZE; ++i)
83 template<
typename Obj,
unsigned int SIZE>
84 bool MATRIX::operator==(
const MATRIX &mat)
const {
85 for (
unsigned int i=0; i<SIZE; ++i) {
86 if (((*
this)[i] != mat[i]))
93 template<
typename Obj,
unsigned int SIZE>
94 bool MATRIX::operator!=(
const MATRIX &mat)
const {
95 for (
unsigned int i=0; i<SIZE; ++i) {
96 if (((*
this)[i] != mat[i]))
103 template<
typename Obj,
unsigned int SIZE>
104 MATRIX & MATRIX::operator*=(
const MATRIX &mat) {
105 (*this) = (*this) * mat;
109 template<
typename Obj,
unsigned int SIZE>
110 MATRIX & MATRIX::operator*=(
const Obj &obj) {
111 for (
unsigned int i=0; i<SIZE; ++i)
117 template<
typename Obj,
unsigned int SIZE>
118 MATRIX & MATRIX::operator/=(
const MATRIX &mat) {
125 template<
typename Obj,
unsigned int SIZE>
126 MATRIX & MATRIX::operator/=(
const Obj &obj) {
127 for (
unsigned int i=0; i<SIZE; ++i)
133 template<
typename Obj,
unsigned int SIZE>
134 Obj MATRIX::determinant()
const {
137 return (*
this)[0][0] * (*this)[1][1] - (*this)[1][0] * (*this)[0][1];
141 return (*
this)[0][0] * ((*this)[1][1]*(*this)[2][2] - (*this)[1][2] * (*this)[2][1])
142 - (*
this)[0][1] * ((*this)[1][0]*(*this)[2][2] - (*this)[1][2] * (*this)[2][0])
143 + (*
this)[0][2] * ((*this)[1][0]*(*this)[2][1] - (*this)[1][1] * (*this)[2][0]) ;
150 for (
unsigned int j1=0; j1<SIZE; ++j1) {
151 tlp::Matrix<Obj, SIZE - 1> m;
153 for (
unsigned int i=1; i<SIZE; i++) {
156 for (
unsigned int j=0; j<SIZE; ++j) {
160 m[i-1][j2] = (*this)[i][j];
166 det += (*this)[0][j1] * m.determinant();
168 det -= (*this)[0][j1] * m.determinant();
175 template<
typename Obj,
unsigned int SIZE>
176 MATRIX MATRIX::cofactor()
const {
181 (result)[0][0] = (*
this)[1][1];
182 (result)[0][1] = - (*
this)[1][0];
183 (result)[1][0] = - (*
this)[0][1];
184 (result)[1][1] = (*
this)[0][0];
188 (result)[0][0] = (*
this)[1][1]*(*this)[2][2] - (*this)[1][2]*(*this)[2][1];
189 (result)[0][1] = - ((*
this)[1][0]*(*this)[2][2] - (*this)[2][0]*(*this)[1][2]);
190 (result)[0][2] = (*
this)[1][0]*(*this)[2][1] - (*this)[1][1]*(*this)[2][0];
191 (result)[1][0] = - ((*
this)[0][1]*(*this)[2][2] - (*this)[0][2]*(*this)[2][1]);
192 (result)[1][1] = (*
this)[0][0]*(*this)[2][2] - (*this)[0][2]*(*this)[2][0];
193 (result)[1][2] = - ((*
this)[0][0]*(*this)[2][1] - (*this)[0][1]*(*this)[2][0]);
194 (result)[2][0] = (*
this)[0][1]*(*this)[1][2] - (*this)[0][2]*(*this)[1][1];
195 (result)[2][1] = - ((*
this)[0][0]*(*this)[1][2] - (*this)[0][2]*(*this)[1][0]);
196 (result)[2][2] = (*
this)[0][0]*(*this)[1][1] - (*this)[0][1]*(*this)[1][0];
201 tlp::Matrix<Obj,SIZE - 1> c;
203 for (
unsigned int j=0; j<SIZE; ++j) {
204 for (
unsigned int i=0; i<SIZE; ++i) {
207 for (
unsigned int ii=0; ii<SIZE; ++ii) {
213 for (
unsigned int jj=0; jj<SIZE; jj++) {
217 c[i1][j1] = (*this)[ii][jj];
224 if ((i+j) & 1) result[i][j]=c.determinant();
225 else result[i][j]=-c.determinant();
235 template<
typename Obj,
unsigned int SIZE>
236 MATRIX & MATRIX::transpose() {
239 for (
unsigned int i=1; i<SIZE; ++i) {
240 for (
unsigned int j=0; j<i; ++j) {
242 (*this)[i][j] = (*this)[j][i];
250 template<
typename Obj,
unsigned int SIZE>
251 MATRIX & MATRIX::inverse() {
252 (*this) = (*this).cofactor().transpose() /= (*this).determinant();
256 template<
typename Obj,
unsigned int SIZE>
257 MATRIX tlp::operator+(
const MATRIX &mat1 ,
const MATRIX &mat2) {
258 return MATRIX(mat1)+=mat2;
261 template<
typename Obj,
unsigned int SIZE>
262 MATRIX tlp::operator-(
const MATRIX &mat1 ,
const MATRIX &mat2) {
263 return MATRIX(mat1)-=mat2;
266 template<
typename Obj,
unsigned int SIZE>
267 MATRIX tlp::operator*(
const MATRIX &mat1 ,
const MATRIX &mat2) {
270 for (
unsigned int i=0; i<SIZE; ++i)
271 for (
unsigned int j=0; j<SIZE; ++j) {
272 result[i][j] = mat1[i][0] * mat2[0][j];
274 for (
unsigned int k=1; k<SIZE; ++k)
275 result[i][j] += mat1[i][k] * mat2[k][j];
281 template<
typename Obj,
unsigned int SIZE>
282 MATRIX MATRIX::operator/(
const MATRIX &mat2)
const {
283 return MATRIX(*
this)/=mat2;
286 template<
typename Obj,
unsigned int SIZE>
287 MATRIX MATRIX::operator/(
const Obj &obj)
const {
288 return MATRIX(*
this) /= obj;
291 template<
typename Obj,
unsigned int SIZE>
292 MATRIX tlp::operator*(
const MATRIX &mat ,
const Obj &obj) {
293 return MATRIX(mat) *= obj;
296 template<
typename Obj,
unsigned int SIZE>
297 tlp::Vector<Obj, SIZE> tlp::operator*(
const MATRIX &mat ,
const tlp::Vector<Obj, SIZE> &vec) {
298 tlp::Vector<Obj,SIZE> result;
300 for (
unsigned int row=0; row<SIZE; ++row) {
301 result[row] = mat[row][0] * vec[0];
304 for (
unsigned int col=1; col<SIZE; ++col) {
305 for (
unsigned int row=0; row<SIZE; ++row) {
306 result[row] += mat[row][col] * vec[col];
313 template<
typename Obj,
unsigned int SIZE>
314 tlp::Vector<Obj,SIZE> tlp::operator*(
const tlp::Vector<Obj,SIZE> &vec,
const MATRIX &mat) {
315 tlp::Vector<Obj,SIZE> result;
317 for (
unsigned int row=0; row<SIZE; ++row) {
318 result[row] = mat[0][row] * vec[0];
321 for (
unsigned int col=1; col<SIZE; ++col) {
322 for (
unsigned int row=0; row<SIZE; ++row) {
323 result[row] += mat[col][row] * vec[col];
331 template<
typename Obj,
unsigned int SIZE>
332 tlp::Vector<Obj, SIZE> MATRIX::powerIteration(
const unsigned int nIterations)
const {
333 tlp::Vector<Obj, SIZE> iteration;
335 for(
unsigned int i=0; i < SIZE; i++)
338 for(
unsigned int i=0; i < nIterations; i++) {
339 iteration = (*this) * iteration;
341 iteration /= iteration.norm();
349 template<
typename Obj,
unsigned int SIZE>
350 bool MATRIX::simplify(tlp::Matrix<Obj, 2> &simplifiedMatrix)
const {
352 tlp::warning() <<
"Computation allowed only for 3x3 Matrices. Yours sizes : " << SIZE <<
"x" << SIZE << std::endl;
383 coeff = (*this)[1][2] / (*this)[0][2];
388 simplifiedMatrix[0][0] = (*this)[1][0] - (coeff * (*this)[0][0]);
389 simplifiedMatrix[0][1] = (*this)[1][1] - (coeff * (*this)[0][1]);
394 coeff = (*this)[2][1] / (*this)[0][1];
399 simplifiedMatrix[1][0] = (*this)[2][0] - (coeff * (*this)[0][0]);
400 simplifiedMatrix[1][1] = (*this)[2][2] - (coeff * (*this)[0][2]);
407 template<
typename Obj,
unsigned int SIZE>
408 bool MATRIX::computeEigenVector(
const float x, tlp::Vector<Obj, 3> &eigenVector)
const {
410 tlp::warning() <<
"Computation allowed only for 2x2 Matrices. Yours sizes : " << SIZE <<
"x" << SIZE << std::endl;
435 eigenVector[1] = (-a * x) / b;
436 eigenVector[2] = (-c * x) / d;