src/solvers/svd.c file

Functions

static auto vm_mat_at(const vm_mat* m, const int r, const int c) -> vm_float_t*
Column-major element pointer data[r + c * rows].
static auto vm_mat_max_abs(const vm_mat* m) -> vm_float_t
Largest absolute entry of m.
static auto vm_factor_tol(const vm_float_t scale, const int n) -> vm_float_t
Scale-aware cutoff n * eps * scale (at least eps).
static auto vm_min_int(const int a, const int b) -> int
Integer minimum.
static auto vm_mat_resize(vm_mat* m, const int rows, const int cols) -> bool
Ensure m is rows x cols, zeroed.
static auto vm_mat_transpose_copy(vm_mat* out, const vm_mat* in) -> bool
Copy in^T into out, resizing as needed.
static auto vm_col_dot(const vm_mat* A, const int p, const int q) -> vm_float_t
Dot product of columns p and q.
static void vm_swap_columns(const vm_mat* A, const int p, const int q)
Swap columns p and q in place.
static void vm_sort_singular(const vm_mat* U, vm_float_t* s, const vm_mat* V, const int k)
Sort singular values descending and permute U/V columns to match.
static auto vm_svd_jacobi_tall(const vm_mat* B, vm_mat* V) -> bool
One-sided Jacobi SVD of a tall-or-square copy B (m x n, m >= n).
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.

Function documentation

static vm_float_t* vm_mat_at(const vm_mat* m, const int r, const int c)

Column-major element pointer data[r + c * rows].

Parameters
m Matrix.
r Row index.
c Column index.
Returns Pointer to element (r, c).

static vm_float_t vm_mat_max_abs(const vm_mat* m)

Largest absolute entry of m.

Parameters
m Matrix.
Returns Max |m_ij|, or 0 if empty.

static vm_float_t vm_factor_tol(const vm_float_t scale, const int n)

Scale-aware cutoff n * eps * scale (at least eps).

Parameters
scale Typical magnitude of the matrix.
n Matrix order (clamped to at least 1).
Returns Factorization / rank tolerance.

static int vm_min_int(const int a, const int b)

Integer minimum.

Parameters
a First value.
b Second value.
Returns The smaller of a and b.

static bool vm_mat_resize(vm_mat* m, const int rows, const int cols)

Ensure m is rows x cols, zeroed.

Parameters
m Matrix to resize.
rows Desired rows.
cols Desired columns.
Returns True if m->data is valid.

Reuses the buffer when the size already matches.

static bool vm_mat_transpose_copy(vm_mat* out, const vm_mat* in)

Copy in^T into out, resizing as needed.

Parameters
out Destination transpose.
in Source matrix.
Returns True on success.

static vm_float_t vm_col_dot(const vm_mat* A, const int p, const int q)

Dot product of columns p and q.

Parameters
A Matrix.
p First column.
q Second column.
Returns Column inner product.

static void vm_swap_columns(const vm_mat* A, const int p, const int q)

Swap columns p and q in place.

Parameters
A Matrix (storage is mutated).
p First column.
q Second column.

static void vm_sort_singular(const vm_mat* U, vm_float_t* s, const vm_mat* V, const int k)

Sort singular values descending and permute U/V columns to match.

Parameters
U Left singular vectors.
s Singular values, length k.
V Right singular vectors.
k Number of values / columns.

static bool vm_svd_jacobi_tall(const vm_mat* B, vm_mat* V)

One-sided Jacobi SVD of a tall-or-square copy B (m x n, m >= n).

Parameters
B Tall working copy, overwritten.
V Right factor, resized to n x n.
Returns True on success.

On exit columns of B are u_j * s_j and V is n x n (then trimmed to n x k).

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.