include/vecmat/spectral.h file

Dense spectral primitives.

Matrix-vector products (vm_matvec, vm_gemv, vm_symv), rank-1 update (vm_ger), Jacobi eigensolve (vm_syev, vm_syev_topk), unnormalized graph Laplacian, and nearest-neighbor spacing (vm_nn_spacing_mean, vm_nn_spacing_hist, vm_pair_correlation).

Functions

void vm_matvec(vm_float_t* y, const vm_mat* A, const vm_float_t* x)
Dense matrix-vector product y = A x.
void vm_gemv(vm_float_t* y, vm_float_t alpha, const vm_mat* A, const vm_float_t* x, vm_float_t beta, bool trans)
y = alpha * op(A) * x + beta * y.
void vm_ger(vm_mat* A, vm_float_t alpha, const vm_float_t* x, const vm_float_t* y)
Rank-1 update A += alpha * x * yᵀ.
void vm_symv(vm_float_t* y, const vm_mat* A, const vm_float_t* x)
Symmetric matrix-vector product.
auto vm_syev(vm_mat* A, vm_float_t* eigenvalues, vm_mat* V) -> bool
Symmetric real eigenvalue decomposition (Jacobi).
auto vm_laplacian(vm_mat* L, const vm_mat* A) -> bool
Unnormalized graph Laplacian L = D − A.
auto vm_syev_topk(const vm_mat* A, int k, vm_float_t* eigenvalues, vm_mat* eigenvectors) -> bool
Top-k eigenvectors of a symmetric matrix.
auto vm_nn_spacing_mean(const vm_float_t* eigenvalues, int n) -> vm_float_t
Mean nearest-neighbor spacing of an ascending spectrum.
auto vm_nn_spacing_hist(const vm_float_t* eigenvalues, int n, vm_float_t s_min, vm_float_t s_max, vm_float_t* hist, int bins) -> bool
Raw histogram of nearest-neighbor spacings divided by their mean.
auto vm_pair_correlation(const vm_float_t* eigenvalues, int n, vm_float_t s) -> vm_float_t
Coarse pair correlation at unfolded distance s.

Function documentation

void vm_matvec(vm_float_t* y, const vm_mat* A, const vm_float_t* x)

Dense matrix-vector product y = A x.

Parameters
y Output vector.
A Input matrix.
x

A is rows × cols. x has length cols, y has length rows.

void vm_gemv(vm_float_t* y, vm_float_t alpha, const vm_mat* A, const vm_float_t* x, vm_float_t beta, bool trans)

y = alpha * op(A) * x + beta * y.

Parameters
y Output. Length is rows, or cols when trans is true.
alpha Scale on op(A) x.
A Matrix. Not modified.
x
beta Scale on the incoming y. Zero skips the read.
trans True to apply Aᵀ.

A is column-major. trans false uses A (x length cols, y length rows). trans true uses Aᵀ (x length rows, y length cols). A null pointer, or a matrix with a non-positive dimension, does not read or write. beta == 0 does not read y, so a NaN already in y is replaced. alpha == 0 does not read x. y may exactly alias x when the buffer is long enough for both lengths; a partial overlap is caller error. The exact-alias case allocates one temporary and frees it. Allocation failure leaves y unchanged. vm_matvec stays the no-transpose, alpha = 1, beta = 0 path.

void vm_ger(vm_mat* A, vm_float_t alpha, const vm_float_t* x, const vm_float_t* y)

Rank-1 update A += alpha * x * yᵀ.

Parameters
A Matrix updated in place.
alpha Scale on the outer product.
x
y Right vector, length cols.

x has length A->rows and y has length A->cols. x may exactly alias y. A must not overlap either vector. No allocation. alpha == 0 or an empty matrix does not read x or y and does not write A.

void vm_symv(vm_float_t* y, const vm_mat* A, const vm_float_t* x)

Symmetric matrix-vector product.

