Skip to main content

Crate lib_jsl_linalg

Crate lib_jsl_linalg 

Source
Expand description

Dense linear algebra in pure Rust for real (f64) and complex (Complex<f64>) matrices.

OperationTraitAlgorithm
Solve A x = b, inverse, determinantSolve, Inverse, Factorize, DeterminantLU with partial pivoting
Hermitian positive-definite factorisationCholeskyCholesky A = L Lᴴ
Triangular systemsSolveTriangularForward and back substitution
Hermitian / symmetric eigenproblemEigh, EigValshHouseholder tridiagonalisation and implicit QL
General eigenproblemEig, EigValsHessenberg reduction and shifted QR (complex Schur form)
Singular value decompositionSVD, SvdJacobiGolub-Kahan bidiagonalisation; one-sided Jacobi for high relative accuracy
Least squares, minimum-norm for rank-deficient systemsLeastSquaresSvdHouseholder QR with column pivoting; SVD when rank-deficient
Complex Schur formschur_complexHessenberg reduction and shifted QR
QR (optionally pivoted), RQ, Hessenberg, real Schur, LDLᵀ, polarqr, rq, hessenberg, schur_real, ldl, polarHouseholder reflectors as LAPACK’s; Bunch-Kaufman pivoting
Generalised eigenproblems A x = λ B xeigh_generalized, eig_generalized, qzCholesky reduction; the QZ algorithm
Structured matrices and their solverstoeplitz, circulant, solve_toeplitz, solve_circulant, solve_banded, solveh_banded, …Levinson recursion; FFT; band LU and Cholesky
General matrix functions, principal anglesfunm, subspace_anglesSchur-Parlett; SVDs of orthonormal bases
Matrix exponentialexpmScaling and squaring with Padé approximants
Square root, logarithm, fractional powerssqrtm, logm, fractional_matrix_powerSchur method; inverse scaling and squaring
Pseudoinverse, rank, null space, range, norms, conditionpinv, matrix_rank, null_space, orth, norm, condFrom the SVD
Sparse matrices: storage, sparse LU, Krylov solvers, a few eigenvalues and singular valuessparseCSR/CSC; Gilbert-Peierls LU with reverse Cuthill-McKee; CG, MINRES, GMRES, LGMRES, BiCGSTAB, LSQR; Lanczos eigsh, svds; Krylov-Schur eigs
Sylvester and Lyapunov equationssolve_sylvester, solve_continuous_lyapunov, solve_discrete_lyapunovBartels-Stewart on the complex Schur form
Algebraic Riccati equationssolve_continuous_are, solve_discrete_areMatrix sign function; structure-preserving doubling; Newton refinement

The traits follow the names and signatures of the ndarray-linalg crate, so code written against it ports by changing the import. They are implemented for every ndarray ArrayBase<S, Ix2> (owned arrays and views). Everything is written for clarity and moderate sizes (up to a few hundred rows); for large problems an optimised LAPACK will be faster. The benchmarks in crates/linalg_bench compare the two.

See the book chapter Dense linear algebra.

Modules§

sparse
Sparse matrices and solvers for large systems, following scipy.sparse and scipy.sparse.linalg: COO, CSR and CSC storage; a sparse LU factorisation with a fill-reducing column ordering (minimum degree, COLAMD, nested dissection or reverse Cuthill-McKee; spsolve, SparseLu); Krylov solvers (CG, MINRES, GMRES, LGMRES, BiCGSTAB, LSQR) for any LinearOperator, with Jacobi, incomplete-LU and incomplete-Cholesky preconditioners; a few eigenvalues or singular values by Lanczos methods (eigsh, its shift-invert mode, svds) and of nonsymmetric matrices by Arnoldi’s (eigs, eigs_shift_invert); and expm_multiply. These are real (f64); complex matrices have their own storage, LU and Krylov solvers (ComplexCsrMatrix, ComplexSparseLu, cg_complex, gmres_complex, bicgstab_complex).

Structs§

