STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
Matrix.hpp
Go to the documentation of this file.
1#ifndef COMMS_MATRIX_HPP
2#define COMMS_MATRIX_HPP
3
4#include <algorithm>
5#include <cmath>
6#include <cstddef>
7#include <cstring>
8#include <stdexcept>
9
10namespace comms {
11namespace math {
12
13/**
14 * @brief Matrix operations for sensor calibration and filtering
15 *
16 * Standalone implementation for DAQ system.
17 * Provides essential matrix operations needed for calibration algorithms.
18 */
19
20// Matrix multiplication: C = A * B
21// A: m x n, B: n x p, C: m x p
22inline void matrix_multiply(const double* A, size_t m, size_t n, const double* B, size_t p,
23 double* C) {
24 for (size_t i = 0; i < m; i++) {
25 for (size_t j = 0; j < p; j++) {
26 double sum = 0.0;
27 for (size_t k = 0; k < n; k++) {
28 sum += A[i * n + k] * B[k * p + j];
29 }
30 C[i * p + j] = sum;
31 }
32 }
33}
34
35// Matrix transpose: B = A^T
36// A: m x n, B: n x m
37inline void matrix_transpose(const double* A, size_t m, size_t n, double* B) {
38 for (size_t i = 0; i < m; i++) {
39 for (size_t j = 0; j < n; j++) {
40 B[j * m + i] = A[i * n + j];
41 }
42 }
43}
44
45// Matrix addition: C = A + B
46// All matrices: m x n
47inline void matrix_add(const double* A, const double* B, size_t m, size_t n, double* C) {
48 for (size_t i = 0; i < m * n; i++) {
49 C[i] = A[i] + B[i];
50 }
51}
52
53// Matrix subtraction: C = A - B
54// All matrices: m x n
55inline void matrix_subtract(const double* A, const double* B, size_t m, size_t n, double* C) {
56 for (size_t i = 0; i < m * n; i++) {
57 C[i] = A[i] - B[i];
58 }
59}
60
61// Scalar multiplication: B = alpha * A
62// A, B: m x n
63inline void matrix_scale(const double* A, double alpha, size_t m, size_t n, double* B) {
64 for (size_t i = 0; i < m * n; i++) {
65 B[i] = alpha * A[i];
66 }
67}
68
69// Initialize matrix to zeros
70inline void matrix_zeros(double* A, size_t m, size_t n) {
71 std::fill(A, A + m * n, 0.0);
72}
73
74// Initialize matrix to identity
75inline void matrix_identity(double* A, size_t n) {
76 matrix_zeros(A, n, n);
77 for (size_t i = 0; i < n; i++) {
78 A[i * n + i] = 1.0;
79 }
80}
81
82// Copy matrix: B = A
83inline void matrix_copy(const double* A, size_t m, size_t n, double* B) {
84 std::memcpy(B, A, m * n * sizeof(double));
85}
86
87// Matrix-vector multiplication: y = A * x
88// A: m x n, x: n x 1, y: m x 1
89inline void matrix_vector_multiply(const double* A, size_t m, size_t n, const double* x,
90 double* y) {
91 for (size_t i = 0; i < m; i++) {
92 double sum = 0.0;
93 for (size_t j = 0; j < n; j++) {
94 sum += A[i * n + j] * x[j];
95 }
96 y[i] = sum;
97 }
98}
99
100// Vector dot product
101inline double vector_dot(const double* a, const double* b, size_t n) {
102 double sum = 0.0;
103 for (size_t i = 0; i < n; i++) {
104 sum += a[i] * b[i];
105 }
106 return sum;
107}
108
109// Vector L2 norm
110inline double vector_norm(const double* a, size_t n) {
111 return std::sqrt(vector_dot(a, a, n));
112}
113
114// Matrix trace (sum of diagonal elements)
115inline double matrix_trace(const double* A, size_t n) {
116 double sum = 0.0;
117 for (size_t i = 0; i < n; i++) {
118 sum += A[i * n + i];
119 }
120 return sum;
121}
122
123} // namespace math
124} // namespace comms
125
126#endif // COMMS_MATRIX_HPP
int32_t p
Definition flight_e2e.c:78
*brief create JSON pointer *sa reference_tokens const string_t const string_t & b
Definition json.hpp:13878
*brief create JSON pointer *sa reference_tokens const string_t & a
Definition json.hpp:13877
void matrix_scale(const double *A, double alpha, size_t m, size_t n, double *B)
Definition Matrix.hpp:63
void matrix_identity(double *A, size_t n)
Definition Matrix.hpp:75
void matrix_zeros(double *A, size_t m, size_t n)
Definition Matrix.hpp:70
void matrix_subtract(const double *A, const double *B, size_t m, size_t n, double *C)
Definition Matrix.hpp:55
void matrix_multiply(const double *A, size_t m, size_t n, const double *B, size_t p, double *C)
Matrix operations for sensor calibration and filtering.
Definition Matrix.hpp:22
double vector_dot(const double *a, const double *b, size_t n)
Definition Matrix.hpp:101
void matrix_transpose(const double *A, size_t m, size_t n, double *B)
Definition Matrix.hpp:37
void matrix_copy(const double *A, size_t m, size_t n, double *B)
Definition Matrix.hpp:83
double matrix_trace(const double *A, size_t n)
Definition Matrix.hpp:115
double vector_norm(const double *a, size_t n)
Definition Matrix.hpp:110
void matrix_vector_multiply(const double *A, size_t m, size_t n, const double *x, double *y)
Definition Matrix.hpp:89
void matrix_add(const double *A, const double *B, size_t m, size_t n, double *C)
Definition Matrix.hpp:47
Central message header for all communication messages.
*brief user defined string literal for JSON values *sa std::size_t n
Definition json.hpp:24474