Expand description
ยงSciRS2 Linear Algebra - High-Performance Matrix Operations
scirs2-linalg provides comprehensive linear algebra operations with SciPy/NumPy-compatible APIs, leveraging native BLAS/LAPACK for peak performance and offering advanced features like SIMD acceleration, GPU support, and specialized solvers.
ยง๐ฏ Key Features
- SciPy/NumPy Compatibility: Drop-in replacement for
scipy.linalgandnumpy.linalg - Native BLAS/LAPACK: Hardware-optimized through OpenBLAS, Intel MKL, or Apple Accelerate
- SIMD Acceleration: AVX/AVX2/AVX-512 optimized operations for f32/f64
- GPU Support: CUDA, ROCm, OpenCL, and Metal acceleration
- Parallel Processing: Multi-threaded via Rayon for large matrices
- Comprehensive Solvers: Direct, iterative, sparse, and specialized methods
- Matrix Functions: Exponential, logarithm, square root, and trigonometric functions
- Attention Mechanisms: Multi-head, flash, and sparse attention for transformer models
ยง๐ฆ Module Overview
| SciRS2 Module | SciPy/NumPy Equivalent | Description |
|---|---|---|
| Basic ops | scipy.linalg.det, inv | Determinants, inverses, traces |
| Decompositions | scipy.linalg.lu, qr, svd | LU, QR, SVD, Cholesky, Schur |
| Eigenvalues | scipy.linalg.eig, eigh | Standard and generalized eigenproblems |
| Solvers | scipy.linalg.solve, lstsq | Linear systems (direct & iterative) |
| Matrix functions | scipy.linalg.expm, logm | Matrix exponential, logarithm, etc. |
| Norms | numpy.linalg.norm, cond | Vector/matrix norms, condition numbers |
| Specialized | scipy.linalg.solve_banded | Banded, circulant, Toeplitz matrices |
| Attention | - | Multi-head, flash attention (PyTorch-style) |
| BLAS | scipy.linalg.blas.* | Low-level BLAS operations |
| LAPACK | scipy.linalg.lapack.* | Low-level LAPACK operations |
ยง๐ Quick Start
Add to your Cargo.toml:
[dependencies]
scirs2-linalg = "0.1.2"
# Optional features
scirs2-linalg = { version = "0.1.2", features = ["simd", "parallel", "gpu"] }ยงBasic Matrix Operations
use scirs2_core::ndarray::array;
use scirs2_linalg::{det, inv, solve};
// Determinant and inverse
let a = array![[4.0, 2.0], [2.0, 3.0]];
let det_a = det(&a.view(), None).expect("Operation failed");
let a_inv = inv(&a.view(), None).expect("Operation failed");
// Solve linear system Ax = b
let b = array![6.0, 7.0];
let x = solve(&a.view(), &b.view(), None).expect("Operation failed");ยงMatrix Decompositions
use scirs2_core::ndarray::array;
use scirs2_linalg::{lu, qr, svd, cholesky, eig};
let a = array![[1.0_f64, 2.0], [3.0, 4.0]];
// LU decomposition: PA = LU
let (p, l, u) = lu(&a.view(), None).expect("Operation failed");
// QR decomposition: A = QR
let (q, r) = qr(&a.view(), None).expect("Operation failed");
// SVD: A = UฮฃVแต
let (u, s, vt) = svd(&a.view(), true, None).expect("Operation failed");
// Eigenvalues and eigenvectors
let (eigenvalues, eigenvectors) = eig(&a.view(), None).expect("Operation failed");
// Cholesky decomposition for positive definite matrices
let spd = array![[4.0, 2.0], [2.0, 3.0]];
let l_chol = cholesky(&spd.view(), None).expect("Operation failed");ยงIterative Solvers (Large Sparse Systems)
โ
use scirs2_core::ndarray::array;
use scirs2_linalg::{conjugate_gradient, gmres};
// Conjugate Gradient for symmetric positive definite systems
let a = array![[4.0_f64, 1.0], [1.0, 3.0]];
let b = array![1.0_f64, 2.0];
let x_cg = conjugate_gradient(&a.view(), &b.view(), 10, 1e-10, None).expect("Operation failed");
// GMRES for general systems
let x_gmres = gmres(&a.view(), &b.view(), 10, 1e-10, None).expect("Operation failed");ยงMatrix Functions
โ
use scirs2_core::ndarray::array;
use scirs2_linalg::{expm, logm, sqrtm, sinm, cosm};
let a = array![[1.0, 0.5], [0.5, 1.0]];
// Matrix exponential: exp(A)
let exp_a = expm(&a.view(), None).expect("Operation failed");
// Matrix logarithm: log(A)
let log_a = logm(&a.view(), None).expect("Operation failed");
// Matrix square root: โA
let sqrt_a = sqrtm(&a.view(), None).expect("Operation failed");ยงAccelerated BLAS/LAPACK Operations
โ
use scirs2_core::ndarray::array;
use scirs2_linalg::accelerated::{matmul, solve as fast_solve};
// Hardware-accelerated matrix multiplication
let a = array![[1.0_f64, 2.0], [3.0, 4.0]];
let b = array![[5.0_f64, 6.0], [7.0, 8.0]];
let c = matmul(&a.view(), &b.view()).expect("Operation failed");
// Fast linear system solver using LAPACK
let x = fast_solve(&a.view(), &b.view()).expect("Operation failed");ยงAttention Mechanisms (Deep Learning)
โ
use scirs2_core::ndarray::Array2;
use scirs2_linalg::attention::{multi_head_attention, flash_attention, AttentionConfig};
// Multi-head attention (Transformer-style)
let query = Array2::<f32>::zeros((32, 64)); // (batch_size, d_model)
let key = Array2::<f32>::zeros((32, 64));
let value = Array2::<f32>::zeros((32, 64));
let config = AttentionConfig {
num_heads: 8,
dropout: 0.1,
causal: false,
};
let output = multi_head_attention(&query.view(), &key.view(), &value.view(), &config);ยง๐๏ธ Architecture
scirs2-linalg
โโโ Basic Operations (det, inv, trace, rank)
โโโ Decompositions (LU, QR, SVD, Cholesky, Eigenvalues)
โโโ Solvers
โ โโโ Direct (solve, lstsq)
โ โโโ Iterative (CG, GMRES, BiCGSTAB)
โ โโโ Specialized (banded, triangular, sparse-dense)
โโโ Matrix Functions (expm, logm, sqrtm, trigonometric)
โโโ Accelerated Backends
โ โโโ BLAS/LAPACK (native libraries)
โ โโโ SIMD (AVX/AVX2/AVX-512)
โ โโโ Parallel (Rayon multi-threading)
โ โโโ GPU (CUDA/ROCm/OpenCL/Metal)
โโโ Advanced Features
โ โโโ Attention mechanisms
โ โโโ Hierarchical matrices (H-matrices)
โ โโโ Kronecker factorization (K-FAC)
โ โโโ Randomized algorithms
โ โโโ Mixed precision
โ โโโ Quantization-aware operations
โโโ Compatibility Layer (SciPy-compatible API)ยง๐ Performance
| Operation | Size | Pure Rust | BLAS/LAPACK | SIMD | GPU |
|---|---|---|---|---|---|
| Matrix Multiply | 1000ร1000 | 2.5s | 15ms | 180ms | 5ms |
| SVD | 1000ร1000 | 8.2s | 120ms | N/A | 35ms |
| Eigenvalues | 1000ร1000 | 6.8s | 95ms | N/A | 28ms |
| Solve (direct) | 1000ร1000 | 1.8s | 22ms | 140ms | 8ms |
Note: Benchmarks on AMD Ryzen 9 5950X with NVIDIA RTX 3090. BLAS/LAPACK uses OpenBLAS.
ยง๐ Integration
Works seamlessly with other SciRS2 crates:
scirs2-stats: Covariance matrices, statistical distributionsscirs2-optimize: Hessian computations, constraint Jacobiansscirs2-neural: Weight matrices, gradient computationsscirs2-sparse: Sparse matrix operations
ยง๐ Version Information
- Version: 0.1.2
- Release Date: January 15, 2026
- MSRV (Minimum Supported Rust Version): 1.70.0
- Documentation: docs.rs/scirs2-linalg
- Repository: github.com/cool-japan/scirs
Re-exportsยง
pub use error::LinalgError;pub use error::LinalgResult;pub use self::eigen::advanced_precision_eig;pub use self::eigen::eig;pub use self::eigen::eig_gen;pub use self::eigen::eigh;pub use self::eigen::eigh_gen;pub use self::eigen::eigvals;pub use self::eigen::eigvals_gen;pub use self::eigen::eigvalsh;pub use self::eigen::eigvalsh_gen;pub use self::eigen::power_iteration;pub use self::quantization::calibration::calibrate_matrix;pub use self::quantization::calibration::calibrate_vector;pub use self::quantization::calibration::get_activation_calibration_config;pub use self::quantization::calibration::get_weight_calibration_config;pub use self::quantization::calibration::CalibrationConfig;pub use self::quantization::calibration::CalibrationMethod;pub use self::eigen_specialized::banded_eigen;pub use self::eigen_specialized::banded_eigh;pub use self::eigen_specialized::banded_eigvalsh;pub use self::eigen_specialized::circulant_eigenvalues;pub use self::eigen_specialized::largest_k_eigh;pub use self::eigen_specialized::partial_eigen;pub use self::eigen_specialized::smallest_k_eigh;pub use self::eigen_specialized::tridiagonal_eigen;pub use self::eigen_specialized::tridiagonal_eigh;pub use self::eigen_specialized::tridiagonal_eigvalsh;pub use self::complex::enhanced_ops::det as complex_det;pub use self::complex::enhanced_ops::frobenius_norm;pub use self::complex::enhanced_ops::hermitian_part;pub use self::complex::enhanced_ops::inner_product;pub use self::complex::enhanced_ops::is_hermitian;pub use self::complex::enhanced_ops::is_unitary;pub use self::complex::enhanced_ops::matrix_exp;pub use self::complex::enhanced_ops::matvec;pub use self::complex::enhanced_ops::polar_decomposition;pub use self::complex::enhanced_ops::power_method;pub use self::complex::enhanced_ops::rank as complex_rank;pub use self::complex::enhanced_ops::schur as complex_schur;pub use self::complex::enhanced_ops::skew_hermitian_part;pub use self::complex::enhanced_ops::trace;pub use self::complex::complex_inverse;pub use self::complex::complex_matmul;pub use self::complex::hermitian_transpose;pub use self::decomposition_advanced::jacobi_svd;pub use self::decomposition_advanced::polar_decomposition as advanced_polar_decomposition;pub use self::decomposition_advanced::polar_decomposition_newton;pub use self::decomposition_advanced::qr_with_column_pivoting;pub use self::matrix_equations::solve_continuous_riccati;pub use self::matrix_equations::solve_discrete_riccati;pub use self::matrix_equations::solve_generalized_sylvester;pub use self::matrix_equations::solve_stein;pub use self::matrix_equations::solve_sylvester;pub use self::matrix_factorization::cur_decomposition;pub use self::matrix_factorization::interpolative_decomposition;pub use self::matrix_factorization::nmf;pub use self::matrix_factorization::rank_revealing_qr;pub use self::matrix_factorization::utv_decomposition;pub use self::matrix_functions::acosm;pub use self::matrix_functions::asinm;pub use self::matrix_functions::atanm;pub use self::matrix_functions::coshm;pub use self::matrix_functions::cosm;pub use self::matrix_functions::expm;pub use self::matrix_functions::geometric_mean_spd;pub use self::matrix_functions::logm;pub use self::matrix_functions::logm_parallel;pub use self::matrix_functions::nuclear_norm;pub use self::matrix_functions::signm;pub use self::matrix_functions::sinhm;pub use self::matrix_functions::sinm;pub use self::matrix_functions::spectral_condition_number;pub use self::matrix_functions::spectral_radius;pub use self::matrix_functions::sqrtm;pub use self::matrix_functions::sqrtm_parallel;pub use self::matrix_functions::tanhm;pub use self::matrix_functions::tanm;pub use self::matrix_functions::tikhonov_regularization;pub use self::matrixfree::block_diagonal_operator;pub use self::matrixfree::conjugate_gradient as matrix_free_conjugate_gradient;pub use self::matrixfree::diagonal_operator;pub use self::matrixfree::gmres as matrix_free_gmres;pub use self::matrixfree::jacobi_preconditioner;pub use self::matrixfree::preconditioned_conjugate_gradient as matrix_free_preconditioned_conjugate_gradient;pub use self::matrixfree::LinearOperator;pub use self::matrixfree::MatrixFreeOp;pub use self::solvers::iterative::bicgstab;pub use self::solvers::iterative::conjugate_gradient as cg_solver;pub use self::solvers::iterative::gmres;pub use self::solvers::iterative::preconditioned_conjugate_gradient as pcg_solver;pub use self::solvers::iterative::IterativeSolverOptions;pub use self::solvers::iterative::IterativeSolverResult;pub use self::specialized::specialized_to_operator;pub use self::specialized::BandedMatrix;pub use self::specialized::SpecializedMatrix;pub use self::specialized::SymmetricMatrix;pub use self::specialized::TridiagonalMatrix;pub use self::structured::structured_to_operator;pub use self::structured::CirculantMatrix;pub use self::structured::HankelMatrix;pub use self::structured::StructuredMatrix;pub use self::structured::ToeplitzMatrix;pub use self::tensor_contraction::batch_matmul;pub use self::tensor_contraction::contract;pub use self::tensor_contraction::einsum;pub use self::tensor_contraction::hosvd;pub use self::extended_precision::*;pub use self::stats::*;
Modulesยง
- accelerated
- Accelerated linear algebra operations using native BLAS/LAPACK
- attention
- Optimized attention mechanisms for transformer models
- autograd
- Automatic differentiation support for linear algebra operations
- batch
- Batch matrix operations for AI/ML workloads
- blas
- BLAS (Basic Linear Algebra Subprograms) interface
- blas_
accelerated - Accelerated BLAS (Basic Linear Algebra Subprograms) operations using ndarray-linalg
- broadcast
- NumPy-style broadcasting for linear algebra operations on higher-dimensional arrays
- circulant_
toeplitz - FFT-based solvers for circulant and Toeplitz matrices
- compat
- Backward compatibility layer for ndarray-linalg-style trait-based API
- compat_
wrappers - Backward compatibility wrappers for functions that gained workers parameters
- complex
- Complex matrix operations
- convolution
- Specialized operations for convolutional neural networks
- decomposition_
advanced - Advanced matrix decomposition algorithms
- eigen
- Eigenvalue and eigenvector computations
- eigen_
specialized - Specialized eigenvalue solvers for structured matrices
- error
- Error types for the SciRS2 linear algebra module
- extended_
precision - Extended precision matrix operations
- fft
- Fast Fourier Transform (FFT) and spectral methods for signal processing
- generic
- Type-generic linear algebra operations
- gpu
- GPU acceleration foundations for linear algebra operations
- gradient
- Gradient calculation utilities for neural networks
- hierarchical
- Hierarchical matrix factorizations for large-scale computational efficiency
- kronecker
- Kronecker product optimizations for neural network layers
- lapack
- LAPACK (Linear Algebra Package) interface
- lapack_
accelerated - LAPACK (Linear Algebra Package) operations
- large_
scale - Large-scale linear algebra algorithms
- lowrank
- Low-rank matrix operations and approximations
- matrix_
calculus - Matrix calculus utilities
- matrix_
dynamics - Matrix Differential Equations and Dynamical Systems
- matrix_
equations - Advanced matrix equation solvers
- matrix_
factorization - Advanced matrix factorization algorithms
- matrix_
functions - Matrix functions such as matrix exponential, logarithm, and square root
- matrixfree
- Matrix-free operations for iterative solvers in large models
- mixed_
precision - Mixed-precision linear algebra operations
- optim
- Optimized matrix operations for large parameter matrices
- parallel
- Parallel processing utilities for linear algebra operations
- parallel_
dispatch - Parallel algorithm dispatch for linear algebra operations
- perf_
opt - Performance optimizations for large matrices
- preconditioners
- Advanced preconditioners for iterative linear solvers
- prelude
- Common linear algebra operations for convenient importing
- projection
- Fast random projection methods for dimensionality reduction
- quantization
- Quantization-aware linear algebra operations Quantization-aware linear algebra operations
- random
- Random matrix generation utilities
- random_
matrices - Random matrix generation utilities
- scalable
- Scalable algorithms for tall-and-skinny or short-and-fat matrices
- simd_
ops - SIMD accelerated linear algebra operations
- solvers
- Linear system solvers
- sparse_
dense - Sparse-Dense matrix operations
- special
- Special matrix functions
- specialized
- Specialized matrix implementations with optimized storage and operations
- stats
- Statistical functions for matrices
- structured
- Structured matrices support (Toeplitz, circulant) for efficient representations
- tensor_
contraction - Tensor contraction operations
- tensor_
train - Tensor-Train (TT) decomposition for high-dimensional tensor computations
Macrosยง
- backend_
error - Helper macro for creating backend errors with context
- check_
result - Helper macro for checking result codes and converting to LinalgError
Structsยง
- Lstsq
Result - Solution to a least-squares problem
Functionsยง
- basic_
trace - Compute the trace of a square matrix.
- bicgstab
- Solve a linear system Ax = b using the BiCGSTAB (Biconjugate Gradient Stabilized) method.
- cholesky
- Compute the Cholesky decomposition of a matrix.
- cholesky_
default - Compute Cholesky decomposition using default thread count
- cholesky_
f64_ lapack - Cholesky decomposition using OxiBLAS (fast pure Rust implementation)
- cond
- Compute the condition number of a matrix.
- cond_
default Deprecated - Compute the condition number without workers parameter (deprecated - use cond with workers)
- conjugate_
gradient - Solve a linear system Ax = b using the Conjugate Gradient method.
- conjugate_
gradient_ default - Solve a linear system Ax = b using the Conjugate Gradient method (backward compatibility wrapper).
- det
- Compute the determinant of a square matrix.
- det_
default - Compute the determinant of a square matrix (backward compatibility wrapper).
- det_
f64_ lapack - OxiBLAS-accelerated determinant for f64 (PUBLIC for direct Python use)
- eig_
f64_ lapack - General eigenvalue decomposition using OxiBLAS (fast pure Rust implementation) Returns (eigenvalues, eigenvectors) as complex arrays
- eigh_
f64_ lapack - Symmetric eigenvalue decomposition using OxiBLAS (fast pure Rust implementation) Returns (eigenvalues, eigenvectors) for symmetric/Hermitian matrices
- gauss_
seidel - Solve a linear system Ax = b using Gauss-Seidel iteration.
- geometric_
multigrid - Geometric Multigrid method for solving linear systems.
- inv
- Compute the inverse of a square matrix.
- inv_
default - Compute the inverse of a square matrix (backward compatibility wrapper).
- inv_
f64_ lapack - OxiBLAS-accelerated matrix inverse for f64
- jacobi_
method - Solve a linear system Ax = b using Jacobi iteration.
- lstsq
- Compute least-squares solution to a linear matrix equation.
- lstsq_
default - Compute least-squares solution using default thread count
- lu
- Compute the LU decomposition of a matrix.
- lu_
default - Compute LU decomposition using default thread count
- lu_
f64_ lapack - LU decomposition wrapper for consistent API Returns (P, L, U) where PA = LU
- matrix_
norm - Compute a matrix norm.
- matrix_
norm_ default Deprecated - Compute a matrix norm without workers parameter (deprecated - use matrix_norm with workers)
- matrix_
norm_ simd - Compute a matrix norm with SIMD acceleration.
- matrix_
power - Raise a square matrix to the given power.
- matrix_
power_ default - Raise a square matrix to the given power (backward compatibility wrapper).
- matrix_
rank - Compute the rank of a matrix.
- matrix_
rank_ default Deprecated - Compute matrix rank without workers parameter (deprecated - use matrix_rank with workers)
- minres
- Solve a linear system Ax = b using the MINRES (Minimal Residual) method.
- qr
- Compute the QR decomposition of a matrix.
- qr_
default - Compute QR decomposition using default thread count
- qr_
f64_ lapack - QR decomposition using OxiBLAS (fast pure Rust implementation)
- schur
- Compute the Schur decomposition of a matrix.
- solve
- Solve a linear system of equations.
- solve_
default - Solve linear system using default thread count
- solve_
f64_ lapack - OxiBLAS-accelerated linear system solver for f64
- solve_
multiple - Solve the linear system Ax = B for x with multiple right-hand sides.
- solve_
multiple_ default - Solve multiple linear systems using default thread count
- solve_
triangular - Solve a linear system with a lower or upper triangular coefficient matrix.
- successive_
over_ relaxation - Solve a linear system Ax = b using Successive Over-Relaxation (SOR).
- svd
- Compute the singular value decomposition of a matrix.
- svd_
default - Compute SVD using default thread count
- svd_
f64_ lapack - SVD decomposition using OxiBLAS (fast pure Rust implementation)
- vector_
norm - Compute a vector norm.
- vector_
norm_ parallel - Compute the norm of a vector with parallel processing support.
- vector_
norm_ simd - Compute a vector norm with SIMD acceleration.