dense.h file
Heap vm_ plus LU / QR / SVD / Cholesky.
Rank-deficient work returns false, not a silent NaN.
Functions
-
auto vm_mat_alloc(int rows,
int cols) -> vm_
mat - Allocates a new matrix with the specified dimensions.
-
void vm_mat_free(vm_
mat* m) - Frees the memory allocated for a matrix and resets its state.
-
void vm_mat_zero(vm_
mat* m) - Sets all elements of the matrix to zero.
-
auto vm_mat_get(const vm_
mat* m, int r, int c) -> vm_ float_ t - Returns the value of the matrix element at the specified row and column.
-
void vm_mat_set(vm_
mat* m, int r, int c, vm_ float_ t v) - Sets the element at the specified row and column in the matrix to the given value.
-
auto vm_mat_copy(vm_
mat* dst, const vm_ mat* src) -> bool - Copies the contents of one matrix to another.
-
auto vm_mat_det(const vm_
mat* A) -> vm_ float_ t - Determinant of a square matrix via LU.
-
auto vm_mat_inverse(vm_
mat* out, const vm_ mat* A) -> bool - Inverse of a square matrix via LU.
-
auto vm_lu_factor(vm_
mat* A, int* pivot, int* sign) -> bool - In-place LU factorization with partial pivoting.
-
auto vm_lu_solve(const vm_
mat* LU, const int* pivot, const vm_ float_ t* b, vm_ float_ t* x) -> bool - Solves
A x = bfrom a factored LU. -
auto vm_qr_factor(vm_
mat* A, vm_ float_ t* tau) -> bool - In-place Householder QR.
-
auto vm_qr_unpack(vm_
mat* Q, vm_ mat* R, const vm_ mat* QR, const vm_ float_ t* tau) -> bool - Thin factors: Q is m x k, R is k x n, k = min(m, n).
-
auto vm_qr_solve(const vm_
mat* QR, const vm_ float_ t* tau, const vm_ float_ t* b, vm_ float_ t* x) -> bool - Least-squares solve
min ||A x - b||from a factored QR. -
auto vm_svd_factor(const vm_
mat* A, vm_ mat* U, vm_ float_ t* s, vm_ mat* V) -> bool - Thin SVD
A = U diag(s) V^T. -
auto vm_chol_factor(vm_
mat* A) -> bool - In-place dense Cholesky A = L Lᵀ (lower triangle overwritten).
-
auto vm_chol_solve(const vm_
mat* L, const vm_ float_ t* b, vm_ float_ t* x) -> bool - Solve L Lᵀ x = b after
vm_.chol_ factor
Function documentation
vm_ mat vm_mat_alloc(int rows,
int cols)
Allocates a new matrix with the specified dimensions.
| Parameters | |
|---|---|
| rows | number of rows in the matrix |
| cols | number of columns in the matrix |
| Returns | allocated matrix or zero matrix on failure |
Memory is allocated using calloc to initialize all elements to zero. If allocation fails or if dimensions are invalid, a matrix with zero dimensions and NULL data pointer is returned.
void vm_mat_free(vm_ mat* m)
Frees the memory allocated for a matrix and resets its state.
| Parameters | |
|---|---|
| m | pointer to the matrix to free |
If the matrix pointer is NULL, the function returns immediately. The data pointer is freed (if allocated), then set to NULL and both dimensions are reset to zero.
void vm_mat_zero(vm_ mat* m)
Sets all elements of the matrix to zero.
| Parameters | |
|---|---|
| m | pointer to the matrix to be zeroed |
If the matrix pointer or its data pointer is NULL, the function returns immediately without performing any operation.
vm_ float_ t vm_mat_get(const vm_ mat* m,
int r,
int c)
Returns the value of the matrix element at the specified row and column.
| Parameters | |
|---|---|
| m | pointer to the matrix |
| r | zero-based row index |
| c | zero-based column index |
| Returns | value at (r, c) or 0.0f on invalid input |
If the matrix pointer is NULL, the data pointer is NULL, or the row or column indices are out of bounds, the function returns 0.0f without accessing memory.
void vm_mat_set(vm_ mat* m,
int r,
int c,
vm_ float_ t v)
Sets the element at the specified row and column in the matrix to the given value.
| Parameters | |
|---|---|
| m | pointer to the matrix to modify |
| r | row index |
| c | column index |
| v | value to set at the specified position |
The function performs bounds checking and validates the matrix pointer and its data buffer before performing the assignment. If any validation fails, the function returns without modifying any data.
bool vm_mat_copy(vm_ mat* dst,
const vm_ mat* src)
Copies the contents of one matrix to another.
| Parameters | |
|---|---|
| dst | pointer to the destination matrix |
| src | pointer to the source matrix |
| Returns | true on success, false if either pointer is NULL, source data is NULL or memory allocation fails |
If the destination matrix has incompatible dimensions or unallocated data it is freed and reallocated to match the source dimensions. The source matrix must be valid and have allocated data.
vm_ float_ t vm_mat_det(const vm_ mat* A)
Determinant of a square matrix via LU.
| Parameters | |
|---|---|
| A | Square matrix (not modified). |
| Returns | det(A), or 0 on failure. |
bool vm_mat_inverse(vm_ mat* out,
const vm_ mat* A)
Inverse of a square matrix via LU.
| Parameters | |
|---|---|
| out | Inverse on success. |
| A | Square matrix (not modified). |
| Returns | True on success. |
Allocates or resizes out when it is not already n x n.
bool vm_lu_factor(vm_ mat* A,
int* pivot,
int* sign)
In-place LU factorization with partial pivoting.
| Parameters | |
|---|---|
| A | Square matrix, overwritten with L and U. |
| pivot | Row permutation; length A->rows. |
| sign | Optional; set to +1 or -1 for the permutation sign. |
| Returns | True on success. |
On success A holds L (unit diagonal, strictly below) and U (on and above the diagonal). pivot[i] is the original row now at position i.
bool vm_lu_solve(const vm_ mat* LU,
const int* pivot,
const vm_ float_ t* b,
vm_ float_ t* x)
Solves A x = b from a factored LU.
| Parameters | |
|---|---|
| LU | Factored matrix from vm_. |
| pivot | Row permutation from vm_. |
| b | Right-hand side, length n. |
| x | Solution, length n. |
| Returns | True on success. |
bool vm_qr_factor(vm_ mat* A,
vm_ float_ t* tau)
In-place Householder QR.
| Parameters | |
|---|---|
| A | Matrix overwritten with R and Householder vectors. |
| tau | Householder scales, length min(m, n). |
| Returns | True on success. |
A is m x n.
On success the upper triangle of A is R and the strict lower part stores Householder vectors. tau must hold min(m, n) scalars.
bool vm_qr_unpack(vm_ mat* Q,
vm_ mat* R,
const vm_ mat* QR,
const vm_ float_ t* tau)
Thin factors: Q is m x k, R is k x n, k = min(m, n).
| Parameters | |
|---|---|
| Q | Orthonormal factor on success. |
| R | Upper-triangular factor on success. |
| QR | Factored matrix from vm_. |
| tau | Householder scales from vm_. |
| Returns | True on success. |
Allocates or resizes Q and R when they do not already match.
bool vm_qr_solve(const vm_ mat* QR,
const vm_ float_ t* tau,
const vm_ float_ t* b,
vm_ float_ t* x)
Least-squares solve min ||A x - b|| from a factored QR.
| Parameters | |
|---|---|
| QR | Factored matrix from vm_. |
| tau | Householder scales from vm_. |
| b | Right-hand side, length m. |
| x | Solution, length n. |
| Returns | True on success. |
Requires m >= n. b has length m, x has length n.
bool vm_svd_factor(const vm_ mat* A,
vm_ mat* U,
vm_ float_ t* s,
vm_ mat* V)
Thin SVD A = U diag(s) V^T.
| Parameters | |
|---|---|
| A | Input matrix (not modified). |
| U | Left singular vectors on success. |
| s | Singular values, length min(m, n). |
| V | Right singular vectors on success. |
| Returns | True on success. |
s has length k = min(m, n) (descending). U is m x k, V is n x k (columns are singular vectors). Allocates or resizes U and V.
bool vm_chol_factor(vm_ mat* A)
In-place dense Cholesky A = L Lᵀ (lower triangle overwritten).
| Parameters | |
|---|---|
| A | Square dense matrix (destroyed / factored in place). |
| Returns | true on success, false if not SPD or invalid input. |
A must be square SPD. The strict upper triangle is left untouched.
bool vm_chol_solve(const vm_ mat* L,
const vm_ float_ t* b,
vm_ float_ t* x)
Solve L Lᵀ x = b after vm_.
| Parameters | |
|---|---|
| L | Factored lower triangle from vm_. |
| b | Right-hand side (length n). |
| x | Solution (length n). |
| Returns | true on success, false on error. |