src/features/lanczos.c file

Classes

struct lanczos_ws
Lanczos workspace.
struct lanczos_dense_ctx

Functions

static void info_set(vm_ksp_info* info, const int iters, const vm_float_t rel_res, const bool ok)
static auto lanczos_dot(const vm_float_t* a, const vm_float_t* b, const int n) -> vm_float_t
static auto lanczos_col(const lanczos_ws* ws, const int j) -> vm_float_t*
static auto tridiag_anorm(const vm_float_t* alpha, const vm_float_t* beta, const int basis) -> vm_float_t
static auto lanczos_break_tol(const vm_float_t anorm, const int n) -> vm_float_t
static auto vec_finite(const vm_float_t* x, const int n) -> bool
static void ws_free(lanczos_ws* ws)
static auto ws_alloc(lanczos_ws* ws, const int n, const int m) -> bool
static auto seed_column(lanczos_ws* ws, const int col, const int nprev) -> bool
Unit vector in column col, orthogonal to columns 0 .
static auto seed_any(lanczos_ws* ws, const int col, const int n_prev) -> bool
static auto extend_to(lanczos_ws* ws, void(*)(vm_float_t*y, const vm_float_t*x, void*ctx) matvec, void* ctx, const int target) -> bool
Extend the factorization until target steps or an invariant subspace.
static auto solve_tridiag(const vm_float_t* alpha, const vm_float_t* beta, const int basis, vm_float_t* ritz, vm_mat* Y) -> bool
static auto wanted_column(const vm_lanczos_which which, const int basis, const int k, const int i) -> int
static auto residual_scale(const vm_float_t lambda, const vm_float_t anorm) -> vm_float_t
static auto worst_estimate(const lanczos_ws* ws, const vm_mat* Y, const vm_float_t* ritz, const vm_lanczos_which which, const int k) -> vm_float_t
static void orient_positive(vm_float_t* u, const int n)
static auto export_pairs(const lanczos_ws* ws, const vm_mat* Y, const vm_float_t* ritz, const vm_lanczos_which which, const int k, vm_float_t* eigenvalues, vm_mat* eigenvectors, void(*)(vm_float_t*y, const vm_float_t*x, void*ctx) matvec, void* ctx, vm_float_t* rel_out) -> bool
Write the selected Ritz pairs and the worst true relative residual.
static void fill_tridiag(vm_mat* T, const vm_float_t* alpha, const vm_float_t* beta, const int n)
static auto qr_shift(vm_mat* T, const vm_float_t sigma, vm_mat* Q_total) -> bool
One implicit QR shift: T <- Qᵀ T Q, and Q_total <- Q_total Q.
static auto implicit_restart(lanczos_ws* ws, const vm_lanczos_which which, const int k, const vm_float_t* ritz) -> bool
Purge m - k unwanted Ritz values and keep a k-step factorization.
static auto give_up(const lanczos_ws* ws, const vm_mat* Y, const vm_float_t* ritz, const vm_lanczos_which which, const int k, vm_float_t* eigenvalues, vm_mat* eigenvectors, void(*)(vm_float_t*y, const vm_float_t*x, void*ctx) matvec, void* ctx, const vm_float_t tol, vm_float_t* rel, bool* ok) -> bool
auto vm_lanczos(int n, int k, void(*)(vm_float_t*y, const vm_float_t*x, void*ctx) matvec, void* ctx, vm_lanczos_which which, int m, vm_float_t tol, int max_restarts, vm_float_t* eigenvalues, vm_mat* eigenvectors, vm_ksp_info* info) -> bool
Implicitly restarted Lanczos for a real symmetric operator.
static void lanczos_dense_matvec(vm_float_t* y, const vm_float_t* x, void* ctx)
auto vm_lanczos_dense(const vm_mat* A, int k, vm_lanczos_which which, int m, vm_float_t tol, int max_restarts, vm_float_t* eigenvalues, vm_mat* eigenvectors, vm_ksp_info* info) -> bool
Lanczos on a dense symmetric matrix.
static void lanczos_sparse_matvec(vm_float_t* y, const vm_float_t* x, void* ctx)
auto vm_lanczos_sparse(const vm_spmat* A, int k, vm_lanczos_which which, int m, vm_float_t tol, int max_restarts, vm_float_t* eigenvalues, vm_mat* eigenvectors, vm_ksp_info* info) -> bool
Lanczos on a square CSR matrix.

Variables

static const vm_float_t lanczos_macheps

Function documentation

static bool seed_column(lanczos_ws* ws, const int col, const int nprev)