Parameters
y Output vector.
A Symmetric matrix (lower triangle used).
x

Only the lower triangle of A is read. A must be square.

bool vm_syev(vm_mat* A, vm_float_t* eigenvalues, vm_mat* V)

Symmetric real eigenvalue decomposition (Jacobi).

Parameters
A Symmetric matrix, overwritten on success.
eigenvalues Output eigenvalues (length n), descending.
V Optional n × n eigenvector matrix (columns).
Returns true on success.

On success, eigenvalues are written in descending order. Eigenvector columns are written to V if non-NULL (V must be n × n). A is overwritten. Returns false on non-square, bad sizes, or numerical failure.

bool vm_laplacian(vm_mat* L, const vm_mat* A)

Unnormalized graph Laplacian L = D − A.

Parameters
L Output Laplacian.
A Symmetric adjacency (non-negative entries expected).
Returns true on success.

From a dense symmetric adjacency matrix A. L is allocated or resized as needed.

bool vm_syev_topk(const vm_mat* A, int k, vm_float_t* eigenvalues, vm_mat* eigenvectors)

Top-k eigenvectors of a symmetric matrix.

Parameters
A Symmetric matrix (not overwritten).
k Number of leading eigenpairs (1 ≤ k ≤ n).
eigenvalues Output eigenvalues (length k).
eigenvectors Output n × k eigenvectors (columns).
Returns true on success.

Thin wrapper around vm_syev. Eigenvalues descending. eigenvectors is n × k on success.

vm_float_t vm_nn_spacing_mean(const vm_float_t* eigenvalues, int n)

Mean nearest-neighbor spacing of an ascending spectrum.

Parameters
eigenvalues Ascending eigenvalues, length n.
n
Returns Mean spacing, or NaN on bad input.

Returns the mean of eigenvalues[i + 1] - eigenvalues[i]. The buffer is not sorted and nothing is allocated. A null pointer, n < 2, a non-finite value, a descent, a non-finite sum, or zero mean returns NaN.

bool vm_nn_spacing_hist(const vm_float_t* eigenvalues, int n, vm_float_t s_min, vm_float_t s_max, vm_float_t* hist, int bins)

Raw histogram of nearest-neighbor spacings divided by their mean.

Parameters
eigenvalues Ascending eigenvalues, length n.
n
s_min Inclusive left edge of the first bin. Must be less than s_max.
s_max Inclusive right edge of the last bin.
hist Output counts, length bins. Overwritten on success.
bins Number of bins. Must be positive.
Returns true when hist holds counts.

Each gap is divided by vm_nn_spacing_mean. Bin i covers [s_min + i w, s_min + (i + 1) w) with w = (s_max - s_min) / bins. The last bin also includes s_max. Counts are raw, not a density. A spacing outside [s_min, s_max] is skipped. The caller owns hist. Nothing is allocated and the spectrum is not sorted.

A null pointer, n < 2, a non-finite eigenvalue, a descent, a mean that is not finite and positive, bins <= 0, or s_min >= s_max returns false and does not write hist.

vm_float_t vm_pair_correlation(const vm_float_t* eigenvalues, int n, vm_float_t s)

Coarse pair correlation at unfolded distance s.

Parameters
eigenvalues Ascending eigenvalues, length n.
n
s Center of the unfolded window. Negative is rejected.
Returns count / (n * 0.5), or NaN on bad input.

Positions are unfolded by the global mean spacing, ξ_i = λ_i / mean. Counts unordered pairs whose unfolded distance lies in [s - 0.25, s + 0.25) and returns count / (n * 0.5). The window is fixed. A Poisson spectrum is near 1. This is not a literature R_2 estimator. Local unfolding is not done. Nothing is allocated and the spectrum is not sorted.

A null pointer, n < 2, a non-finite eigenvalue, a descent, a mean that is not finite and positive, or s < 0 returns NaN. A non-finite s returns NaN.