MTL5 -- Matrix Template Library C++ API
mtl:: -- C++20 header-only linear algebra for mixed-precision algorithm design
Loading...
Searching...
No Matches
mtl::itl Namespace Reference

Namespaces

namespace  detail
namespace  mg
namespace  pc
namespace  smoother

Classes

struct  ritz_pairs
 A set of computed Ritz pairs (approximate eigenpairs from a Krylov subspace). More...
struct  eigenpair
 A single computed eigenpair. More...
class  basic_iteration
 Convergence controller: tracks iteration count, residual, and tolerance. More...
class  cyclic_iteration
 Prints residual every cycle iterations (cycle >= 1); prints a summary when the solver returns it as its int result, once. More...
class  noisy_iteration
 Prints residual at every iteration (cyclic_iteration with cycle = 1). More...
struct  idrs_options
 Options of idr_s (issue 874). More...

Enumerations

enum class  eigen_which { largest_magnitude , smallest_magnitude , largest_algebraic , smallest_algebraic }
 Which end of the spectrum a Krylov eigensolver should return. More...

Functions

template<typename Accumulator = void, typename LinearOp, typename T>
ritz_pairs< std::complex< T > > arnoldi (const LinearOp &A, vec::dense_vector< T > v0, std::size_t k, eigen_which which=eigen_which::largest_magnitude, std::size_t subspace=0, std::type_identity_t< T > tol=T(1e-8))
 Compute k eigenpairs of a general operator A by the Arnoldi method.
template<typename Accumulator = void, typename LinearOp, typename T>
vec::dense_vector< T > ev_matvec (const LinearOp &A, const vec::dense_vector< T > &x)
 Apply a LinearOperator and materialize the result as a dense_vector.
template<typename Accumulator = void, typename LinearOp, typename T>
ritz_pairs< T > lanczos (const LinearOp &A, vec::dense_vector< T > v0, std::size_t k, eigen_which which=eigen_which::largest_algebraic, std::size_t subspace=0, std::type_identity_t< T > tol=T(1e-8))
 Compute k extremal eigenpairs of a symmetric operator A by the Lanczos method.
template<typename Accumulator = void, typename LinearOp, typename T>
eigenpair< T > power_iteration (const LinearOp &A, vec::dense_vector< T > v0, int max_iter=1000, std::type_identity_t< T > tol=T(1e-10))
 Power iteration for the eigenvalue of largest magnitude (assumed real and dominant, as for symmetric / SPD operators or any operator with a real dominant eigenvalue).
template<typename Accumulator = void, typename LinearOp, typename VecIn, typename VecOut>
void apply_operator (const LinearOp &A, const VecIn &x, VecOut &y)
 y = A * x, for the ITL Krylov solvers.
template<typename Accumulator = void, typename LinearOp, typename VecIn, typename VecOut>
void apply_transpose (const LinearOp &A, const VecIn &x, VecOut &y)
 y = A^T * x, for the solvers that need the transpose (bicg, qmr).
template<typename Accumulator = void, typename LinearOp, typename VecIn, typename VecOut, typename VecTmp>
void apply_adjoint (const LinearOp &A, const VecIn &x, VecOut &y, VecTmp &tmp)
 y = A^H * x, the adjoint, for the shadow recurrences of bicg and qmr.
template<typename Accumulator = void, typename LinearOp, typename VecX, typename VecB, typename PC, typename Iter>
requires FieldVector<VecX>
int bicg (const LinearOp &A, VecX &x, const VecB &b, const PC &M, Iter &iter)
 BiConjugate Gradient method.
template<typename Accumulator = void, typename LinearOp, typename VecX, typename VecB, typename PC, typename Iter>
requires FieldVector<VecX>
int bicgstab (const LinearOp &A, VecX &x, const VecB &b, const PC &M, Iter &iter)
 BiCGSTAB method for non-symmetric systems.
template<typename Accumulator = void, typename LinearOp, typename VecX, typename VecB, typename PC, typename Iter>
requires FieldVector<VecX>
int bicgstab_ell (const LinearOp &A, VecX &x, const VecB &b, const PC &M, Iter &iter, std::size_t ell)
 BiCGSTAB(ell) method for non-symmetric systems.
