scirs2-linalg
🚀 Production-Ready Linear Algebra for Rust
v0.1.0-rc.1 - SciRS2 POLICY & Enhanced Performance
scirs2-linalg
delivers comprehensive linear algebra functionality comparable to NumPy/SciPy's linalg module, providing a robust mathematical foundation for scientific computing, machine learning, and data analysis in Rust. Following the new SciRS2 POLICY, this release ensures ecosystem consistency through scirs2-core abstractions. With 549 passing tests and comprehensive feature coverage, this library is production-ready for demanding applications.
Features
Core Linear Algebra
- Basic Operations: Determinants, inverses, matrix multiplication, matrix powers
- Decompositions: LU, QR, SVD, Cholesky, Eigendecomposition, Schur, Polar
- Solvers: Direct methods, least squares, triangular systems
- Eigenvalue Problems: Standard and specialized eigenvalue/eigenvector computations
- Matrix Functions: Matrix exponential, logarithm, square root
- Norms and Condition Numbers: Various matrix and vector norms
Advanced Capabilities
- Iterative Solvers: Conjugate gradient, GMRES, Jacobi, Gauss-Seidel, multigrid
- Structured Matrices: Efficient operations on Toeplitz, Hankel, circulant matrices
- Specialized Matrices: Optimized algorithms for tridiagonal, banded, symmetric matrices
- BLAS/LAPACK Integration: High-performance native library support when available
Machine Learning & AI Support
- Attention Mechanisms: Scaled dot-product, multi-head, flash attention
- Batch Operations: Vectorized operations for mini-batch processing
- Gradient Computations: Automatic differentiation support for matrix operations
- Low-Rank Approximations: SVD-based dimensionality reduction
- Tensor Operations: Einstein summation, tensor contractions, mode products
- Memory-Efficient Algorithms: Flash attention, linear attention variants
- Quantization-Aware Operations: 4-bit, 8-bit and 16-bit precision support with advanced calibration and numerical stability analysis
- Matrix-free operations for quantized tensors
- Fusion operations for consecutive quantized operations
- Specialized solvers for quantized matrices
- Mixed Precision: Operations across different numeric types
- Sparse-Dense Operations: Efficient handling of sparse matrices
Performance
SciRS2 is designed for high performance with multiple optimization strategies:
- BLAS/LAPACK Integration: Native acceleration through optimized libraries
- SIMD Vectorization: Hand-tuned SIMD kernels for critical operations
- Memory Efficiency: Cache-friendly algorithms and reduced allocations
- Parallel Processing: Multi-core acceleration for large matrices
- SciPy API Compatibility: Zero-overhead wrappers maintaining familiar interfaces
📊 Performance Guides: See docs/PERFORMANCE_GUIDE.md for detailed benchmarks and docs/OPTIMIZATION_GUIDELINES.md for practical optimization strategies.
Performance Highlights
Operation | Small Matrices | Medium Matrices | Large Matrices |
---|---|---|---|
Basic Ops | 0.1-1 μs | 10-100 μs | 1-10 ms |
Decompositions | 1-10 μs | 100 μs-1 ms | 10-100 ms |
Eigenvalues | 5-50 μs | 500 μs-5 ms | 50-500 ms |
For detailed performance analysis, benchmarking guides, and optimization tips:
- Performance Guide - Comprehensive performance analysis and best practices
- Benchmarking Guide - Instructions for running and creating custom benchmarks
Installation
Add scirs2-linalg to your Cargo.toml:
[]
= "0.1.0-rc.1"
= "0.16.1"
For accelerated performance with native BLAS/LAPACK:
[]
= { = "0.1.0-rc.1", = ["openblas"] }
# Or use Intel MKL:
# scirs2-linalg = { version = "0.1.0-rc.1", features = ["mkl"] }
Quick Start
Basic Usage
use array;
use *;
SciPy Compatibility
For users migrating from Python/SciPy:
use compat;
// Use familiar SciPy-style function signatures
let det = det?;
let inv = inv?;
let = svd?;
Matrix Decompositions
use array;
use ;
// Create a test matrix
let a = array!;
// LU decomposition
let = lu?;
assert_eq!; // P·A = L·U
// QR decomposition
let = qr?;
assert_eq!; // A = Q·R
// Singular Value Decomposition
let = svd?;
// Note: reconstruction requires proper diagonal matrix construction
// Cholesky decomposition (for positive definite matrices)
let l = cholesky?;
assert_eq!; // A = L·L^T
Iterative Solvers
use ;
// Solve using conjugate gradient (for symmetric positive definite)
let x = conjugate_gradient?;
// Solve using GMRES (for general matrices)
let x = gmres?;
Advanced Matrix Operations
use ;
use ;
use ;
// Matrix functions
let exp_a = expm?; // Matrix exponential
let log_a = logm?; // Matrix logarithm
let sqrt_a = sqrtm?; // Matrix square root
// Specialized matrices for efficiency
let tridiag = from_diagonals;
let x = tridiag.solve?;
// Structured matrices
let toeplitz = from_column_and_row;
let result = toeplitz.matvec?;
Machine Learning Operations
use Array3;
use ;
use batch_multi_head_attention;
// Attention mechanism for transformers
let batch_size = 2;
let seq_len = 4;
let d_model = 64;
// Create query, key, value tensors
let query = from_shape_fn;
let key = from_shape_fn;
let value = from_shape_fn;
// Scaled dot-product attention
let scale = 1.0 / .sqrt;
let output = scaled_dot_product_attention?;
// Multi-head attention
let num_heads = 8;
let output = multi_head_attention?;
Quantization and Model Compression
use ;
// Create a neural network weight matrix
let weights = from_shape_fn;
let activations = from_shape_fn;
// Calibrate quantization parameters using advanced methods
let weight_config = CalibrationConfig ;
// Dynamic calibration for activations that change over time
let activation_config = CalibrationConfig ;
// Calibrate and quantize weights (to 8-bit)
let weight_params = calibrate_matrix?;
let = quantize_matrix;
// Calibrate and quantize activations
let activation_params = calibrate_matrix?;
let = quantize_matrix;
// Perform matrix multiplication with quantized matrices
let result = quantized_matmul?;
// Calculate quantization error
let reference = activations.dot;
let rel_error = .mapv.sum /
reference.mapv.sum;
println!;
println!;
Performance Considerations
Backend Selection
The library supports multiple BLAS/LAPACK backends:
# OpenBLAS (default, good general performance)
= { = "0.1.0-rc.1", = ["openblas"] }
# Intel MKL (best for Intel CPUs)
= { = "0.1.0-rc.1", = ["mkl"] }
# Netlib (reference implementation)
= { = "0.1.0-rc.1", = ["netlib"] }
Optimization Features
- SIMD Acceleration: Enable with
features = ["simd"]
- Parallel Operations: Built-in Rayon support for large matrices
- Memory-Efficient Algorithms: Automatic selection based on matrix size
- Cache-Friendly Implementations: Blocked algorithms for better cache usage
📈 Production Performance Benchmarks
Production-validated performance (1000×1000 matrices, optimized builds):
Operation | Pure Rust | SIMD | OpenBLAS | Intel MKL | Status |
---|---|---|---|---|---|
Matrix Multiply | 245ms | 89ms | 42ms | 38ms | ✅ Production |
LU Decomposition | 185ms | N/A | 78ms | 71ms | ✅ Production |
SVD | 892ms | N/A | 340ms | 298ms | ✅ Production |
Eigenvalues | 1.2s | N/A | 445ms | 412ms | ✅ Production |
Performance is competitive with industry-standard libraries and ready for production deployment.
Error Handling
The library uses a comprehensive error system:
use ;
match inv
🎯 Production Readiness
✅ Comprehensive Implementation: All major linear algebra operations implemented and tested ✅ Performance Optimized: Native BLAS/LAPACK integration with SIMD acceleration ✅ API Stable: Backward compatible with comprehensive error handling ✅ Test Coverage: 549 tests with 100% pass rate ensuring reliability ✅ Documentation: Complete API documentation with examples and guides
🚀 Deployment Ready: This library is suitable for production use in scientific computing, machine learning frameworks, and high-performance numerical applications.
For detailed feature status, see TODO.md.
Contributing
Contributions are welcome! Please see our contributing guidelines.
Current priorities for v0.1.0 stable:
- Performance benchmarking and optimization
- Additional documentation and examples
- Integration testing with downstream applications
- Community feedback and API refinement
Future enhancements (post-v0.1.0):
- GPU acceleration support
- Additional specialized algorithms
- Distributed computing integration
License
This project is dual-licensed under:
You can choose to use either license. See the LICENSE file for details.
Acknowledgments
This library is inspired by NumPy and SciPy's excellent linear algebra implementations. We aim to bring similar functionality to the Rust ecosystem while leveraging Rust's performance and safety guarantees.