Unit vector in column col, orthogonal to columns 0 .

. nprev-1.

static bool extend_to(lanczos_ws* ws, void(*)(vm_float_t*y, const vm_float_t*x, void*ctx) matvec, void* ctx, const int target)

Extend the factorization until target steps or an invariant subspace.

Returns false on a non-finite matvec. A short return with basis < target means the Krylov space stopped early and beta[basis - 1] == 0.

static bool qr_shift(vm_mat* T, const vm_float_t sigma, vm_mat* Q_total)

One implicit QR shift: T <- Qᵀ T Q, and Q_total <- Q_total Q.

T stays symmetric tridiagonal. Fill outside that band is roundoff and is dropped, which is the bulge-chase update.

bool vm_lanczos(int n, int k, void(*)(vm_float_t*y, const vm_float_t*x, void*ctx) matvec, void* ctx, vm_lanczos_which which, int m, vm_float_t tol, int max_restarts, vm_float_t* eigenvalues, vm_mat* eigenvectors, vm_ksp_info* info)

Implicitly restarted Lanczos for a real symmetric operator.

Parameters
n
k Number of eigenpairs. 1 <= k <= m.
matvec Applies the symmetric operator. y and x have length n.
ctx Passed through to matvec. May be NULL.
which Largest or smallest end of the projected spectrum.
m Krylov length. k <= m <= n.
tol Relative residual tolerance. Must be finite and positive.
max_restarts Restart cap. Zero is a single build. Negative is an error.
eigenvalues Output eigenvalues, length k.
eigenvectors Optional n × k eigenvectors (columns). NULL skips them.
info Optional iteration stats. May be NULL.
Returns true when the worst returned residual is within tol.

Builds a Krylov subspace of length m with full reorthogonalization, solves the projected tridiagonal with vm_syev, and keeps the k eigenvalues at the end selected by which. Unwanted Ritz values are implicit shifts. The start vector is deterministic.

k <= m <= n. tol is the relative residual ||A u − λ u|| / max(|λ|, ||T||, ε), where ||T|| is the max absolute row sum of the projected tridiagonal and u is a unit Ritz vector. max_restarts < 0 is rejected. Zero restarts is one Krylov build.

On success the k eigenvalues are descending within the selected end. Eigenvector columns, when requested, are unit length with a positive entry of largest magnitude. eigenvectors NULL skips the vectors. info may be NULL. iters is the number of restarts performed (zero when the first build is enough), rel_res is the worst residual of the returned pairs, and ok matches the bool result.

A bad k, m, or tol (tol <= 0 or NaN), a null matvec or eigenvalues, or an eigenvectors matrix that is not n × k returns false and does not write the eigenvalues. If the iteration stops without meeting tol and at least k steps exist, the best pairs are written and the return is false.

Real symmetric only. No shift-invert, no block Lanczos, no complex Hermitian path, and no device path.

bool vm_lanczos_dense(const vm_mat* A, int k, vm_lanczos_which which, int m, vm_float_t tol, int max_restarts, vm_float_t* eigenvalues, vm_mat* eigenvectors, vm_ksp_info* info)

Lanczos on a dense symmetric matrix.

Parameters
A Symmetric matrix. Lower triangle only.
k Number of eigenpairs.
which Largest or smallest end.
m Krylov length. k <= m <= n.
tol Relative residual tolerance.
max_restarts Restart cap.
eigenvalues Output eigenvalues, length k.
eigenvectors Optional n × k eigenvectors. NULL skips them.
info Optional iteration stats. May be NULL.
Returns true when the worst returned residual is within tol.

Calls vm_symv, so only the lower triangle of A is read. A is not modified. A must be square. The remaining arguments match vm_lanczos.

bool vm_lanczos_sparse(const vm_spmat* A, int k, vm_lanczos_which which, int m, vm_float_t tol, int max_restarts, vm_float_t* eigenvalues, vm_mat* eigenvectors, vm_ksp_info* info)

Lanczos on a square CSR matrix.

Parameters
A CSR operator.
k Number of eigenpairs.
which Largest or smallest end.
m Krylov length. k <= m <= n.
tol Relative residual tolerance.
max_restarts Restart cap.
eigenvalues Output eigenvalues, length k.
eigenvectors Optional n × k eigenvectors. NULL skips them.
info Optional iteration stats. May be NULL.
Returns true when the worst returned residual is within tol.

Calls vm_spmv. The matrix must already store the operator the product should apply. A nonsymmetric CSR is applied as stored; the eigenvalues are meaningful when that operator is symmetric.