spectral.h file
Dense spectral primitives.
Matrix-vector products (vm_, vm_, vm_), rank-1 update (vm_), Jacobi eigensolve (vm_, vm_), unnormalized graph Laplacian, and nearest-neighbor spacing (vm_, vm_, vm_).
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_ 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_. 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_. 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.