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 Obj tmpObj = mat1[i][0] * mat2[0][j];
274 for (
unsigned int k=1; k<SIZE; ++k)
275 tmpObj += mat1[i][k] * mat2[k][j];
277 result[i][j] = tmpObj;
283 template<
typename Obj,
unsigned int SIZE>
284 MATRIX MATRIX::operator/(
const MATRIX &mat2)
const {
285 return MATRIX(*
this)/=mat2;
288 template<
typename Obj,
unsigned int SIZE>
289 MATRIX MATRIX::operator/(
const Obj &obj)
const {
290 return MATRIX(*
this) /= obj;
293 template<
typename Obj,
unsigned int SIZE>
294 MATRIX tlp::operator*(
const MATRIX &mat ,
const Obj &obj) {
295 return MATRIX(mat) *= obj;
298 template<
typename Obj,
unsigned int SIZE>
299 tlp::Vector<Obj, SIZE> tlp::operator*(
const MATRIX &mat ,
const tlp::Vector<Obj, SIZE> &vec) {
300 tlp::Vector<Obj,SIZE> result;
302 for (
unsigned int row=0; row<SIZE; ++row) {
303 result[row] = mat[row][0] * vec[0];
306 for (
unsigned int col=1; col<SIZE; ++col) {
307 for (
unsigned int row=0; row<SIZE; ++row) {
308 result[row] += mat[row][col] * vec[col];
315 template<
typename Obj,
unsigned int SIZE>
316 tlp::Vector<Obj,SIZE> tlp::operator*(
const tlp::Vector<Obj,SIZE> &vec,
const MATRIX &mat) {
317 tlp::Vector<Obj,SIZE> result;
319 for (
unsigned int row=0; row<SIZE; ++row) {
320 result[row] = mat[0][row] * vec[0];
323 for (
unsigned int col=1; col<SIZE; ++col) {
324 for (
unsigned int row=0; row<SIZE; ++row) {
325 result[row] += mat[col][row] * vec[col];
333 template<
typename Obj,
unsigned int SIZE>
334 tlp::Vector<Obj, SIZE> MATRIX::powerIteration(
const unsigned int nIterations)
const {
335 tlp::Vector<Obj, SIZE> iteration;
337 for(
unsigned int i=0; i < SIZE; i++)
340 for(
unsigned int i=0; i < nIterations; i++) {
341 iteration = (*this) * iteration;
343 iteration /= iteration.norm();
351 template<
typename Obj,
unsigned int SIZE>
352 bool MATRIX::simplify(tlp::Matrix<Obj, 2> &simplifiedMatrix)
const {
354 qWarning() <<
"Computation allowed only for 3x3 Matrices. Yours sizes : " << SIZE <<
"x" << SIZE;
385 coeff = (*this)[1][2] / (*this)[0][2];
390 simplifiedMatrix[0][0] = (*this)[1][0] - (coeff * (*this)[0][0]);
391 simplifiedMatrix[0][1] = (*this)[1][1] - (coeff * (*this)[0][1]);
396 coeff = (*this)[2][1] / (*this)[0][1];
401 simplifiedMatrix[1][0] = (*this)[2][0] - (coeff * (*this)[0][0]);
402 simplifiedMatrix[1][1] = (*this)[2][2] - (coeff * (*this)[0][2]);
409 template<
typename Obj,
unsigned int SIZE>
410 bool MATRIX::computeEigenVector(
const float x, tlp::Vector<Obj, 3> &eigenVector)
const {
412 qWarning() <<
"Computation allowed only for 2x2 Matrices. Yours sizes : " << SIZE <<
"x" << SIZE;
437 eigenVector[1] = (-a * x) / b;
438 eigenVector[2] = (-c * x) / d;