libqe 0.2.6-dev.9001
QE C++ library
Loading...
Searching...
No Matches
qe::linalg Namespace Reference

Functions

Eigendecomposition
void eig_sym (arma::vec &eigval, arma::mat &eigvec, const arma::mat &S)
 Eigendecomposition of a real symmetric matrix.
 
QR Factorization
void qr_full (arma::mat &Q, arma::mat &R, const arma::mat &A)
 Full (square) QR factorization of an n×p matrix.
 
Singular Value Decomposition
void svd (arma::mat &U, arma::vec &s, arma::mat &V, const arma::mat &A)
 Full SVD: A ≈ U * diag(s) * V', singular values descending.
 
void svd_right (arma::vec &s, arma::mat &V, const arma::mat &A)
 Singular values and right singular vectors only (left vectors skipped).
 
Linear System Solve
arma::mat solve_spd (const arma::mat &A, const arma::mat &b, double tol=1e-12)
 Solve A*x = b for symmetric positive (semi-)definite A.
 
arma::mat inv_sym (const arma::mat &A, double tol=1e-12)
 Inverse of a symmetric positive (semi-)definite matrix, falling back to the pseudo-inverse when it is singular.
 

Function Documentation

◆ eig_sym()

void qe::linalg::eig_sym ( arma::vec &  eigval,
arma::mat &  eigvec,
const arma::mat &  S 
)
inline

Eigendecomposition of a real symmetric matrix.

Computes eigenvalues and eigenvectors of the symmetric p×p matrix S and returns them sorted in ascending eigenvalue order.

Parameters
[out]eigvalp×1 vector of eigenvalues, ascending.
[out]eigvecp×p matrix whose columns are the corresponding orthonormal eigenvectors.
[in]SReal symmetric p×p input matrix (not modified).
Warning
With ARMA_USE_LAPACK defined this calls arma::eig_sym directly. Without LAPACK the fallback runs up to 50*p cyclic Jacobi sweeps; convergence is guaranteed for well-conditioned matrices but may be slow for p > 200.
Note
Fallback algorithm: classical cyclic-by-rows Jacobi iteration. Convergence is declared when the Frobenius norm of the strictly off-diagonal part drops below 1e-13.

< working copy of S

< accumulate rotations; starts as identity

◆ inv_sym()

arma::mat qe::linalg::inv_sym ( const arma::mat &  A,
double  tol = 1e-12 
)
inline

Inverse of a symmetric positive (semi-)definite matrix, falling back to the pseudo-inverse when it is singular.

Parameters
[in]AReal symmetric positive (semi-)definite n×n matrix.
[in]tolWithout LAPACK: relative eigenvalue threshold, as in solve_spd. Default: 1e-12.
Returns
n×n matrix: A^-1, or the pseudo-inverse when A is singular.
Warning
With ARMA_USE_LAPACK defined this calls arma::inv and, when that reports a singular matrix, arma::pinv. Without LAPACK it is the eigendecomposition-based pseudo-inverse (solve_spd with the identity), which equals the inverse for a non-singular A.

◆ qr_full()

void qe::linalg::qr_full ( arma::mat &  Q,
arma::mat &  R,
const arma::mat &  A 
)
inline

Full (square) QR factorization of an n×p matrix.

Produces a square n×n orthogonal factor Q and an n×p upper-trapezoidal factor R such that A = Q*R. This matches the convention of arma::qr(), which returns the full (non-economy) Q.

Parameters
[out]Qn×n orthogonal matrix.
[out]Rn×p upper-trapezoidal matrix.
[in]An×p input matrix (not modified).
Warning
With ARMA_USE_LAPACK defined this calls arma::qr directly. Without LAPACK the fallback constructs the full ONB by seeding columns beyond A.n_cols with standard basis vectors and then applying modified Gram-Schmidt; numerical quality degrades for nearly rank-deficient inputs.
Note
The caller (orthogonal_svd) depends on Q being n×n so that Q.cols(k, n-1) spans the null space of A^T.

< standard basis vector e_j used as seed

◆ solve_spd()

arma::mat qe::linalg::solve_spd ( const arma::mat &  A,
const arma::mat &  b,
double  tol = 1e-12 
)
inline

Solve A*x = b for symmetric positive (semi-)definite A.

Returns the n×k solution matrix x given the n×n system matrix A and the n×k right-hand side b. Near-zero eigenvalues are handled gracefully, making this a pseudo-inverse solve safe for rank-deficient Gram matrices.

Parameters
[in]AReal symmetric positive (semi-)definite n×n matrix.
[in]bn×k right-hand side matrix.
[in]tolRelative threshold below which eigenvalues are treated as zero. Eigenvalues with |λ| < tol * max(|λ|) are inverted as zero (i.e. those directions are projected out). Default: 1e-12.
Returns
n×k solution matrix x satisfying A*x ≈ b.
Warning
With ARMA_USE_LAPACK defined this delegates to arma::solve(A, b, arma::solve_opts::fast), which uses a Cholesky factorization and does not handle rank deficiency. Without LAPACK the eigendecomposition-based pseudo-inverse is used, which is safe for rank-deficient inputs but slower.
Note
Without LAPACK: A = V * diag(λ) * V^T is computed via eig_sym; the solution is x = V * diag(1/λ) * V^T * b with small λ skipped.

< A = eigvec * diag(eigval) * eigvec^T

◆ svd()

void qe::linalg::svd ( arma::mat &  U,
arma::vec &  s,
arma::mat &  V,
const arma::mat &  A 
)
inline

Full SVD: A ≈ U * diag(s) * V', singular values descending.

Decomposes the n×p matrix A into left singular vectors U, singular values s, and right singular vectors V.

Parameters
[out]Un×min(n,p) matrix of left singular vectors.
[out]smin(n,p)×1 vector of singular values, descending.
[out]Vp×p matrix of right singular vectors (all p columns).
[in]An×p input matrix (not modified).
Warning
With ARMA_USE_LAPACK defined this calls arma::svd directly. Without LAPACK the fallback computes A^T*A, calls eig_sym, and derives U via U[:,j] = A*V[:,j]/s_j. Columns with s_j < 1e-14 are zeroed. Accuracy is governed by the conditioning of A^T*A (squares the condition number of A).
Note
Fallback algorithm: eigendecomposition of $A^T A$ via cyclic Jacobi (Demmel & Veselić 1992). Suitable for the small, moderately conditioned matrices typical in ENA (p ≤ ~100).

< p×p symmetric Gram matrix

◆ svd_right()

void qe::linalg::svd_right ( arma::vec &  s,
arma::mat &  V,
const arma::mat &  A 
)
inline

Singular values and right singular vectors only (left vectors skipped).

A lighter-weight variant of svd() for callers that only need s and V. Under LAPACK the full arma::svd is still called internally; the savings come in WASM builds where computing U is avoided.

Parameters
[out]smin(n,p)×1 vector of singular values, descending.
[out]Vp×p matrix of right singular vectors.
[in]An×p input matrix (not modified).
Warning
With ARMA_USE_LAPACK defined this still invokes the full arma::svd; the left-vector output is discarded.