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 columns0 . -
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
targetsteps 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, andQ_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 - kunwanted 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_, 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_, so only the lower triangle of A is read. A is not modified. A must be square. The remaining arguments match vm_.
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_. 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.