21#ifndef M5_UNIT_IMU_MATH_MATRIX_HPP
22#define M5_UNIT_IMU_MATH_MATRIX_HPP
38template <std::
size_t R, std::
size_t C>
44 static constexpr std::size_t
rows()
49 static constexpr std::size_t
cols()
55 inline float&
at(
const std::size_t row,
const std::size_t col)
57 return _data[row][col];
60 inline float at(
const std::size_t row,
const std::size_t col)
const
62 return _data[row][col];
65 inline float&
operator()(
const std::size_t row,
const std::size_t col)
70 inline float operator()(
const std::size_t row,
const std::size_t col)
const
79 for (std::size_t r = 0; r < R; ++r) {
80 for (std::size_t c = 0; c < C; ++c) {
81 result._data[r][c] = _data[r][c] + rhs._data[r][c];
90 for (std::size_t r = 0; r < R; ++r) {
91 for (std::size_t c = 0; c < C; ++c) {
92 result._data[r][c] = _data[r][c] - rhs._data[r][c];
101 for (std::size_t r = 0; r < R; ++r) {
102 for (std::size_t c = 0; c < C; ++c) {
103 result._data[r][c] = _data[r][c] * scalar;
110 template <std::
size_t K>
114 for (std::size_t r = 0; r < R; ++r) {
115 for (std::size_t k = 0; k < K; ++k) {
117 for (std::size_t c = 0; c < C; ++c) {
118 sum += _data[r][c] * rhs(c, k);
130 for (std::size_t r = 0; r < R; ++r) {
131 for (std::size_t c = 0; c < C; ++c) {
132 result(c, r) = _data[r][c];
141 static_assert(R == C,
"Matrix::identity() requires a square matrix");
143 for (std::size_t i = 0; i < R; ++i) {
144 result._data[i][i] = 1.0f;
150 std::array<std::array<float, C>, R> _data{};
167 const float a = m(0, 0);
168 const float b = m(0, 1);
169 const float c = m(0, 2);
170 const float d = m(1, 0);
171 const float e = m(1, 1);
172 const float f = m(1, 2);
173 const float g = m(2, 0);
174 const float h = m(2, 1);
175 const float i = m(2, 2);
177 const float cof00 = e * i - f * h;
178 const float cof01 = -(d * i - f * g);
179 const float cof02 = d * h - e * g;
180 const float cof10 = -(b * i - c * h);
181 const float cof11 = a * i - c * g;
182 const float cof12 = -(a * h - b * g);
183 const float cof20 = b * f - c * e;
184 const float cof21 = -(a * f - c * d);
185 const float cof22 = a * e - b * d;
187 const float det = a * cof00 + b * cof01 + c * cof02;
191 const float inv_det = 1.0f / det;
194 out(0, 0) = cof00 * inv_det;
195 out(0, 1) = cof10 * inv_det;
196 out(0, 2) = cof20 * inv_det;
197 out(1, 0) = cof01 * inv_det;
198 out(1, 1) = cof11 * inv_det;
199 out(1, 2) = cof21 * inv_det;
200 out(2, 0) = cof02 * inv_det;
201 out(2, 1) = cof12 * inv_det;
202 out(2, 2) = cof22 * inv_det;
213template <std::
size_t N>
216 std::array<std::array<float, N>, N> l{};
218 for (std::size_t row = 0; row < N; ++row) {
219 for (std::size_t col = 0; col <= row; ++col) {
220 float sum = a(row, col);
221 for (std::size_t k = 0; k < col; ++k) {
222 sum -= l[row][k] * l[col][k];
228 l[row][col] = std::sqrt(sum);
230 l[row][col] = sum / l[col][col];
236 std::array<float, N> y{};
237 for (std::size_t row = 0; row < N; ++row) {
238 float sum = b(row, 0);
239 for (std::size_t k = 0; k < row; ++k) {
240 sum -= l[row][k] * y[k];
242 y[row] = sum / l[row][row];
246 for (std::size_t ridx = N; ridx-- > 0;) {
248 for (std::size_t k = ridx + 1; k < N; ++k) {
249 sum -= l[k][ridx] * x(k, 0);
251 x(ridx, 0) = sum / l[ridx][ridx];
Fixed-size (R rows x C columns) float matrix, std::array backed (no heap allocation)
Definition matrix.hpp:39
Matrix< R, C > operator+(const Matrix< R, C > &rhs) const
Component-wise addition.
Definition matrix.hpp:76
static Matrix< R, C > identity()
Identity matrix (square matrices only)
Definition matrix.hpp:139
static constexpr std::size_t rows()
Number of rows.
Definition matrix.hpp:44
float at(const std::size_t row, const std::size_t col) const
Element access (read-only)
Definition matrix.hpp:60
Matrix< R, C > operator*(const float scalar) const
Scalar multiplication.
Definition matrix.hpp:98
float & at(const std::size_t row, const std::size_t col)
Element access (mutable)
Definition matrix.hpp:55
float operator()(const std::size_t row, const std::size_t col) const
Element access via operator() (read-only)
Definition matrix.hpp:70
Matrix< R, K > operator*(const Matrix< C, K > &rhs) const
Matrix product (this [R x C] times rhs [C x K] -> [R x K])
Definition matrix.hpp:111
static constexpr std::size_t cols()
Number of columns.
Definition matrix.hpp:49
float & operator()(const std::size_t row, const std::size_t col)
Element access via operator() (mutable)
Definition matrix.hpp:65
Matrix< R, C > operator-(const Matrix< R, C > &rhs) const
Component-wise subtraction.
Definition matrix.hpp:87
Matrix< C, R > transposed() const
Transpose (C x R)
Definition matrix.hpp:127
constexpr float MATRIX_ZERO_EPSILON
Magnitudes below this are treated as singular/non-positive-definite (matrix safety)
Definition matrix.hpp:32
bool invert3x3(const Matrix< 3, 3 > &m, Matrix< 3, 3 > &out)
Closed-form inverse of a 3x3 matrix.
Definition matrix.hpp:165
bool cholesky_solve(const Matrix< N, N > &a, const Matrix< N, 1 > &b, Matrix< N, 1 > &x)
Solve A*x = b for a symmetric positive-definite A via Cholesky decomposition.
Definition matrix.hpp:214
Top level namespace of M5Stack.
Definition ahrs_controller.cpp:26