scirs2-linalg
High-performance linear algebra for Rust, modeled after SciPy/NumPy linalg.
scirs2-linalg provides a comprehensive linear algebra library with SciPy-compatible APIs, pure-Rust BLAS/LAPACK via OxiBLAS (no C or Fortran dependencies), SIMD acceleration, randomized methods, tensor decompositions, and iterative solvers suitable for large-scale scientific computing and machine learning.
Installation
[]
= "0.3.4"
With optional acceleration:
[]
= { = "0.3.4", = ["simd", "parallel"] }
Features (v0.3.4)
Core Decompositions
- LU (with partial/rook/complete pivoting), QR, SVD, Cholesky, LDL^T
- Eigendecomposition:
eig,eigh(symmetric), generalized eigenproblem - Schur decomposition (real and complex), QZ decomposition
- Polar decomposition, complete orthogonal decomposition
- Tall-and-Skinny QR (TSQR), LQ decomposition for wide matrices
- Randomized SVD (Halko, Martinsson, Tropp), Nystrom approximation
Iterative Solvers
- GMRES (restarted, deflated, recycled/GCRO-DR style)
- Preconditioned Conjugate Gradient (PCG)
- BiCGStab (stabilized bi-conjugate gradient)
- MINRES, SYMMLQ
- Arnoldi iteration, Lanczos iteration (with thick restarts)
- SOR, SSOR, Gauss-Seidel, Jacobi
Matrix Functions
- Matrix exponential
expm(Pade approximant + scaling/squaring) - Matrix logarithm
logm(inverse scaling/squaring) - Matrix square root
sqrtm(Schur-based) - Matrix sign function
signm - Matrix trigonometric functions (sin, cos, tan, sinh, cosh via Schur)
- Polar decomposition
- Matrix polynomial evaluation
Control Theory
- Algebraic Riccati equations (CARE, DARE): Newton iteration, Hamiltonian Schur
- Lyapunov equations (continuous and discrete)
- Sylvester equations (Bartels-Stewart, Hessenberg-Schur)
- Controllability and observability Gramians
Tensor Operations
- CP decomposition (Canonical Polyadic via ALS)
- Tucker decomposition (Higher-Order SVD / HOOI)
- Tensor contractions and mode-n products
- Einstein summation (
einsum) - Hierarchical Tucker (HT) decomposition
- Tensor-train format basics
Randomized Linear Algebra
- Randomized SVD with power iteration and oversampling
- Nystrom extension for kernel matrices
- Randomized eigensolvers (subspace iteration)
- Sketching: CountSketch, Gaussian sketch, SRHT
Structured and Specialized Matrices
- Toeplitz, Hankel, Circulant (FFT-based O(n log n) matvec)
- Cauchy matrix, companion matrix
- Banded matrices (tridiagonal, pentadiagonal), block tridiagonal
- Block diagonal, block sparse row
- Indefinite systems (symmetric indefinite factorization)
Matrix Completion and Low-Rank
- Nuclear norm minimization via alternating projections
- Soft-impute algorithm for matrix completion
- CUR decomposition (column-row factorization)
- Sparse-dense hybrid operations
Numerical Analysis
- Perturbation theory: condition number bounds, backward error analysis
- Numerical range (field of values) computation
- Matrix pencil problems (regular and singular pencils)
- Error analysis for linear systems and least squares
ML / AI Support
- Scaled dot-product attention, multi-head attention
- Flash attention (memory-efficient)
- Sparse attention patterns
- Positional encodings: RoPE, ALiBi
- Quantization-aware matrix multiply (4-bit, 8-bit, 16-bit)
- Mixed-precision operations with iterative refinement
- Batch matrix operations for mini-batch processing
Usage Examples
Basic operations
use ;
use array;
let a = array!;
let b = array!;
let d = det?;
let a_inv = inv?;
let x = solve?;
let = svd?;
let = eigh?;
Iterative solvers
use ;
// GMRES for a general non-symmetric system
let x = gmres?;
// PCG for symmetric positive definite
let x = pcg?;
// BiCGStab for non-symmetric
let x = bicgstab?;
Matrix functions
use ;
let exp_a = expm?;
let log_a = logm?;
let sqrt_a = sqrtm?;
Tensor decompositions
use ;
// CP decomposition with 5 components, 200 ALS iterations
let cp = cp_als?;
// Tucker decomposition with rank [3, 3, 3]
let tucker = tucker_hooi?;
Control theory
use ;
// Continuous algebraic Riccati equation: A^T X + X A - X B R^{-1} B^T X + Q = 0
let x = solve_care?;
// Discrete algebraic Riccati equation
let x = solve_dare?;
// Lyapunov equation: A X + X A^T + Q = 0
let x = solve_lyapunov?;
Feature Flags
| Feature | Description |
|---|---|
simd |
SIMD-accelerated kernels (AVX/AVX2/AVX-512/NEON) |
parallel |
Multi-threaded operations via Rayon |
gpu |
GPU acceleration (requires scirs2-core gpu feature) |
linalg |
Enable OxiBLAS pure-Rust BLAS/LAPACK backend |
serde |
Serialization for matrix types |
Links
License
Licensed under the Apache License 2.0. See LICENSE for details.