File syn_matrix.h¶
FileList > src > syntropic > util > syn_matrix.h
Go to the source code of this file
Fixed-point Q16.16 matrix operations — zero heap allocation. More...
#include "../common/syn_defs.h"#include "syn_qmath.h"#include <stdbool.h>#include <stdint.h>
Classes¶
| Type | Name |
|---|---|
| struct | SYN_Matrix Fixed-point matrix descriptor (non-owning view). |
Public Functions¶
| Type | Name |
|---|---|
| void | syn_matrix_add (const SYN_Matrix * a, const SYN_Matrix * b, SYN_Matrix * out) Element-wise addition: out = A + B. |
| void | syn_matrix_copy (SYN_Matrix * dst, const SYN_Matrix * src) Copy matrix contents. |
| q16_t | syn_matrix_det (const SYN_Matrix * m) Determinant of a square matrix. |
| SYN_Status | syn_matrix_eigen_sym2 (const SYN_Matrix * A, q16_t evals, SYN_Matrix * E) Compute eigenvalues and eigenvectors of a symmetric 2×2 matrix A. |
| SYN_Status | syn_matrix_eigen_sym3 (const SYN_Matrix * A, q16_t evals, SYN_Matrix * E) Compute eigenvalues and eigenvectors of a symmetric 3×3 matrix A via Jacobi rotations. |
| SYN_Status | syn_matrix_get_block (const SYN_Matrix * src, uint8_t r0, uint8_t c0, SYN_Matrix * dst) Extract a submatrix block from src into dst. |
| void | syn_matrix_identity (SYN_Matrix * m) Set matrix to identity. |
| SYN_Status | syn_matrix_inv (const SYN_Matrix * m, SYN_Matrix * out) Matrix inverse: out = M⁻¹. |
| SYN_Status | syn_matrix_inv_3x3 (const SYN_Matrix * src, SYN_Matrix * dst) Invert a 3×3 matrix in-place or out-of-place via Cramer's rule. |
| SYN_Status | syn_matrix_inv_lu (const SYN_Matrix * src, SYN_Matrix * dst) Invert an arbitrary N×N matrix via LU decomposition (convenience wrapper). |
| SYN_Status | syn_matrix_inv_lu_work (const SYN_Matrix * src, SYN_Matrix * dst, q16_t * lu_work, uint8_t * p_work, q16_t * col_work) Invert an arbitrary N×N matrix via LU decomposition using caller-supplied workspace. |
| SYN_Status | syn_matrix_least_squares (const SYN_Matrix * A, const SYN_Matrix * b, SYN_Matrix * x) Solve overdetermined system A · x ≈ b via Normal Equations (Least Squares). |
| SYN_Status | syn_matrix_least_squares_work (const SYN_Matrix * A, const SYN_Matrix * b, SYN_Matrix * x, q16_t * ata_work, q16_t * atb_work, q16_t * at_work, q16_t * solver_lu, q16_t * solver_y) Solve overdetermined system A · x ≈ b via Least Squares using caller-supplied workspace. |
| void | syn_matrix_mul (const SYN_Matrix * a, const SYN_Matrix * b, SYN_Matrix * out) Matrix multiply: out = A × B. |
| void | syn_matrix_mul_vec (const SYN_Matrix * m, const q16_t * v_in, q16_t * v_out, uint8_t n_in) Matrix–vector multiply: out = M × v. |
| SYN_Status | syn_matrix_outer_product (const q16_t * u, uint8_t rows, const q16_t * v, uint8_t cols, SYN_Matrix * out) Vector outer product: out = u · vᵀ (rows(u) × cols(v)). |
| SYN_Status | syn_matrix_qr (const SYN_Matrix * A, SYN_Matrix * Q, SYN_Matrix * R) QR Decomposition: A = Q · R via Modified Gram-Schmidt process. |
| void | syn_matrix_rotate_2d (SYN_Matrix * out, q16_t angle) Set 3×3 matrix to a 2D rotation. |
| void | syn_matrix_rotate_x (SYN_Matrix * out, q16_t angle) Set 4×4 matrix to a rotation around the X axis. |
| void | syn_matrix_rotate_y (SYN_Matrix * out, q16_t angle) Set 4×4 matrix to a rotation around the Y axis. |
| void | syn_matrix_rotate_z (SYN_Matrix * out, q16_t angle) Set 4×4 matrix to a rotation around the Z axis. |
| void | syn_matrix_scale (const SYN_Matrix * a, q16_t scalar, SYN_Matrix * out) Scalar multiply: out = A × scalar. |
| void | syn_matrix_scale_2d (SYN_Matrix * out, q16_t sx, q16_t sy) Set 3×3 matrix to a 2D scale. |
| SYN_Status | syn_matrix_set_block (SYN_Matrix * dst, uint8_t r0, uint8_t c0, const SYN_Matrix * src) Embed a submatrix block src into dst at position (r0, c0). |
| SYN_Status | syn_matrix_solve_cholesky (const SYN_Matrix * A, const SYN_Matrix * b, SYN_Matrix * x) Solve symmetric positive-definite system A · x = b via Cholesky (convenience wrapper). |
| SYN_Status | syn_matrix_solve_cholesky_work (const SYN_Matrix * A, const SYN_Matrix * b, SYN_Matrix * x, q16_t * L_work, q16_t * y_work) Solve system A · x = b via Cholesky decomposition using caller-supplied workspace buffer. |
| SYN_Status | syn_matrix_solve_lu (const SYN_Matrix * A, const SYN_Matrix * b, SYN_Matrix * x) Solve system A · x = b via LU decomposition (convenience wrapper). |
| SYN_Status | syn_matrix_solve_lu_work (const SYN_Matrix * A, const SYN_Matrix * b, SYN_Matrix * x, q16_t * lu_work, uint8_t * p_work, q16_t * y_work) Solve system A · x = b via LU decomposition using caller-supplied workspace buffer. |
| void | syn_matrix_sub (const SYN_Matrix * a, const SYN_Matrix * b, SYN_Matrix * out) Element-wise subtraction: out = A − B. |
| q16_t | syn_matrix_trace (const SYN_Matrix * m) Trace (sum of diagonal elements). |
| void | syn_matrix_translate_2d (SYN_Matrix * out, q16_t tx, q16_t ty) Set 3×3 matrix to a 2D translation. |
| void | syn_matrix_translate_3d (SYN_Matrix * out, q16_t tx, q16_t ty, q16_t tz) Set 4×4 matrix to a 3D translation. |
| void | syn_matrix_transpose (const SYN_Matrix * a, SYN_Matrix * out) Transpose: out = Aᵀ. |
| void | syn_matrix_zero (SYN_Matrix * m) Set all elements to zero. |
| void | syn_vec3_cross (const q16_t * a, const q16_t * b, q16_t * out) Cross product of two 3-element Q16 vectors. |
| q16_t | syn_vec_dot (const q16_t * a, const q16_t * b, uint8_t n) Dot product of two n-element Q16 vectors. |
| q16_t | syn_vec_norm (const q16_t * v, uint8_t n) Magnitude (Euclidean norm) of an n-element Q16 vector. |
| SYN_Status | syn_vec_normalize (const q16_t * v, q16_t * out, uint8_t n) Normalize a vector to unit length. |
Macros¶
| Type | Name |
|---|---|
| define | SYN_MAT2_DECL (name) [**SYN\_MAT\_DECL**](syn__matrix_8h.md#define-syn_mat_decl)(name, 2, 2)Shorthand for 2×2. |
| define | SYN_MAT3_DECL (name) [**SYN\_MAT\_DECL**](syn__matrix_8h.md#define-syn_mat_decl)(name, 3, 3)Shorthand for 3×3. |
| define | SYN_MAT4_DECL (name) [**SYN\_MAT\_DECL**](syn__matrix_8h.md#define-syn_mat_decl)(name, 4, 4)Shorthand for 4×4. |
| define | SYN_MAT_AT (m, r, c) ((m)->data[(r) \* (m)->cols + (c)])Element access (row, col) — evaluates to an lvalue. |
| define | SYN_MAT_DECL (name, R, C) /* multi line expression */Declare a matrix with automatic (stack) storage. |
| define | SYN_MAT_INIT (name, storage, R, C) [**SYN\_Matrix**](structSYN__Matrix.md) name = {(storage), (R), (C)}Initialize a matrix view over existing storage. |
| define | SYN_MAT_STATIC (name, R, C) /* multi line expression */Declare a matrix with static storage (zero-initialized). |
| define | SYN_SOLVER_MAX_N 8Maximum matrix dimension N for general linear solvers (LU, Cholesky, LS) |
Detailed Description¶
Supports compile-time-arbitrary matrix dimensions. Callers supply all storage via stack or static arrays. The compiler constant-folds dimension parameters and unrolls loops when sizes are compile-time constants.
** **
// Declare three 3×3 matrices on the stack
SYN_MAT_DECL(A, 3, 3);
SYN_MAT_DECL(B, 3, 3);
SYN_MAT_DECL(C, 3, 3);
syn_matrix_identity(&A);
syn_matrix_rotate_2d(&B, Q16_PI_2); // 90° rotation
syn_matrix_mul(&A, &B, &C); // C = A × B
// Non-square multiply: 2×3 × 3×4 = 2×4
SYN_MAT_DECL(M, 2, 3);
SYN_MAT_DECL(N, 3, 4);
SYN_MAT_DECL(P, 2, 4);
syn_matrix_mul(&M, &N, &P);
// Element access
SYN_MAT_AT(&A, 1, 2) = Q16_FROM_INT(42);
Public Functions Documentation¶
function syn_matrix_add¶
Element-wise addition: out = A + B.
Parameters:
aFirst operand.bSecond operand (same dimensions as a).outResult (same dimensions as a).
function syn_matrix_copy¶
Copy matrix contents.
Parameters:
dstDestination (must match src dimensions).srcSource matrix.
function syn_matrix_det¶
Determinant of a square matrix.
Supports 2×2, 3×3, and 4×4. Uses cofactor expansion for 2×2/3×3 and LU decomposition for 4×4.
Parameters:
mSquare matrix.
Returns:
Determinant in Q16.
function syn_matrix_eigen_sym2¶
Compute eigenvalues and eigenvectors of a symmetric 2×2 matrix A.
Parameters:
ASymmetric 2×2 matrix.evalsOutput array of 2 eigenvalues (sorted descending).EOutput 2×2 matrix whose columns are the normalized eigenvectors.
Returns:
SYN_OK on success, SYN_INVALID_PARAM on invalid inputs.
function syn_matrix_eigen_sym3¶
Compute eigenvalues and eigenvectors of a symmetric 3×3 matrix A via Jacobi rotations.
Parameters:
ASymmetric 3×3 matrix.evalsOutput array of 3 eigenvalues (sorted descending).EOutput 3×3 matrix whose columns are the normalized eigenvectors.
Returns:
SYN_OK on success, SYN_INVALID_PARAM on invalid inputs.
function syn_matrix_get_block¶
Extract a submatrix block from src into dst.
SYN_Status syn_matrix_get_block (
const SYN_Matrix * src,
uint8_t r0,
uint8_t c0,
SYN_Matrix * dst
)
Parameters:
srcSource matrix (M×N).r0Starting row index in src.c0Starting column index in src.dstDestination matrix (R×C). Must fit within src bounds from (r0, c0).
Returns:
SYN_OK on success, SYN_INVALID_PARAM on out-of-bounds.
function syn_matrix_identity¶
Set matrix to identity.
Parameters:
mSquare matrix.
function syn_matrix_inv¶
Matrix inverse: out = M⁻¹.
Supports 2×2, 3×3, and 4×4 square matrices. Returns SYN_ERROR if the matrix is singular (determinant ≈ 0).
Parameters:
mInput square matrix.outOutput inverse (same dimensions). Must not alias m.
Returns:
SYN_OK on success, SYN_ERROR if singular.
function syn_matrix_inv_3x3¶
Invert a 3×3 matrix in-place or out-of-place via Cramer's rule.
Parameters:
srcInput 3×3 matrix.dstOutput 3×3 inverse matrix. May alias src.
Returns:
SYN_OK on success, SYN_ERROR if matrix is singular (det == 0).
function syn_matrix_inv_lu¶
Invert an arbitrary N×N matrix via LU decomposition (convenience wrapper).
Parameters:
srcInput square matrix (N×N).dstOutput inverse matrix (N×N).
Returns:
SYN_OK on success, SYN_ERROR if src is singular or dimensions invalid.
function syn_matrix_inv_lu_work¶
Invert an arbitrary N×N matrix via LU decomposition using caller-supplied workspace.
SYN_Status syn_matrix_inv_lu_work (
const SYN_Matrix * src,
SYN_Matrix * dst,
q16_t * lu_work,
uint8_t * p_work,
q16_t * col_work
)
Parameters:
srcInput square matrix (N×N).dstOutput inverse matrix (N×N). Must not alias src.lu_workCaller-supplied N×N buffer (N*N elements).p_workCaller-supplied N pivot buffer (N elements).col_workCaller-supplied N vector buffer (N elements).
Returns:
SYN_OK on success, SYN_ERROR if src is singular or dimensions invalid.
function syn_matrix_least_squares¶
Solve overdetermined system A · x ≈ b via Normal Equations (Least Squares).
Parameters:
AOverdetermined system matrix (M×N, M ≥ N).bRight-hand side measurement vector (M×1).xOutput solution parameter vector (N×1).
Returns:
SYN_OK on success, SYN_ERROR if AᵀA is singular.
function syn_matrix_least_squares_work¶
Solve overdetermined system A · x ≈ b via Least Squares using caller-supplied workspace.
SYN_Status syn_matrix_least_squares_work (
const SYN_Matrix * A,
const SYN_Matrix * b,
SYN_Matrix * x,
q16_t * ata_work,
q16_t * atb_work,
q16_t * at_work,
q16_t * solver_lu,
q16_t * solver_y
)
Parameters:
AOverdetermined system matrix (M×N, M ≥ N).bRight-hand side measurement vector (M×1).xOutput solution parameter vector (N×1).ata_workCaller-supplied N×N matrix buffer (N*N elements).atb_workCaller-supplied N vector buffer (N elements).at_workCaller-supplied N×M transpose matrix buffer (N*M elements).solver_luCaller-supplied N×N solver matrix buffer (N*N elements).solver_yCaller-supplied N solver vector buffer (N elements).
Returns:
SYN_OK on success, SYN_ERROR if AᵀA is singular.
function syn_matrix_mul¶
Matrix multiply: out = A × B.
Requires: A.cols == B.rows, out is A.rows × B.cols. Uses int64_t accumulator for full Q16 precision.
Parameters:
aLeft operand (R₁ × C₁).bRight operand (C₁ × C₂).outResult (R₁ × C₂). Must not alias a or b.
function syn_matrix_mul_vec¶
Matrix–vector multiply: out = M × v.
Parameters:
mSquare or rectangular matrix (R × C).v_inInput vector (C elements).v_outOutput vector (R elements). Must not alias v_in.n_inLength of input vector (must equal m->cols).
function syn_matrix_outer_product¶
Vector outer product: out = u · vᵀ (rows(u) × cols(v)).
SYN_Status syn_matrix_outer_product (
const q16_t * u,
uint8_t rows,
const q16_t * v,
uint8_t cols,
SYN_Matrix * out
)
Parameters:
uColumn vector elements (R elements).rowsNumber of rows (R).vRow vector elements (C elements).colsNumber of columns (C).outOutput matrix (R×C).
Returns:
SYN_OK on success, SYN_INVALID_PARAM if NULL or invalid dimensions.
function syn_matrix_qr¶
QR Decomposition: A = Q · R via Modified Gram-Schmidt process.
Decomposes an M×N matrix A (M ≥ N) into orthogonal M×N matrix Q (QᵀQ = I) and upper-triangular N×N matrix R.
Parameters:
AInput matrix (M×N, M ≥ N).QOutput orthogonal matrix (M×N).ROutput upper-triangular matrix (N×N).
Returns:
SYN_OK on success, SYN_ERROR if A is linearly dependent / singular.
function syn_matrix_rotate_2d¶
Set 3×3 matrix to a 2D rotation.
Parameters:
outOutput 3×3 matrix.angleRotation angle in Q16 radians (counter-clockwise).
function syn_matrix_rotate_x¶
Set 4×4 matrix to a rotation around the X axis.
Parameters:
outOutput 4×4 matrix.angleRotation angle in Q16 radians.
function syn_matrix_rotate_y¶
Set 4×4 matrix to a rotation around the Y axis.
Parameters:
outOutput 4×4 matrix.angleRotation angle in Q16 radians.
function syn_matrix_rotate_z¶
Set 4×4 matrix to a rotation around the Z axis.
Parameters:
outOutput 4×4 matrix.angleRotation angle in Q16 radians.
function syn_matrix_scale¶
Scalar multiply: out = A × scalar.
Parameters:
aInput matrix.scalarQ16 scalar multiplier.outResult (same dimensions as a).
function syn_matrix_scale_2d¶
Set 3×3 matrix to a 2D scale.
Parameters:
outOutput 3×3 matrix.sxX scale factor in Q16.syY scale factor in Q16.
function syn_matrix_set_block¶
Embed a submatrix block src into dst at position (r0, c0).
SYN_Status syn_matrix_set_block (
SYN_Matrix * dst,
uint8_t r0,
uint8_t c0,
const SYN_Matrix * src
)
Parameters:
dstDestination matrix (M×N).r0Starting row index in dst.c0Starting column index in dst.srcSource submatrix block (R×C).
Returns:
SYN_OK on success, SYN_INVALID_PARAM on out-of-bounds.
function syn_matrix_solve_cholesky¶
Solve symmetric positive-definite system A · x = b via Cholesky (convenience wrapper).
Parameters:
ASymmetric positive-definite system matrix (N×N).bRight-hand side vector (N×1).xOutput solution vector (N×1). May alias b.
Returns:
SYN_OK on success, SYN_ERROR if A is not positive-definite.
function syn_matrix_solve_cholesky_work¶
Solve system A · x = b via Cholesky decomposition using caller-supplied workspace buffer.
SYN_Status syn_matrix_solve_cholesky_work (
const SYN_Matrix * A,
const SYN_Matrix * b,
SYN_Matrix * x,
q16_t * L_work,
q16_t * y_work
)
Parameters:
ASymmetric positive-definite system matrix (N×N).bRight-hand side vector (N×1).xOutput solution vector (N×1).L_workCaller-supplied N×N lower triangular buffer (N*N elements).y_workCaller-supplied N vector buffer (N elements).
Returns:
SYN_OK on success, SYN_ERROR if A is not positive-definite.
function syn_matrix_solve_lu¶
Solve system A · x = b via LU decomposition (convenience wrapper).
Parameters:
ASquare system matrix (N×N).bRight-hand side vector (N×1).xOutput solution vector (N×1). May alias b.
Returns:
SYN_OK on success, SYN_ERROR if A is singular or dimensions invalid.
function syn_matrix_solve_lu_work¶
Solve system A · x = b via LU decomposition using caller-supplied workspace buffer.
SYN_Status syn_matrix_solve_lu_work (
const SYN_Matrix * A,
const SYN_Matrix * b,
SYN_Matrix * x,
q16_t * lu_work,
uint8_t * p_work,
q16_t * y_work
)
Zero stack allocation inside solver.
Parameters:
ASquare system matrix (N×N).bRight-hand side vector (N×1).xOutput solution vector (N×1).lu_workCaller-supplied N×N buffer (N*N elements).p_workCaller-supplied N pivot buffer (N elements).y_workCaller-supplied N vector buffer (N elements).
Returns:
SYN_OK on success, SYN_ERROR if A is singular or dimensions invalid.
function syn_matrix_sub¶
Element-wise subtraction: out = A − B.
Parameters:
aMinuend.bSubtrahend (same dimensions as a).outResult (same dimensions as a).
function syn_matrix_trace¶
Trace (sum of diagonal elements).
Parameters:
mSquare matrix.
Returns:
Trace in Q16.
function syn_matrix_translate_2d¶
Set 3×3 matrix to a 2D translation.
Parameters:
outOutput 3×3 matrix.txX translation in Q16.tyY translation in Q16.
function syn_matrix_translate_3d¶
Set 4×4 matrix to a 3D translation.
Parameters:
outOutput 4×4 matrix.txX translation in Q16.tyY translation in Q16.tzZ translation in Q16.
function syn_matrix_transpose¶
Transpose: out = Aᵀ.
Parameters:
aInput (R × C).outOutput (C × R). Must not alias a.
function syn_matrix_zero¶
Set all elements to zero.
Parameters:
mMatrix to clear.
function syn_vec3_cross¶
Cross product of two 3-element Q16 vectors.
Parameters:
aFirst vector (3 elements).bSecond vector (3 elements).outOutput vector (3 elements). Must not alias a or b.
function syn_vec_dot¶
Dot product of two n-element Q16 vectors.
Parameters:
aFirst vector.bSecond vector.nNumber of elements.
Returns:
Dot product in Q16.
function syn_vec_norm¶
Magnitude (Euclidean norm) of an n-element Q16 vector.
Parameters:
vVector.nNumber of elements.
Returns:
|v| in Q16.
function syn_vec_normalize¶
Normalize a vector to unit length.
Parameters:
vInput vector (n elements).outOutput unit vector (n elements). May alias v.nNumber of elements.
Returns:
SYN_OK on success, SYN_ERROR if zero-length vector.
Macro Definition Documentation¶
define SYN_MAT2_DECL¶
Shorthand for 2×2.
define SYN_MAT3_DECL¶
Shorthand for 3×3.
define SYN_MAT4_DECL¶
Shorthand for 4×4.
define SYN_MAT_AT¶
Element access (row, col) — evaluates to an lvalue.
define SYN_MAT_DECL¶
Declare a matrix with automatic (stack) storage.
#define SYN_MAT_DECL (
name,
R,
C
) `q16_t name##_store[(R) * (C)]; \ SYN_Matrix name = {name##_store, (R), (C)}`
define SYN_MAT_INIT¶
Initialize a matrix view over existing storage.
define SYN_MAT_STATIC¶
Declare a matrix with static storage (zero-initialized).
define SYN_SOLVER_MAX_N¶
Maximum matrix dimension N for general linear solvers (LU, Cholesky, LS)
The documentation for this class was generated from the following file src/syntropic/util/syn_matrix.h