template<typename Accumulator = void, typename LinearOp, typename VecX, typename VecB, typename PC, typename Iter>
requires FieldVector<VecX>
int cg (const LinearOp &A, VecX &x, const VecB &b, const PC &M, Iter &iter)
 Preconditioned Conjugate Gradient method.
template<typename Accumulator = void, typename LinearOp, typename VecX, typename VecB, typename PC, typename Iter>
requires FieldVector<VecX>
int cgs (const LinearOp &A, VecX &x, const VecB &b, const PC &M, Iter &iter)
 Conjugate Gradient Squared method for non-symmetric systems.
template<typename Accumulator = void, typename LinearOp, typename VecX, typename VecB, typename PC, typename Iter>
requires FieldVector<VecX>
int gmres (const LinearOp &A, VecX &x, const VecB &b, const PC &M, Iter &iter, int restart=30)
 GMRES with restart.
template<typename Accumulator = void, typename LinearOp, typename VecX, typename VecB, typename PC, typename Iter>
requires FieldVector<VecX>
int idr_s (const LinearOp &A, VecX &x, const VecB &b, const PC &M, Iter &iter, const idrs_options &opt, std::vector< vec::dense_vector< typename VecX::value_type > > P)
 IDR(s) for nonsymmetric systems A x = b, right preconditioned by M, with the shadow space P given: s = P.size() vectors of length n, of full rank.
template<typename Accumulator = void, typename LinearOp, typename VecX, typename VecB, typename PC, typename Iter>
requires FieldVector<VecX>
int idr_s (const LinearOp &A, VecX &x, const VecB &b, const PC &M, Iter &iter, const idrs_options &opt)
 IDR(s) with the options in opt and a random shadow space drawn from opt.seed (see idrs_options).
template<typename Accumulator = void, typename LinearOp, typename VecX, typename VecB, typename PC, typename Iter>
requires FieldVector<VecX>
int idr_s (const LinearOp &A, VecX &x, const VecB &b, const PC &M, Iter &iter, typename VecX::size_type s=4)
 IDR(s) with shadow space dimension s and the default options.
template<typename Accumulator = void, typename LinearOp, typename VecX, typename VecB, typename PC, typename Iter>
requires FieldVector<VecX>
int minres (const LinearOp &A, VecX &x, const VecB &b, const PC &M, Iter &iter)
 MINRES method for symmetric (possibly indefinite) systems.
template<typename Accumulator = void, typename LinearOp, typename VecX, typename VecB, typename PC, typename Iter>
requires FieldVector<VecX>
int qmr (const LinearOp &A, VecX &x, const VecB &b, const PC &M, Iter &iter)
 QMR solver for non-symmetric systems A*x = b.
template<typename Accumulator = void, typename LinearOp, typename VecX, typename VecB, typename PC, typename Iter>
requires FieldVector<VecX>
int tfqmr (const LinearOp &A, VecX &x, const VecB &b, const PC &M, Iter &iter)
 TFQMR solver for non-symmetric systems A*x = b.

Enumeration Type Documentation

◆ eigen_which

enum class mtl::itl::eigen_which
strong

Which end of the spectrum a Krylov eigensolver should return.

Algebraic selectors apply to real (symmetric) spectra; magnitude selectors apply to any spectrum (including complex Arnoldi Ritz values).

Enumerator
largest_magnitude 
smallest_magnitude 
largest_algebraic 
smallest_algebraic 

Function Documentation

◆ apply_adjoint()

template<typename Accumulator = void, typename LinearOp, typename VecIn, typename VecOut, typename VecTmp>
void mtl::itl::apply_adjoint ( const LinearOp & A,
const VecIn & x,
VecOut & y,
VecTmp & tmp )

y = A^H * x, the adjoint, for the shadow recurrences of bicg and qmr.

The solvers' dot products conjugate their first argument, so the shadow sequence must be built with A^H. With A^T, bicg and qmr did not converge on a complex non-Hermitian system: 200 iterations at a residual of 2.1 where cgs and gmres took 3 (issue 924). For a real element type this is apply_transpose. For a complex one it is A^H x = conj(A^T conj(x)), which needs only the transpose, so a matrix-free operator still provides just trans(A). tmp holds conj(x): x's size for a complex type, unused otherwise.

