2017-02-26 19:24:35 -04:00
|
|
|
/*
|
|
|
|
* N dimensional matrix operations
|
|
|
|
*/
|
|
|
|
|
2019-09-27 17:45:12 -03:00
|
|
|
#pragma GCC optimize("O2")
|
2017-02-26 19:24:35 -04:00
|
|
|
|
|
|
|
#include "matrixN.h"
|
|
|
|
|
|
|
|
|
|
|
|
// multiply two vectors to give a matrix, in-place
|
|
|
|
template <typename T, uint8_t N>
|
|
|
|
void MatrixN<T,N>::mult(const VectorN<T,N> &A, const VectorN<T,N> &B)
|
|
|
|
{
|
|
|
|
for (uint8_t i = 0; i < N; i++) {
|
|
|
|
for (uint8_t j = 0; j < N; j++) {
|
|
|
|
v[i][j] = A[i] * B[j];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// subtract B from the matrix
|
|
|
|
template <typename T, uint8_t N>
|
|
|
|
MatrixN<T,N> &MatrixN<T,N>::operator -=(const MatrixN<T,N> &B)
|
|
|
|
{
|
|
|
|
for (uint8_t i = 0; i < N; i++) {
|
|
|
|
for (uint8_t j = 0; j < N; j++) {
|
|
|
|
v[i][j] -= B.v[i][j];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// add B to the matrix
|
|
|
|
template <typename T, uint8_t N>
|
|
|
|
MatrixN<T,N> &MatrixN<T,N>::operator +=(const MatrixN<T,N> &B)
|
|
|
|
{
|
|
|
|
for (uint8_t i = 0; i < N; i++) {
|
|
|
|
for (uint8_t j = 0; j < N; j++) {
|
|
|
|
v[i][j] += B.v[i][j];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Matrix symmetry routine
|
|
|
|
template <typename T, uint8_t N>
|
|
|
|
void MatrixN<T,N>::force_symmetry(void)
|
|
|
|
{
|
|
|
|
for (uint8_t i = 0; i < N; i++) {
|
|
|
|
for (uint8_t j = 0; j < (i - 1); j++) {
|
2019-04-04 19:31:02 -03:00
|
|
|
v[i][j] = (v[i][j] + v[j][i]) / 2;
|
2017-02-26 19:24:35 -04:00
|
|
|
v[j][i] = v[i][j];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template void MatrixN<float,4>::mult(const VectorN<float,4> &A, const VectorN<float,4> &B);
|
|
|
|
template MatrixN<float,4> &MatrixN<float,4>::operator -=(const MatrixN<float,4> &B);
|
|
|
|
template MatrixN<float,4> &MatrixN<float,4>::operator +=(const MatrixN<float,4> &B);
|
|
|
|
template void MatrixN<float,4>::force_symmetry(void);
|