24template <
typename Obj>
25class 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() {
50template <
typename Obj,
size_t SIZE>
51MATRIX::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]));
59template <
typename Obj,
size_t SIZE>
60MATRIX &MATRIX::fill(Obj obj) {
61 for (
size_t i = 0; i < SIZE; ++i)
67template <
typename Obj,
size_t SIZE>
68MATRIX &MATRIX::operator+=(
const MATRIX &mat) {
69 for (
size_t i = 0; i < SIZE; ++i)
75template <
typename Obj,
size_t SIZE>
76MATRIX &MATRIX::operator-=(
const MATRIX &mat) {
77 for (
size_t i = 0; i < SIZE; ++i)
83template <
typename Obj,
size_t SIZE>
84bool MATRIX::operator==(
const MATRIX &mat)
const {
85 for (
size_t i = 0; i < SIZE; ++i) {
86 if (((*
this)[i] != mat[i]))
93template <
typename Obj,
size_t SIZE>
94bool MATRIX::operator!=(
const MATRIX &mat)
const {
95 for (
size_t i = 0; i < SIZE; ++i) {
96 if (((*
this)[i] != mat[i]))
103template <
typename Obj,
size_t SIZE>
104MATRIX &MATRIX::operator*=(
const MATRIX &mat) {
105 (*this) = (*this) * mat;
109template <
typename Obj,
size_t SIZE>
110MATRIX &MATRIX::operator*=(
const Obj &obj) {
111 for (
size_t i = 0; i < SIZE; ++i)
117template <
typename Obj,
size_t SIZE>
118MATRIX &MATRIX::operator/=(
const MATRIX &mat) {
125template <
typename Obj,
size_t SIZE>
126MATRIX &MATRIX::operator/=(
const Obj &obj) {
127 for (
size_t i = 0; i < SIZE; ++i)
133template <
typename Obj,
size_t SIZE>
134Obj 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();
175template <
typename Obj,
size_t SIZE>
176MATRIX 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();
237template <
typename Obj,
size_t SIZE>
238MATRIX &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];
252template <
typename Obj,
size_t SIZE>
253MATRIX &MATRIX::inverse() {
254 (*this) = (*this).cofactor().transpose() /= (*this).determinant();
258template <
typename Obj,
size_t SIZE>
259MATRIX tlp::operator+(
const MATRIX &mat1,
const MATRIX &mat2) {
260 return MATRIX(mat1) += mat2;
263template <
typename Obj,
size_t SIZE>
264MATRIX tlp::operator-(
const MATRIX &mat1,
const MATRIX &mat2) {
265 return MATRIX(mat1) -= mat2;
268template <
typename Obj,
size_t SIZE>
269MATRIX 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];
283template <
typename Obj,
size_t SIZE>
284MATRIX MATRIX::operator/(
const MATRIX &mat2)
const {
285 return MATRIX(*
this) /= mat2;
288template <
typename Obj,
size_t SIZE>
289MATRIX MATRIX::operator/(
const Obj &obj)
const {
290 return MATRIX(*
this) /= obj;
293template <
typename Obj,
size_t SIZE>
294MATRIX tlp::operator*(
const MATRIX &mat,
const Obj &obj) {
295 return MATRIX(mat) *= obj;
298template <
typename Obj,
size_t SIZE>
299tlp::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];
315template <
typename Obj,
size_t SIZE>
316tlp::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];
333template <
typename Obj,
size_t SIZE>
334tlp::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();
351template <
typename Obj,
size_t SIZE>
352bool 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]);
410template <
typename Obj,
size_t SIZE>
411bool 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;