tenrso-sparse
Sparse tensor formats, operations, linear solvers, graph algorithms, and more for TenRSo.
Version: 0.1.0-rc.1 | Status: RC.1 — 426 tests passing (3 ignored), 100% pass rate | Last Updated: 2026-03-06
Overview
tenrso-sparse provides efficient sparse tensor storage formats, operations, iterative linear solvers, eigensolvers, and graph algorithms:
Sparse Formats
- COO - Coordinate format (triplets); for construction and format conversion
- CSR - Compressed Sparse Row; optimized for row-wise access and SpMV
- CSC - Compressed Sparse Column; optimized for column-wise access
- BCSR - Block Compressed Sparse Row; for block-structured sparsity
- ELL - ELLPACK format; suited for SIMD/GPU execution
- DIA - Diagonal format; for banded/diagonal-dominant matrices
- CSF - Compressed Sparse Fiber; true N-D sparsity (feature-gated:
csf) - HiCOO - Hierarchical COO; cache-friendly N-D format (feature-gated:
csf)
Operations (36 total)
- Element-wise: add, sub, mul, div (sparse-sparse, sparse-scalar)
- Transcendentals: exp, ln, sqrt, abs, sign, sin, cos, pow
- Binary: min, max, gt, lt, ge, le, eq, ne
- Reductions: sum, min_val, max_val, frobenius norm, trace
- Matrix products: SpMV, SpMM, SpSpMM, spmv_transpose
- Masked operations: einsum with sparse masks
Iterative Linear Solvers (6)
- CG - Conjugate Gradient; for symmetric positive definite systems
- BiCGSTAB - Biconjugate Gradient Stabilized; for nonsymmetric systems
- GMRES - Generalized Minimal Residual; with configurable restart
- MINRES - Minimum Residual; for symmetric indefinite systems
- CGNE - CG on Normal Equations; for overdetermined least-squares (m > n)
- CGNR - CG on Normal Residual; for underdetermined systems (m < n)
Preconditioners (4)
- Identity - No preconditioning (baseline)
- ILU(0) - Incomplete LU factorization
- Jacobi - Diagonal (simplest, O(nnz) construction)
- SSOR - Symmetric Successive Over-Relaxation (configurable omega)
Eigensolvers (3)
- Power Iteration - Dominant eigenvalue/eigenvector
- Inverse Power Iteration - Smallest eigenvalue (via ILU)
- Lanczos Algorithm - Multiple eigenvalues for symmetric matrices
Graph Algorithms (14)
- BFS, DFS - Breadth-first and depth-first traversal
- Dijkstra - Single-source shortest paths (non-negative weights)
- Bellman-Ford - Shortest paths with negative weights, negative cycle detection
- SCC - Strongly Connected Components (Tarjan/Kosaraju)
- PageRank - Vertex importance ranking (power iteration, configurable damping)
- MST - Minimum Spanning Tree (Kruskal's + Union-Find with path compression)
- Graph Coloring - Greedy coloring with degree-based vertex ordering
- MIS - Maximal Independent Set (greedy, degree-based)
- Bipartite detection, Topological Sort, Has Cycle, Vertex Degrees, Connected Components
Matrix Utilities
- Constructors (5): Graph Laplacian, Adjacency, 2D Poisson (5-point stencil), Tridiagonal, Identity
- Factorizations (2): ILU(0), IC(0) with forward/backward substitution
- Reordering (2): Reverse Cuthill-McKee (RCM), Approximate Minimum Degree (AMD)
- I/O: Matrix Market format (read/write)
Features
- 8 sparse formats optimized for different access patterns
- Efficient conversion between all formats
- 36 sparse matrix operations with full type genericity
- 6 iterative linear solvers with preconditioning support
- 3 eigensolvers for spectral analysis
- 14 graph algorithms
- Sparsity statistics (nnz, density, structural analysis)
- Generic over scalar types (f32, f64, complex types)
- Parallel operations via Rayon
Usage
Add to your Cargo.toml:
[]
= "0.1"
# Enable CSF and HiCOO formats
= { = "0.1", = ["csf"] }
COO Format
use Coo;
// Create sparse tensor from triplets
let indices = vec!;
let values = vec!;
let shape = vec!;
let coo = from_triplets?;
println!;
println!;
CSR Operations
use ;
let csr: = coo.to_csr?;
let x = vec!;
let y = csr.spmv?;
Masked Einsum
use MaskPack;
use einsum_ex;
// Create mask for selective computation
let mask = from_indices;
// Compute only masked elements
let result =
.inputs
.hints
.run?;
Iterative Linear Solvers
use ;
let config = SolverConfig ;
// CG for SPD systems
let = solve?;
// BiCGSTAB for nonsymmetric systems
let = solve?;
// GMRES with restart
let = solve_with_restart?;
println!;
Least-Squares Solvers
use ;
// Overdetermined system (m > n): min ||Ax - b||
let = solve?;
// Underdetermined system (m < n): min-norm solution
let = solve?;
Preconditioned Solving
use ;
// ILU(0) preconditioning
let precond = new?;
let = solve_preconditioned?;
// Jacobi preconditioning
let precond = new;
let = solve_preconditioned?;
Eigensolvers
use ;
// Dominant eigenvalue
let = power_iteration?;
// Smallest eigenvalue
let = inverse_power_iteration?;
// Multiple eigenvalues via Lanczos
let = lanczos?;
Graph Algorithms
use ;
// BFS traversal
let order = bfs?;
// Shortest paths
let distances = dijkstra?;
// PageRank (damping = 0.85)
let ranks = page_rank?;
// Minimum Spanning Tree (Kruskal's)
let mst_edges = minimum_spanning_tree?;
// Graph Coloring
let = graph_coloring?;
Matrix Construction
use ;
// 2D Poisson matrix (finite difference)
let p = ?; // 50x50 grid
// Tridiagonal matrix
let a = ?;
// Graph Laplacian from edge list
let l = ?;
Matrix Market I/O
use ;
let csr = ?;
write_matrix_market?;
Sparse Formats
COO (Coordinate)
- Use case: Construction, format conversion, random access by index
- Access: O(nnz) scan
- Memory: ndim x nnz (indices) + nnz (values)
CSR/CSC (Compressed Sparse Row/Column)
- Use case: Matrix-vector products, row/col access patterns
- Access: O(1) row/col start, O(nnz) iteration
- Memory: 2 x nnz + nrows/ncols
BCSR (Block CSR)
- Use case: Block-structured sparsity (FEM, graph convolutions)
- Access: Block-level operations
- Memory: Efficient for dense blocks
ELL (ELLPACK)
- Use case: Regular sparsity patterns, SIMD/GPU execution
- Access: Regular stride (SIMD-friendly)
- Memory: max_nnz_per_row x nrows
DIA (Diagonal)
- Use case: Banded matrices, finite difference stencils
- Access: O(1) diagonal access
- Memory: n_diags x n (dense diagonals)
CSF (Compressed Sparse Fiber)
- Use case: True N-dimensional sparsity, tensor operations
- Access: Fiber-based iteration
- Memory: Hierarchical compression
- Feature: Requires
csffeature flag
HiCOO (Hierarchical COO)
- Use case: Large sparse tensors with cache locality
- Access: Block-hierarchical iteration
- Memory: Better cache utilization than flat COO
- Feature: Requires
csffeature flag
Linear Solvers
| Solver | System Type | Complexity | Notes |
|---|---|---|---|
| CG | SPD | O(nnz x sqrt(kappa)) | Optimal for SPD |
| BiCGSTAB | Nonsymmetric | O(nnz x iters) | Stable variant of BiCG |
| GMRES | General | O(nnz x m x iters) | m = restart parameter |
| MINRES | Symmetric indefinite | O(nnz x iters) | Lanczos + Givens rotations |
| CGNE | Overdetermined (m > n) | O((nnz_A + nnz_At) x iters) | Solves normal equations |
| CGNR | Underdetermined (m < n) | O((nnz_A + nnz_At) x iters) | Minimum-norm solution |
Graph Algorithms
| Algorithm | Complexity | Applications |
|---|---|---|
| BFS | O(V + E) | Level-set traversal, shortest hops |
| DFS | O(V + E) | Cycle detection, topological order |
| Dijkstra | O((V + E) log V) | Routing, shortest paths |
| Bellman-Ford | O(V x E) | Negative weights, arbitrage detection |
| SCC | O(V + E) | Strongly connected components |
| PageRank | O((V + E) x iters) | Search ranking, influence analysis |
| MST (Kruskal) | O(E log E + E x alpha(V)) | Network design, clustering |
| Graph Coloring | O(V + E) | Register allocation, scheduling |
| MIS | O(V + E) | Vertex cover, parallel processing |
| Bipartite | O(V + E) | Matching, two-colorability |
| Topological Sort | O(V + E) | Dependency ordering |
| Has Cycle | O(V + E) | DAG verification |
| Vertex Degrees | O(V + E) | Degree distribution |
| Connected Components | O(V + E) | Cluster identification |
Performance
- SpMV: > 70% of dense GEMM throughput at 10% density
- Masked einsum: >= 5x speedup vs dense at 90% sparsity
- Format conversion: Parallel sorting for COO-to-CSR
Examples
See examples/ directory:
coo_basics.rs- COO format usage and conversioncsr_operations.rs- CSR SpMV, SpMM operationsmasked_einsum.rs- Selective computation with masksformat_conversion.rs- Converting between formatsiterative_solvers.rs- CG, BiCGSTAB, GMRES with preconditioningreordering.rs- RCM/AMD reordering for bandwidth reduction
Feature Flags
default=["parallel"]- Parallel operations enabledparallel- Multi-threaded format conversion and operationscsf- Enable CSF and HiCOO formats for N-D sparse tensors
Testing
RC.1 test coverage: 426 passing (3 ignored), 100% pass rate
- Property tests: 22 (proptest framework)
- Benchmark groups: 21
- Zero
todo!()/unimplemented!()macros - Zero clippy warnings
- Zero documentation warnings
# Run all tests
# Run with nextest
# Run benchmarks
# Property tests
Dependencies
- tenrso-core - Tensor types
- scirs2-core - Array operations
- indexmap - Efficient hash maps for sparse construction
- rayon (optional) - Parallel operations
License
Apache-2.0