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() {
50 template <
typename Obj,
size_t SIZE>
51 MATRIX::Matrix(
const std::vector<std::vector<Obj>> &covarianceMatrix) {
52 for (
size_t i = 0; i < SIZE; i++)
53 for (
size_t j = 0; j < SIZE; j++)
55 covarianceMatrix[i][j] / (sqrt(covarianceMatrix[i][i] * covarianceMatrix[j][j]));
59 template <
typename Obj,
size_t SIZE>
60 MATRIX &MATRIX::fill(Obj obj) {
61 for (
size_t i = 0; i < SIZE; ++i)
67 template <
typename Obj,
size_t SIZE>
68 MATRIX &MATRIX::operator+=(
const MATRIX &mat) {
69 for (
size_t i = 0; i < SIZE; ++i)
75 template <
typename Obj,
size_t SIZE>
76 MATRIX &MATRIX::operator-=(
const MATRIX &mat) {
77 for (
size_t i = 0; i < SIZE; ++i)
83 template <
typename Obj,
size_t SIZE>
84 bool MATRIX::operator==(
const MATRIX &mat)
const {
85 for (
size_t i = 0; i < SIZE; ++i) {
86 if (((*
this)[i] != mat[i]))
93 template <
typename Obj,
size_t SIZE>
94 bool MATRIX::operator!=(
const MATRIX &mat)
const {
95 for (
size_t i = 0; i < SIZE; ++i) {
96 if (((*
this)[i] != mat[i]))
103 template <
typename Obj,
size_t SIZE>
104 MATRIX &MATRIX::operator*=(
const MATRIX &mat) {
105 (*this) = (*this) * mat;
109 template <
typename Obj,
size_t SIZE>
110 MATRIX &MATRIX::operator*=(
const Obj &obj) {
111 for (
size_t i = 0; i < SIZE; ++i)
117 template <
typename Obj,
size_t SIZE>
118 MATRIX &MATRIX::operator/=(
const MATRIX &mat) {
125 template <
typename Obj,
size_t SIZE>
126 MATRIX &MATRIX::operator/=(
const Obj &obj) {
127 for (
size_t i = 0; i < SIZE; ++i)
133 template <
typename Obj,
size_t 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 (
size_t j1 = 0; j1 < SIZE; ++j1) {
151 tlp::Matrix<Obj, SIZE - 1> m;
153 for (
size_t i = 1; i < SIZE; i++) {
156 for (
size_t 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,
size_t 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 (
size_t j = 0; j < SIZE; ++j) {
204 for (
size_t i = 0; i < SIZE; ++i) {
207 for (
size_t ii = 0; ii < SIZE; ++ii) {
213 for (
size_t jj = 0; jj < SIZE; jj++) {
217 c[i1][j1] = (*this)[ii][jj];
225 result[i][j] = c.determinant();
227 result[i][j] = -c.determinant();
237 template <
typename Obj,
size_t SIZE>
238 MATRIX &MATRIX::transpose() {
241 for (
size_t i = 1; i < SIZE; ++i) {
242 for (
size_t j = 0; j < i; ++j) {
244 (*this)[i][j] = (*this)[j][i];
252 template <
typename Obj,
size_t SIZE>
253 MATRIX &MATRIX::inverse() {
254 (*this) = (*this).cofactor().transpose() /= (*this).determinant();
258 template <
typename Obj,
size_t SIZE>
259 MATRIX tlp::operator+(
const MATRIX &mat1,
const MATRIX &mat2) {
260 return MATRIX(mat1) += mat2;
263 template <
typename Obj,
size_t SIZE>
264 MATRIX tlp::operator-(
const MATRIX &mat1,
const MATRIX &mat2) {
265 return MATRIX(mat1) -= mat2;
268 template <
typename Obj,
size_t SIZE>
269 MATRIX tlp::operator*(
const MATRIX &mat1,
const MATRIX &mat2) {
272 for (
size_t i = 0; i < SIZE; ++i)
273 for (
size_t j = 0; j < SIZE; ++j) {
274 result[i][j] = mat1[i][0] * mat2[0][j];
276 for (
size_t k = 1; k < SIZE; ++k)
277 result[i][j] += mat1[i][k] * mat2[k][j];
283 template <
typename Obj,
size_t SIZE>
284 MATRIX MATRIX::operator/(
const MATRIX &mat2)
const {
285 return MATRIX(*
this) /= mat2;
288 template <
typename Obj,
size_t SIZE>
289 MATRIX MATRIX::operator/(
const Obj &obj)
const {
290 return MATRIX(*
this) /= obj;
293 template <
typename Obj,
size_t SIZE>
294 MATRIX tlp::operator*(
const MATRIX &mat,
const Obj &obj) {
295 return MATRIX(mat) *= obj;
298 template <
typename Obj,
size_t 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 (
size_t row = 0; row < SIZE; ++row) {
303 result[row] = mat[row][0] * vec[0];
306 for (
size_t col = 1; col < SIZE; ++col) {
307 for (
size_t row = 0; row < SIZE; ++row) {
308 result[row] += mat[row][col] * vec[col];
315 template <
typename Obj,
size_t 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 (
size_t row = 0; row < SIZE; ++row) {
320 result[row] = mat[0][row] * vec[0];
323 for (
size_t col = 1; col < SIZE; ++col) {
324 for (
size_t row = 0; row < SIZE; ++row) {
325 result[row] += mat[col][row] * vec[col];
333 template <
typename Obj,
size_t SIZE>
334 tlp::Vector<Obj, SIZE> MATRIX::powerIteration(
const unsigned int nIterations)
const {
335 tlp::Vector<Obj, SIZE> iteration;
337 for (
size_t 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,
size_t SIZE>
352 bool MATRIX::simplify(tlp::Matrix<Obj, 2> &simplifiedMatrix)
const {
354 tlp::warning() <<
"Computation allowed only for 3x3 Matrices. Yours sizes : " << SIZE <<
"x" 355 << SIZE << std::endl;
386 coeff = (*this)[1][2] / (*this)[0][2];
391 simplifiedMatrix[0][0] = (*this)[1][0] - (coeff * (*this)[0][0]);
392 simplifiedMatrix[0][1] = (*this)[1][1] - (coeff * (*this)[0][1]);
397 coeff = (*this)[2][1] / (*this)[0][1];
402 simplifiedMatrix[1][0] = (*this)[2][0] - (coeff * (*this)[0][0]);
403 simplifiedMatrix[1][1] = (*this)[2][2] - (coeff * (*this)[0][2]);
410 template <
typename Obj,
size_t SIZE>
411 bool MATRIX::computeEigenVector(
const float x, tlp::Vector<Obj, 3> &eigenVector)
const {
413 tlp::warning() <<
"Computation allowed only for 2x2 Matrices. Yours sizes : " << SIZE <<
"x" 414 << SIZE << std::endl;
439 eigenVector[1] = (-a * x) / b;
440 eigenVector[2] = (-c * x) / d;