File syn_matrix.c¶
FileList > src > syntropic > util > syn_matrix.c
Go to the source code of this file
Fixed-point Q16.16 matrix operations implementation. More...
#include "../util/syn_assert.h"#include "syn_matrix.h"#include <string.h>
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_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. |
Public Static Functions¶
| Type | Name |
|---|---|
| q16_t | det_2x2 (const q16_t * d) 2×2 determinant: ad - bc. |
| q16_t | det_3x3 (const q16_t * d) 3×3 determinant via cofactor expansion along row 0. |
| q16_t | det_4x4 (const q16_t * d) 4×4 determinant via cofactor expansion along row 0. |
| SYN_Status | inv_1x1 (const SYN_Matrix * m, SYN_Matrix * out) 1×1 inverse via scalar division: 1/a. |
| SYN_Status | inv_2x2 (const SYN_Matrix * m, SYN_Matrix * out) 2×2 inverse via adjugate/determinant. |
| SYN_Status | inv_3x3 (const SYN_Matrix * m, SYN_Matrix * out) 3×3 inverse via adjugate/determinant. |
| SYN_Status | inv_4x4 (const SYN_Matrix * m, SYN_Matrix * out) 4×4 inverse via Gauss-Jordan elimination with partial pivoting. |
Detailed Description¶
All operations use caller-owned storage. No heap allocation. int64_t accumulators preserve full Q16 precision in multiply chains.
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_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.
Public Static Functions Documentation¶
function det_2x2¶
2×2 determinant: ad - bc.
Parameters:
dFlat 4-element array [a, b, c, d] (row-major).
Returns:
Determinant in Q16.
function det_3x3¶
3×3 determinant via cofactor expansion along row 0.
Parameters:
dFlat 9-element array (row-major).
Returns:
Determinant in Q16.
function det_4x4¶
4×4 determinant via cofactor expansion along row 0.
Parameters:
dFlat 16-element array (row-major).
Returns:
Determinant in Q16.
function inv_1x1¶
1×1 inverse via scalar division: 1/a.
Parameters:
mInput 1×1 matrix.outOutput 1×1 inverse.
Returns:
SYN_OK or SYN_ERROR if zero.
function inv_2x2¶
2×2 inverse via adjugate/determinant.
Parameters:
mInput 2×2 matrix.outOutput 2×2 inverse.
Returns:
SYN_OK or SYN_ERROR if singular.
function inv_3x3¶
3×3 inverse via adjugate/determinant.
Parameters:
mInput 3×3 matrix.outOutput 3×3 inverse.
Returns:
SYN_OK or SYN_ERROR if singular.
function inv_4x4¶
4×4 inverse via Gauss-Jordan elimination with partial pivoting.
Parameters:
mInput 4×4 matrix.outOutput 4×4 inverse.
Returns:
SYN_OK or SYN_ERROR if singular.
The documentation for this class was generated from the following file src/syntropic/util/syn_matrix.c