algebra-sparse
Efficient sparse linear algebra library built on top of [nalgebra], providing high-performance sparse matrix and vector operations for scientific computing and physics simulations.
Features
- Compressed Sparse Matrix Storage: Support for both CSR (Compressed Sparse Row) and CSC (Compressed Sparse Column) formats
- Block Diagonal Matrices: Optimized storage and operations for block diagonal structures common in physical simulations
- Matrix Operations: Comprehensive sparse-sparse, sparse-dense, and sparse-vector multiplications
- Zero Threshold Support: Automatic filtering of near-zero values for memory efficiency
- View-based API: Efficient borrowing and slicing without allocation
- nalgebra Integration: Seamless conversion between sparse and dense representations
- Matrix Sets: Efficient storage of multiple sparse matrices in a single data structure
Quick Start
Add this to your Cargo.toml:
[]
= "*"
Basic Usage
use CsrMatrix;
use DMatrix;
// Create a dense matrix and convert to sparse
let dense = from_row_slice;
// Convert to CSR format (automatically filters zeros)
let sparse = from_dense;
// Perform sparse matrix-vector multiplication
let vector = from_vec;
let result = sparse.as_view * vector;
println!;
Block Diagonal Matrices
use DiagonalBlockMatrix;
// Create a block diagonal matrix with 2x2 and 3x3 blocks
// Blocks are stored in column-major order
let block_values = vec!;
let block_sizes = ;
let matrix = from_block_values;
println!;
Building Sparse Matrices
use CsrMatrix;
let mut matrix = new; // 4 columns
// Add first row
let mut builder = matrix.new_row_builder;
builder.push;
builder.push;
// Row is automatically added when builder is dropped
// Add second row
let mut builder = matrix.new_row_builder;
builder.push;
builder.push;
println!;
Matrix Formats
CSR (Compressed Sparse Row)
- Optimized for row-wise operations
- Efficient for matrix-vector products
- Fast row access and iteration
CSC (Compressed Sparse Column)
- Optimized for column-wise operations
- Efficient for column-based computations
- Fast column access and iteration
Block Diagonal
- Optimized for block diagonal structures
- Common in physical simulations and FEM
- Efficient storage for structured sparsity
Performance Considerations
- View-based Operations: No unnecessary allocations for read-only access
- Efficient Algorithms: Optimized sparse-sparse and sparse-dense operations
- Memory Layout: Designed for cache-friendly access patterns
Examples
Sparse Matrix Multiplication
use CsrMatrix;
use DMatrix;
let a_dense = from_row_slice;
let b_dense = from_row_slice;
let a = from_dense;
let b = from_dense;
let result = a.as_view * b.transpose;
println!;
println!;
Matrix Transposition
use CsrMatrix;
use DMatrix;
let dense = from_row_slice;
let csr = from_dense;
// Transpose CSR to CSC
let csc = csr.as_view.transpose;
println!;
println!;
Documentation
For comprehensive API documentation, visit docs.rs/algebra-sparse.