Andrey Kolobov
8 years ago
committed by
Andrew Tridgell
3 changed files with 143 additions and 0 deletions
@ -0,0 +1,60 @@
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* N dimensional matrix operations |
||||
*/ |
||||
|
||||
#pragma GCC optimize("O3") |
||||
|
||||
#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++) { |
||||
v[i][j] = (v[i][j] + v[j][i]) * 0.5; |
||||
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); |
@ -0,0 +1,48 @@
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* N dimensional matrix operations |
||||
*/ |
||||
|
||||
#pragma once |
||||
|
||||
#include "math.h" |
||||
#include <stdint.h> |
||||
#include "vectorN.h" |
||||
|
||||
template <typename T, uint8_t N> |
||||
class VectorN; |
||||
|
||||
|
||||
template <typename T, uint8_t N> |
||||
class MatrixN { |
||||
|
||||
friend class VectorN<T,N>; |
||||
|
||||
public: |
||||
// constructor from zeros
|
||||
MatrixN<T,N>(void) { |
||||
memset(v, 0, sizeof(v));
|
||||
} |
||||
|
||||
// constructor from 4 diagonals
|
||||
MatrixN<T,N>(const float d[N]) { |
||||
memset(v, 0, sizeof(v)); |
||||
for (uint8_t i = 0; i < N; i++) { |
||||
v[i][i] = d[i]; |
||||
} |
||||
} |
||||
|
||||
// multiply two vectors to give a matrix, in-place
|
||||
void mult(const VectorN<T,N> &A, const VectorN<T,N> &B); |
||||
|
||||
// subtract B from the matrix
|
||||
MatrixN<T,N> &operator -=(const MatrixN<T,N> &B); |
||||
|
||||
// add B to the matrix
|
||||
MatrixN<T,N> &operator +=(const MatrixN<T,N> &B); |
||||
|
||||
// Matrix symmetry routine
|
||||
void force_symmetry(void); |
||||
|
||||
private: |
||||
T v[N][N]; |
||||
}; |
Loading…
Reference in new issue