GeneralizedEigen
The solution of the generalised eigenproblem A x = λ B x.
LdlFactors
An LDLᵀ (or LDLᴴ) factorisation A = L D Lᴴ with symmetric pivoting.
LeastSquaresResult
The result of LeastSquaresSvd::least_squares.
LuFactorization
A factorised square matrix, reusable for many right-hand sides.
OrdQz
The result of ordqz.
OrdQzComplex
The result of ordqz_complex.
QrFactors
A QR factorisation A P = Q R (P the identity without pivoting).
QzFactors
The generalised complex Schur form A = Q S Zᴴ, B = Q T Zᴴ.
RealQzFactors
The real generalised Schur form A = Q AA Zᵀ, B = Q BB Zᵀ.

Enums§

ConvolutionMode
Which part of a convolution convolution_matrix produces (as numpy.convolve’s mode).
DftScale
How dft scales its matrix (SciPy’s scale).
Diag
Whether a triangular matrix has an implicit unit diagonal.
EigenSort
Which eigenvalues schur_sorted, schur_sorted_complex, ordqz and ordqz_complex move to the top left, as SciPy’s sort argument.
LinalgError
Why a decomposition or solve failed. Converts into the library-wide ErrorsJSL with ?.
Norm
Which matrix norm norm computes (NumPy’s ord).
PascalKind
Which Pascal matrix (SciPy’s kind).
QrMode
How much of a QR (or RQ) factorisation to return (SciPy’s mode).
UPLO
Which triangle of a matrix to use (or produce).

Traits§

Cholesky
The Cholesky factor of a Hermitian positive-definite matrix whose uplo triangle is read: lower L with A = L Lᴴ for UPLO::Lower, upper U = Lᴴ for UPLO::Upper.
Determinant
The determinant of a square matrix.
Eig
Eigenvalues and unit eigenvectors (columns) of a general square matrix, both complex.
EigVals
Eigenvalues of a general square matrix.
EigValsh
Eigenvalues (ascending) of a Hermitian matrix.
Eigh
Eigenvalues (ascending) and orthonormal eigenvectors (columns) of a Hermitian matrix whose uplo triangle is read.
Factorize
An LU factorisation to solve many systems with the same matrix.
Inverse
The inverse of a square matrix.
LeastSquaresSvd
The minimum-norm least-squares solution of A x ≈ b, robust to rank deficiency.
SVD
The singular value decomposition A = U Σ Vᴴ: (U, σ, Vᴴ) with the full square unitary factors when requested and the min(m, n) singular values in descending order.
Scalar
A real or complex double-precision scalar.
Solve
Solves A x = b for square A by LU with partial pivoting.
SolveTriangular
Solves T X = B for a triangular T (only the uplo triangle is read) and a right-hand side that is a vector or a matrix.
SvdJacobi
The SVD by the one-sided Jacobi method, with the same output as SVD::svd. It is slower than the default (Golub-Kahan) method but determines small singular values to high relative accuracy, which matters for graded or badly scaled matrices.

Functions§