◆ apply_operator()

template<typename Accumulator = void, typename LinearOp, typename VecIn, typename VecOut>
void mtl::itl::apply_operator ( const LinearOp & A,
const VecIn & x,
VecOut & y )

y = A * x, for the ITL Krylov solvers.

A Matrix operand – dense2D, compressed2D, a view – goes through mult<Accumulator>, as the solvers called it before #278: any accumulator reaches the matvec, and void keeps mult's BLAS / SIMD / native dispatch, so the result is byte-identical.

Any other operand must be a LinearOperator: only A * x is available, and y takes its entries. A matrix-free operator does its own accumulation, so a non-void Accumulator cannot reach it; it still governs the solver's dot products. This is the contract of the eigensolvers' ev_matvec (#591).

◆ apply_transpose()

template<typename Accumulator = void, typename LinearOp, typename VecIn, typename VecOut>
void mtl::itl::apply_transpose ( const LinearOp & A,
const VecIn & x,
VecOut & y )

y = A^T * x, for the solvers that need the transpose (bicg, qmr).

A Matrix operand goes through mult<Accumulator>(trans(A), x, y). A matrix-free operator provides its transpose as trans(A), found by argument-dependent lookup, returning an operator with operator*; the accumulator then covers only the dot products, as in apply_operator.

◆ arnoldi()

template<typename Accumulator = void, typename LinearOp, typename T>
ritz_pairs< std::complex< T > > mtl::itl::arnoldi ( const LinearOp & A,
vec::dense_vector< T > v0,
std::size_t k,
eigen_which which = eigen_which::largest_magnitude,
std::size_t subspace = 0,
std::type_identity_t< T > tol = T(1e-8) )

Compute k eigenpairs of a general operator A by the Arnoldi method.

v0 is the starting vector; which selects which Ritz values to keep (magnitude selectors, or algebraic selectors on the real part); subspace is the Krylov dimension to build (default: a modest multiple of k, capped at n); tol flags convergence via the Ritz residual estimate.

Returns k complex Ritz values (ordered per which) and their complex Ritz vectors (columns). A is any LinearOperator.

◆ bicg()

template<typename Accumulator = void, typename LinearOp, typename VecX, typename VecB, typename PC, typename Iter>
requires FieldVector<VecX>
int mtl::itl::bicg ( const LinearOp & A,
VecX & x,
const VecB & b,
const PC & M,
Iter & iter )

BiConjugate Gradient method.

Solves A*x = b for non-symmetric A. Uses trans(A) internally. M must provide solve() and adjoint_solve().

Accumulator (optional): accumulation type for the two dot products and the two matrix-vector products (see math/accumulator_traits.hpp, #158). Defaults to void, matching dot()/mult()\'s own default – unspecified behavior is unchanged. A is a Matrix or any LinearOperator (A * x) that also provides trans(A) by ADL. With a matrix-free A the Accumulator reaches only the dot products (apply_operator.hpp, #278).

◆ bicgstab()

template<typename Accumulator = void, typename LinearOp, typename VecX, typename VecB, typename PC, typename Iter>
requires FieldVector<VecX>
int mtl::itl::bicgstab ( const LinearOp & A,
VecX & x,
const VecB & b,
const PC & M,
Iter & iter )

BiCGSTAB method for non-symmetric systems.

Solves A*x = b with preconditioner M and iteration controller iter.

Accumulator (optional): accumulation type for the dot products and the matrix-vector products (see math/accumulator_traits.hpp, #158). Defaults to void, matching dot()/mult()'s own default – unspecified behavior is unchanged. A is a Matrix or any LinearOperator (A * x). With a matrix-free A the Accumulator reaches only the dot products (apply_operator.hpp, #278).

◆ bicgstab_ell()

template<typename Accumulator = void, typename LinearOp, typename VecX, typename VecB, typename PC, typename Iter>
requires FieldVector<VecX>
int mtl::itl::bicgstab_ell ( const LinearOp & A,
VecX & x,
const VecB & b,
const PC & M,
Iter & iter,
std::size_t ell )

BiCGSTAB(ell) method for non-symmetric systems.

ell >= 1 controls the degree of the stabilization polynomial. Higher ell may improve convergence on stiff or highly non-normal problems.

Accumulator (optional): accumulation type for the dot products and the matrix-vector products (see math/accumulator_traits.hpp, #158). Defaults to void, matching dot()/mult()'s own default – unspecified behavior is unchanged. two_norm() calls are NOT routed through Accumulator, matching the convention set by cg/bicgstab/gmres/idr_s/bicg. A is a Matrix or any LinearOperator (A * x). With a matrix-free A the Accumulator reaches only the dot products (apply_operator.hpp, #278).

◆ cg()

template<typename Accumulator = void, typename LinearOp, typename VecX, typename VecB, typename PC, typename Iter>
requires FieldVector<VecX>
int mtl::itl::cg ( const LinearOp & A,
VecX & x,
const VecB & b,
const PC & M,
Iter & iter )

Preconditioned Conjugate Gradient method.

Solves A*x = b where A is symmetric positive definite. M is a preconditioner satisfying M.solve(x, b), symmetric (Hermitian) positive definite. An M found not to be – rho = r^H M^{-1} r negative beyond rounding for an r that has not converged – stops the solve with error code 7 (basic_iteration::not_positive_definite), with x as the last step left it (#949). An exactly zero rho stays a breakdown (2). iter is a convergence controller (basic_iteration or derived).

Accumulator (optional): accumulation type for the two dot products and the matrix-vector product (see math/accumulator_traits.hpp, #158). Defaults to void, matching dot()/mult()'s own default – unspecified behavior is unchanged.

Returns the iteration object (convertible to int error code). A is a Matrix or any LinearOperator (A * x). With a matrix-free A the Accumulator reaches only the dot products (apply_operator.hpp, #278).

◆ cgs()

template<typename Accumulator = void, typename LinearOp, typename VecX, typename VecB, typename PC, typename Iter>
requires FieldVector<VecX>
int mtl::itl::cgs ( const LinearOp & A,
VecX & x,
const VecB & b,
const PC & M,
Iter & iter )

Conjugate Gradient Squared method for non-symmetric systems.

Solves A*x = b with preconditioner M and iteration controller iter.

Accumulator (optional): accumulation type for the dot products and the matrix-vector products (see math/accumulator_traits.hpp, #158). Defaults to void, matching dot()/mult()'s own default – unspecified behavior is unchanged. A is a Matrix or any LinearOperator (A * x). With a matrix-free A the Accumulator reaches only the dot products (apply_operator.hpp, #278).

◆ ev_matvec()

template<typename Accumulator = void, typename LinearOp, typename T>
vec::dense_vector< T > mtl::itl::ev_matvec ( const LinearOp & A,
const vec::dense_vector< T > & x )

Apply a LinearOperator and materialize the result as a dense_vector.

Accumulator (#591): a non-void accumulator reaches the matvec when A is a Matrix – dense or compressed2D – through mult<Accumulator>, as in the ITL linear solvers (cg.hpp). A matrix-free operator only offers A * x, so it has no accumulator to take, and the solvers' Accumulator then covers only their dot products. void keeps A * x for every operator, byte-identical to the helper before #591.

◆ gmres()

template<typename Accumulator = void, typename LinearOp, typename VecX, typename VecB, typename PC, typename Iter>
requires FieldVector<VecX>
int mtl::itl::gmres ( const LinearOp & A,
VecX & x,
const VecB & b,
const PC & M,
Iter & iter,
int restart = 30 )

GMRES with restart.

Solves A*x = b with left preconditioner M. restart: maximum Krylov subspace dimension before restart (default 30). A is a Matrix or any LinearOperator (A * x). With a matrix-free A the Accumulator reaches only the dot products (apply_operator.hpp, #278).

◆ idr_s() [1/3]

template<typename Accumulator = void, typename LinearOp, typename VecX, typename VecB, typename PC, typename Iter>
requires FieldVector<VecX>
int mtl::itl::idr_s ( const LinearOp & A,
VecX & x,
const VecB & b,
const PC & M,
Iter & iter,
const idrs_options & opt )

IDR(s) with the options in opt and a random shadow space drawn from opt.seed (see idrs_options).

◆ idr_s() [2/3]

template<typename Accumulator = void, typename LinearOp, typename VecX, typename VecB, typename PC, typename Iter>
requires FieldVector<VecX>
int mtl::itl::idr_s ( const LinearOp & A,
VecX & x,
const VecB & b,
const PC & M,
Iter & iter,
const idrs_options & opt,
std::vector< vec::dense_vector< typename VecX::value_type > > P )

IDR(s) for nonsymmetric systems A x = b, right preconditioned by M, with the shadow space P given: s = P.size() vectors of length n, of full rank.

P is orthonormalized first, which leaves the method unchanged (it depends on the span of P only) and keeps P^H G well conditioned.

The biorthogonal IDR(s) (van Gijzen & Sonneveld, ACM TOMS 38(1), 2011, Alg. 913). Each cycle is s + 1 steps, each one product with A and one preconditioner solve, and each counted as one iteration. The first s steps keep the new direction G_k orthogonal to P_0 .. P_{k-1}, so the small system P^H G is lower triangular, solved by forward substitution in O(s^2); the last step picks the cycle's omega. This replaces the prototype IDR(s) of 2008 (SIAM J. Sci. Comput. 31, Alg. 3.1), whose s x s system was dense and solved by elimination at every step, and whose iteration counts became erratic at tight tolerances: at rtol 1e-13 on a 2D convection-diffusion system with ILU(0), s = 2 took 729 iterations and s = 8 took 1612, where s = 4 took 71 (issue 874).

Convergence is judged by the true residual: when the recursively updated residual meets the tolerance, or the iteration limit is reached, b - A x is computed, and if that has not converged the method restarts from the current x, as cg, bicgstab, tfqmr and the rest do (#752, issue 873). The returned iter reports the true residual.

A near-breakdown in P^H G (a diagonal entry negligible against ||G_k||) restarts the method from the current x. One at the first step after a start means P is degenerate for the residual: a new shadow space is drawn, each draw from the next seed (opt.seed + 1, + 2, ..., also when P was supplied). Up to three redraws in a row are allowed; a successful step resets that budget (issue 909), so a long solve can draw past opt.seed + 3. The solve fails when three consecutive redraws do not help. Return codes: 0 converged, 1 iteration limit, 2 breakdown in P^H G after three consecutive redraws, 3 breakdown in omega (t = 0, or t orthogonal to r to rounding, |cos(r, t)| <= eps).

Accumulator (optional): accumulation type for the dot products and the matrix-vector products. A is a Matrix or any LinearOperator (A * x).

◆ idr_s() [3/3]

template<typename Accumulator = void, typename LinearOp, typename VecX, typename VecB, typename PC, typename Iter>
requires FieldVector<VecX>
int mtl::itl::idr_s ( const LinearOp & A,
VecX & x,
const VecB & b,
const PC & M,
Iter & iter,
typename VecX::size_type s = 4 )

IDR(s) with shadow space dimension s and the default options.

◆ lanczos()

template<typename Accumulator = void, typename LinearOp, typename T>
ritz_pairs< T > mtl::itl::lanczos ( const LinearOp & A,
vec::dense_vector< T > v0,
std::size_t k,
eigen_which which = eigen_which::largest_algebraic,
std::size_t subspace = 0,
std::type_identity_t< T > tol = T(1e-8) )

Compute k extremal eigenpairs of a symmetric operator A by the Lanczos method.

v0 is the starting vector; which selects the end of the spectrum; subspace is the Krylov dimension to build (default: a modest multiple of k, capped at n – larger is more accurate); tol is the Ritz residual tolerance used to flag convergence.

Returns k Ritz values (ordered per which) and their Ritz vectors (columns). A is any LinearOperator; it MUST be symmetric for the tridiagonal projection to be meaningful.

◆ minres()

template<typename Accumulator = void, typename LinearOp, typename VecX, typename VecB, typename PC, typename Iter>
requires FieldVector<VecX>
int mtl::itl::minres ( const LinearOp & A,
VecX & x,
const VecB & b,
const PC & M,
Iter & iter )

MINRES method for symmetric (possibly indefinite) systems.

Solves A*x = b with preconditioner M and iteration controller iter. A must be symmetric and M symmetric positive definite (Hermitian for a complex system): the Lanczos process runs in the M^{-1} inner product. An M found not to be – a v^H M^{-1} v negative beyond rounding, at the initial residual or at a Lanczos step – stops the solve with error code 7 (basic_iteration::not_positive_definite), with x as the last valid step left it (#949). Formerly its absolute value was taken, and the solve ran on a Lanczos process that is not defined, to a generic breakdown (2) much later.

Accumulator (optional): accumulation type for the dot products and the matrix-vector products (see math/accumulator_traits.hpp, #158). Defaults to void, matching dot()/mult()'s own default – unspecified behavior is unchanged. A is a Matrix or any LinearOperator (A * x). With a matrix-free A the Accumulator reaches only the dot products (apply_operator.hpp, #278).

◆ power_iteration()

template<typename Accumulator = void, typename LinearOp, typename T>
eigenpair< T > mtl::itl::power_iteration ( const LinearOp & A,
vec::dense_vector< T > v0,
int max_iter = 1000,
std::type_identity_t< T > tol = T(1e-10) )

Power iteration for the eigenvalue of largest magnitude (assumed real and dominant, as for symmetric / SPD operators or any operator with a real dominant eigenvalue).

Returns the Rayleigh-quotient eigenvalue and unit eigenvector. Converges when the Ritz residual ||A v - lambda v|| <= tol.

A is any LinearOperator (A * v yields a vector); v0 is the starting vector (need not be normalized). O(1) vectors of storage; one matvec/iter.

Accumulator forms the Rayleigh quotient and the Ritz residual and, when A is a Matrix (dense2D, compressed2D), the matvec too, through mult<Accumulator> (#591). A matrix-free operator keeps A * v. The default void accumulates in T and keeps A * v for every operator.

◆ qmr()

template<typename Accumulator = void, typename LinearOp, typename VecX, typename VecB, typename PC, typename Iter>
requires FieldVector<VecX>
int mtl::itl::qmr ( const LinearOp & A,
VecX & x,
const VecB & b,
const PC & M,
Iter & iter )

QMR solver for non-symmetric systems A*x = b.

A is a Matrix or any LinearOperator (A * x) that also provides trans(A) by ADL. With a matrix-free A the Accumulator reaches only the dot products (apply_operator.hpp, #278).

◆ tfqmr()

template<typename Accumulator = void, typename LinearOp, typename VecX, typename VecB, typename PC, typename Iter>
requires FieldVector<VecX>
int mtl::itl::tfqmr ( const LinearOp & A,
VecX & x,
const VecB & b,
const PC & M,
Iter & iter )

TFQMR solver for non-symmetric systems A*x = b.

Does not require A^T (transpose-free). Right preconditioner M: it solves (A M^-1) z = b with x = M^-1 z, so the residual it tracks is b - A x.

Freund (SIAM J. Sci. Comput. 14, 1993); Saad, Iterative Methods for Sparse Linear Systems, 2nd ed., Algorithm 7.8. Each iteration is two half-steps with two search vectors, u_m and u_{m+1} = u_m - alpha v_m; the next pair starts from u_{m+2} = w + beta u_{m+1}, and v carries A M^-1 u through the recurrence v_{m+2} = A M^-1 u_{m+2} + beta (A M^-1 u_{m+1} + beta v_m). The u vectors are kept preconditioned (y = M^-1 u), so the update direction d lives in x-space. Before #731 the u recurrence was missing – w stood in for u_{m+1} and for u_{m+2} – so the method stalled on unsymmetric systems and converged slowly on SPD ones.

The convergence test is on tau, the quasi-residual norm, which bounds the true residual by sqrt(m + 1) tau after m half-steps; in practice the two are close.

Cost per iteration: two products with A, two preconditioner solves, three dot products and two norms.

Accumulator (optional): accumulation type for the dot products and the matrix-vector products (see math/accumulator_traits.hpp, #158). Defaults to void, matching dot()/mult()'s own default – unspecified behavior is unchanged. A is a Matrix or any LinearOperator (A * x). With a matrix-free A the Accumulator reaches only the dot products (apply_operator.hpp, #278).