Hummingbird Flight Software
Flight software for the Hummingbird FCU quadcopter flight controller. Designed to run on the Teensy 4.1. Developed with VSCode+PlatformIO.
matrix_math.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // MATRIX MATH LIBRARY FOR TEENSY 4.1
3 //
4 // Code By: Michael Wrona
5 // Created: 14 Feb 2021
6 // ----------------------------------------------------------------------------
65 #pragma once
66 
67 #include <Arduino.h>
68 #include <math.h>
69 #include "hummingbird_config.h"
70 #include "maths/math_functs.h"
71 #include "maths/matrices.h"
72 #include "maths/vectors.h"
73 
74 
75 #if defined(DEBUG) && defined(DEBUG_PORT)
76  /* Enable/disable printing debug messages to DEBUG_PORT */
77  #define MATRIX_MATH_DEBUG
78 #endif
79 
80 
81 /* VECTOR FUNCTIONS */
82 void VectorfFill(float *vec, float fill, size_t n);
83 void VectorfAdd(float *c, float *a, float *b, size_t n);
84 void VectorfAccumulate(float *a, float *b, size_t n);
85 void VectorfSubtract(float *c, float *a, float *b, size_t n);
86 
87 /* MATRIX FUNCTIONS */
88 void MatrixFill(float fill, float *A, size_t rows, size_t cols);
89 void MatrixTranspose(float *A, float *At, size_t arows, size_t acols);
90 void MatrixTransposeSquare(float *A, size_t n);
91 
92 // Addition
93 void MatrixAdd(float *C, float *A, float *B, size_t rows, size_t cols);
94 void MatrixAddIdentity(float *A, size_t rows, size_t cols);
95 void MatrixAccumulate(float *A, float *B, size_t rows, size_t cols);
96 
97 // Subtraction
98 void MatrixSubtract(float *C, float *A, float *B, size_t rows, size_t cols);
99 void MatrixSubtractIdentity(float *A, size_t rows, size_t cols);
100 void MatrixSubAccumulate(float *A, float *B, size_t rows, size_t cols);
101 void MatrixNegate(float *A, size_t rows, size_t cols);
102 
103 // Multiplication
104 void MatrixVectorfMult(float *outVec, float *A, float *b, size_t rows, size_t cols);
105 void MatrixMultiply(float *C, float *A, float *B, size_t aRows, size_t aCols, size_t bRows, size_t bCols);
106 void MatrixMultiply_ABt(float *C, float *A, float *B, size_t arows, size_t acols, size_t brows);
107 
108 // Invert
109 bool MatrixInverseCholesky(float *A, size_t n);
110 bool _MatrixCholeskyDecomp(float *A, size_t n);
111 bool _MatrixLowerTriangularInverse(float *A, size_t n);
112 // bool MatrixIsPosDef(float *A, size_t rows, size_t cols);
113 
114 // #ifdef MATRIX_MATH_DEBUG
115  // void PrintVectorf(float *a, size_t n);
116  // void PrintMatrix(float *A, size_t r, size_t c);
117 // #endif
void VectorfAccumulate(float *a, float *b, size_t n)
a <- a + b.
Definition: matrix_math.cpp:114
void MatrixSubAccumulate(float *A, float *B, size_t rows, size_t cols)
A <- A - B.
Definition: matrix_math.cpp:353
void MatrixSubtractIdentity(float *A, size_t rows, size_t cols)
A <- I - A.
Definition: matrix_math.cpp:285
void VectorfFill(float *vec, float fill, size_t n)
USES SINGLE-PRECISION FLOATS!
Definition: matrix_math.cpp:74
void VectorfAdd(float *c, float *a, float *b, size_t n)
Add two vectors together and output result to a new vector.
Definition: matrix_math.cpp:95
void MatrixAdd(float *C, float *A, float *B, size_t rows, size_t cols)
C <- A + B.
Definition: matrix_math.cpp:244
void MatrixTransposeSquare(float *A, size_t n)
Compute the transpose of a SQUARE (n, n) matrix in-place.
Definition: matrix_math.cpp:211
void MatrixAccumulate(float *A, float *B, size_t rows, size_t cols)
A <- A + B.
Definition: matrix_math.cpp:308
void MatrixTranspose(float *A, float *At, size_t arows, size_t acols)
Compute the transpose of a matrix and output it to another one.
Definition: matrix_math.cpp:184
void MatrixSubtract(float *C, float *A, float *B, size_t rows, size_t cols)
C <- A - B.
Definition: matrix_math.cpp:331
bool _MatrixCholeskyDecomp(float *A, size_t n)
Perform Cholesky decomposition on a square, symmetric, positive definite matrix.
Definition: matrix_math.cpp:548
void VectorfSubtract(float *c, float *a, float *b, size_t n)
Subtract two vectors and output result to a new vector.
Definition: matrix_math.cpp:135
bool _MatrixLowerTriangularInverse(float *A, size_t n)
Compute the inverse of a lower triangular matrix.
Definition: matrix_math.cpp:609
void MatrixAddIdentity(float *A, size_t rows, size_t cols)
A <- I + A.
Definition: matrix_math.cpp:264
void MatrixFill(float fill, float *A, size_t rows, size_t cols)
Fill a matrix with a specified value.
Definition: matrix_math.cpp:156
void MatrixVectorfMult(float *outVec, float *A, float *b, size_t rows, size_t cols)
c <- A * b.
Definition: matrix_math.cpp:401
void MatrixMultiply(float *C, float *A, float *B, size_t aRows, size_t aCols, size_t bRows, size_t bCols)
C <- A * B.
Definition: matrix_math.cpp:431
void MatrixMultiply_ABt(float *C, float *A, float *B, size_t arows, size_t acols, size_t brows)
C <- A * B^T.
Definition: matrix_math.cpp:466
bool MatrixInverseCholesky(float *A, size_t n)
Compute the inverse of matrix A via Cholesky decomposition.
Definition: matrix_math.cpp:499
void MatrixNegate(float *A, size_t rows, size_t cols)
Multiply all elements in a matrix by -1.
Definition: matrix_math.cpp:373