block_diag
The block diagonal matrix with the given blocks (SciPy’s block_diag).
circulant
The circulant matrix with first column c (SciPy’s circulant): C[i, j] = c[(i − j) mod n].
companion
The companion matrix of the polynomial a[0] xⁿ + a[1] xⁿ⁻¹ + … + a[n] (SciPy’s companion): first row −a[1..]/a[0], ones on the subdiagonal. Its eigenvalues are the polynomial’s roots.
cond
The condition number ‖A‖ ‖A⁻¹‖ in the given norm (NumPy’s cond); for the 2-norm, σ_max / σ_min, which is also defined for rectangular matrices. Infinite for singular matrices.
convolution_matrix
The matrix M with M v = convolve(a, v) for vectors v of length n (SciPy’s convolution_matrix).
dft
The discrete Fourier transform matrix W[j, k] = ω^{jk}, ω = e^{−2πi/n} (SciPy’s dft).
eig_generalized
The generalised eigenvalues and right eigenvectors of (A, B) (SciPy’s eig(a, b)), from the QZ form: for each k, (β_k S − α_k T) y = 0 is solved by back substitution with y_k = 1, and x = Z y is normalised to unit length.
eigh_generalized
The eigenvalues (ascending) and eigenvectors of the symmetric-definite problem A x = λ B x, A Hermitian and B Hermitian positive definite (SciPy’s eigh(a, b), LAPACK’s sygv type 1). With B = L Lᴴ, the problem becomes the standard C y = λ y for C = L⁻¹ A L⁻ᴴ, and x = L⁻ᴴ y; the eigenvectors are B-orthonormal, Xᴴ B X = I.
expm
The matrix exponential eᴬ = I + A + A²/2! + … (SciPy’s linalg.expm).
fiedler
The Fiedler matrix |a_i − a_j| (SciPy’s fiedler).
fractional_matrix_power
A^p = exp(p log A) for real p (SciPy’s fractional_matrix_power, by this route rather than its Schur-Padé algorithm).
funm
f(A) for a scalar function f (SciPy’s funm), by the Schur-Parlett method: with the complex Schur form A = Z T Zᴴ, F = f(T) is upper triangular with F_ii = f(T_ii), and the commutation T F = F T determines the rest, superdiagonal by superdiagonal: F_ij = (T_ij (F_jj − F_ii) + Σ_{i<k<j} (T_ik F_kj − F_ik T_kj)) / (T_jj − T_ii). Returns (Z F Zᴴ, error estimate); the estimate is large when eigenvalues nearly coincide, where this recurrence divides by small differences.
hadamard
The n × n Hadamard matrix of ±1 entries by Sylvester’s construction (SciPy’s hadamard); n must be a power of two. Its rows are orthogonal: H Hᵀ = n I.
hankel
The Hankel matrix with first column c and last row r (SciPy’s hankel); r[0] is ignored. Without r, the entries below the anti-diagonal are zero.
hessenberg
The Hessenberg form A = Q H Qᴴ (SciPy’s hessenberg with calc_q=True; LAPACK’s gehd2 and orghr): H is zero below its first subdiagonal, Q unitary. Reflector i zeros column i below row i + 1 and is applied from both sides. Returns (H, Q).
hilbert
The Hilbert matrix 1/(i + j + 1) (SciPy’s hilbert), a classic ill-conditioned test matrix.
invhilbert
The exact inverse of the Hilbert matrix (SciPy’s invhilbert), from its closed form (−1)^{i+j} (i + j + 1) binom(n+i, n−j−1) binom(n+j, n−i−1) binom(i+j, i)²; the integer entries are exact for n ≤ 14.
invpascal
The inverse of the Pascal matrix (SciPy’s invpascal): integer entries, (−1)^{i+j} binom(i, j) for the triangular kinds, and their product for the symmetric one.
ldl
The Bunch-Kaufman factorisation of a symmetric or Hermitian matrix (SciPy’s ldl, LAPACK’s sytf2/hetf2): A = L D Lᴴ (or U D Uᴴ with lower = false), D block diagonal with 1 × 1 and 2 × 2 blocks, and L a row permutation of a unit triangular matrix. A 2 × 2 pivot is taken when no diagonal entry is large enough relative to its column, α = (1 + √17)/8, which bounds the growth of the entries for indefinite matrices. Only the lower (or upper) triangle is read; hermitian = false factorises a complex symmetric matrix, A = L D Lᵀ.
leslie
The Leslie matrix of a population model (SciPy’s leslie): fecundities f (length n) on the first row, survival rates s (length n − 1) on the subdiagonal.
logm
The principal logarithm L with e^L = A and eigenvalues with imaginary parts in (−π, π) (SciPy’s logm), by the inverse scaling and squaring method on the Schur form: take square roots of T until ‖T − I‖₁ ≤ 0.25 (k times), evaluate log(I + X) by the [8/8] Padé approximant written as Gauss-Legendre quadrature, Σ wⱼ X (I + xⱼ X)⁻¹, and multiply by 2ᵏ.
matrix_rank
The numerical rank: the number of singular values above tol (NumPy’s default σ_max · max(m, n) · ε when None).
norm
A matrix norm (or, for the Minus variants, the corresponding minimum), as NumPy’s norm.
null_space
An orthonormal basis of the null space of A, as columns (SciPy’s null_space): the right singular vectors of the singular values at most rcond · σ_max (default max(m, n) ε).
ordqz
The real generalised Schur form with the eigenvalues selected by sort leading (SciPy’s ordqz(a, b, sort=..., output='real')): qz_real, then adjacent block swaps (LAPACK’s dtgsen/dtgexc) built from the generalised Sylvester equation A₁₁R − L A₂₂ = −A₁₂, B₁₁R − L B₂₂ = −B₁₂, whose solution spans the deflating subspaces of the lower block.
ordqz_complex
The complex generalised Schur form with the selected eigenvalues leading (SciPy’s ordqz(..., output='complex')): qz, then swaps of adjacent 1 × 1 blocks from the generalised Sylvester equation (LAPACK’s ztgexc).
orth
An orthonormal basis of the range (column space) of A, as columns (SciPy’s orth).
pascal
The Pascal matrix (SciPy’s pascal, exact=False): binomial coefficients, exact while they are below 2⁵³.
pinv
The Moore-Penrose pseudoinverse A⁺ = V Σ⁺ Uᴴ (SciPy’s pinv): singular values at most rtol · σ_max are treated as zero, with rtol = max(m, n) ε by default.
polar
The polar decomposition A = U P (side = right) or A = P U (left) (SciPy’s polar): U with orthonormal columns (or rows) and P Hermitian positive semi-definite, from the SVD A = W Σ Vᴴ: U = W Vᴴ, and P = V Σ Vᴴ or W Σ Wᴴ. Returns (U, P).
qr
The QR factorisation A P = Q R (SciPy’s qr, with pivoting its pivoting=True): Q has orthonormal columns, R is upper triangular (trapezoidal), and with pivoting the columns are chosen in order of largest remaining norm, so that |R₁₁| ≥ |R₂₂| ≥ …, which reveals the rank.
qz
The generalised complex Schur form of the pencil (A, B) (SciPy’s qz(a, b, output='complex')): unitary Q, Z with Qᴴ A Z = S, Qᴴ B Z = T upper triangular. The generalised eigenvalues are S_ii / T_ii (infinite where T_ii = 0). B is first made triangular by a QR factorisation and A Hessenberg by Givens rotations that keep B triangular; then QZ sweeps with Wilkinson-type shifts chase a bulge down the pencil until the subdiagonal of S vanishes. A zero on the diagonal of T (an infinite eigenvalue) is chased to the bottom and deflated.
qz_real
The real generalised Schur form of the pencil (A, B) (SciPy’s qz(a, b, output='real')): orthogonal Q, Z with Qᵀ A Z = AA quasi upper triangular and Qᵀ B Z = BB upper triangular. B is made triangular by a QR factorisation and A Hessenberg by Givens rotations that keep B triangular; then Moler and Stewart’s implicit double-shift QZ sweeps — the Francis step applied to A B⁻¹ without forming it — chase bulges down the pencil until A is quasi-triangular. A zero on the diagonal of B (an infinite eigenvalue) is chased to the bottom of the active window and deflated. 2 × 2 blocks with real eigenvalues are split.
rq
The RQ factorisation A = R Q (SciPy’s rq, LAPACK’s gerq2): R upper triangular (trapezoidal, aligned to the right), Q with orthonormal rows. Each step reflects a row, from the last upwards, onto its own last allowed column. Returns (R, Q).
schur_complex
The complex Schur form A = Z T Zᴴ: T upper triangular with the eigenvalues of A on its diagonal, Z unitary (SciPy’s linalg.schur(a, output='complex')). Returns (T, Z).
schur_real
The real Schur form A = Z T Zᵀ of a real matrix (SciPy’s schur(a, output='real')): Z orthogonal and T quasi upper triangular, with 1 × 1 blocks for real eigenvalues and 2 × 2 blocks [a b; c a], b c < 0, for each complex conjugate pair a ± i√(−bc). Returns (T, Z).
schur_sorted
The real Schur form A = Z T Zᵀ with the eigenvalues selected by sort in the leading sdim positions (SciPy’s schur(a, output='real', sort=...)). A complex pair is selected when either member is. The selected blocks are moved up one adjacent swap at a time (LAPACK’s dtrsen/dtrexc). Returns (T, Z, sdim).
schur_sorted_complex
The complex Schur form A = Z T Zᴴ with the eigenvalues selected by sort first (SciPy’s schur(a, output='complex', sort=...)), by adjacent swaps of 1 × 1 blocks (LAPACK’s ztrexc). Returns (T, Z, sdim).
solve_banded
Solves A x = b for a banded A with l subdiagonals and u superdiagonals, given in LAPACK band storage ab[u + i − j, j] = A[i, j] (SciPy’s solve_banded((l, u), ab, b)), by Gaussian elimination with partial pivoting within the band (LAPACK’s gbsv): O(n l (l + u)) operations. Row interchanges widen the upper band of U to l + u.
solve_circulant
Solves C x = b for the circulant matrix C with first column c in O(n log n) (SciPy’s solve_circulant): the DFT diagonalises every circulant matrix, C = F⁻¹ diag(F c) F, so x = F⁻¹ (F b / F c). An error is returned when some |F c| is below tol (default n ε max |F c|), where C is numerically singular.
solve_continuous_are
Solves the continuous algebraic Riccati equation Aᴴ X + X A − X B R⁻¹ Bᴴ X + Q = 0 for the stabilising solution X (SciPy’s linalg.solve_continuous_are, without its e and s options), for which A − B R⁻¹ Bᴴ X has all its eigenvalues in the left half-plane.
solve_continuous_lyapunov
Solves the continuous Lyapunov equation A X + X Aᴴ = Q (SciPy’s linalg.solve_continuous_lyapunov). For a stable A and Q = −C Cᴴ, X is the controllability Gramian.
solve_discrete_are
Solves the discrete algebraic Riccati equation Aᴴ X A − X − Aᴴ X B (R + Bᴴ X B)⁻¹ Bᴴ X A + Q = 0 for the stabilising solution (SciPy’s linalg.solve_discrete_are, without its e and s options), for which A − B K with K = (R + Bᴴ X B)⁻¹ Bᴴ X A has all its eigenvalues inside the unit circle.
solve_discrete_lyapunov
Solves the discrete Lyapunov equation A X Aᴴ − X + Q = 0 (SciPy’s linalg.solve_discrete_lyapunov). The solution is unique when no product of two eigenvalues of A (one conjugated) equals 1; for A with spectral radius below 1 and Q = B Bᴴ, X is the controllability Gramian Σ Aᵏ B Bᴴ (Aᴴ)ᵏ.
solve_sylvester
Solves the Sylvester equation A X + X B = Q for X (SciPy’s linalg.solve_sylvester), with A n × n, B m × m and Q n × m. The solution is unique when no eigenvalue of A is the negative of an eigenvalue of B; otherwise LinalgError::Singular is returned.
solve_toeplitz
Solves T x = b for the Toeplitz matrix T with first column c and first row r (default conj(c)) by Levinson recursion in O(n²) operations (SciPy’s solve_toeplitz, after the classic toeplz). The recursion builds the solutions for the leading m × m sections one size at a time, together with those of two auxiliary systems for the reversed sections; it needs every leading principal minor to be non-singular, and is not stable for indefinite or badly conditioned T (then use solve).
solveh_banded
Solves A x = b for a Hermitian positive definite banded A (SciPy’s solveh_banded, LAPACK’s pbsv) by the band Cholesky factorisation A = L Lᴴ, which keeps the bandwidth w: O(n w²) operations. ab has w + 1 rows; with lower = false (SciPy’s default) it holds the upper triangle, ab[w + i − j, j] = A[i, j] for i ≤ j, and with lower = true the lower one, ab[i − j, j] = A[i, j] for i ≥ j.
sqrtm
The principal square root X with X² = A and eigenvalues in the right half-plane (SciPy’s sqrtm), by the Schur method: A = Z T Zᴴ, X = Z R Zᴴ with R² = T triangular. Matrices with eigenvalues on the closed negative real axis have no principal square root; zero eigenvalues of multiplicity above one make it fail.
subspace_angles
The principal angles between the column spaces of A and B (SciPy’s subspace_angles), largest first. With orthonormal bases Q_A, Q_B, the cosines are the singular values of Q_Aᴴ Q_B; angles whose cosine exceeds 1/√2 are taken instead from the sines, the singular values of Q_B − Q_A Q_Aᴴ Q_B, which keeps small angles accurate (Knyazev and Argentati, 2002).
toeplitz
The Toeplitz matrix with first column c and first row r (SciPy’s toeplitz); r[0] is ignored. Without r, the matrix is Hermitian: r = conj(c).

Type Aliases§

ComplexEigen
Complex eigenvalues and eigenvectors (as columns).
ComplexSchur
A complex Schur form (T, Z) with A = Z T Zᴴ.
Result