Single-SVDLib: Singular Value Decomposition for Sparse Matrices
A high-performance Rust library for computing Singular Value Decomposition (SVD) on sparse matrices, with support for both Lanczos and randomized SVD algorithms.
Features
- Multiple SVD algorithms:
- Lanczos algorithm (based on SVDLIBC)
- Randomized SVD for very large and sparse matrices
- Sparse matrix support:
- Compressed Sparse Row (CSR) format
- Compressed Sparse Column (CSC) format
- Coordinate (COO) format
- Performance optimizations:
- Parallel execution with Rayon
- Adaptive tuning for highly sparse matrices
- Column masking for subspace SVD
- Generic interface:
- Works with both
f32andf64precision
- Works with both
- Comprehensive error handling and diagnostics
Installation
Add this to your Cargo.toml:
[]
= "0.6.0"
Quick Start
use ;
use svd_dim_seed;
// Create a matrix in COO format
let mut coo = new;
coo.push; coo.push; coo.push;
coo.push; coo.push; coo.push;
coo.push; coo.push; coo.push;
// Convert to CSR for better performance
let csr = from;
// Compute SVD with a fixed random seed
let svd = svd_dim_seed.unwrap;
// Access the results
let singular_values = &svd.s;
let left_singular_vectors = &svd.ut; // Note: These are transposed
let right_singular_vectors = &svd.vt; // Note: These are transposed
// Reconstruct the original matrix
let reconstructed = svd.recompose;
SVD Methods
Lanczos Algorithm (LAS2)
The Lanczos algorithm is well-suited for sparse matrices of moderate size:
use laczos;
// Basic SVD computation (uses defaults)
let svd = svd?;
// SVD with specified target rank
let svd = svd_dim?;
// SVD with specified target rank and fixed random seed
let svd = svd_dim_seed?;
// Full control over SVD parameters
let svd = svd_las2?;
Randomized SVD
For very large sparse matrices, the randomized SVD algorithm offers better performance:
use randomized;
let svd = randomized_svd?;
Column Masking
For operations on specific columns of a matrix:
use MaskedCSRMatrix;
// Create a mask for selected columns
let columns = vec!; // Only use these columns
let masked_matrix = with_columns;
// Compute SVD on the masked matrix
let svd = svd?;
Result Structure
The SVD result contains:
Note that ut and vt are returned in transposed form.
Diagnostics
Each SVD computation returns detailed diagnostics:
let svd = svd?;
println!;
println!;
println!;
println!;
Performance Tips
-
Choose the right algorithm:
- For matrices up to ~10,000 x 10,000 with moderate sparsity, use the Lanczos algorithm
- For larger matrices or very high sparsity (>99%), use randomized SVD
-
Matrix format matters:
- Convert COO matrices to CSR or CSC for computation
- CSR typically performs better for row-oriented operations
-
Adjust parameters for very sparse matrices:
- Increase power iterations in randomized SVD (e.g., 5-7)
- Use a higher
kappavalue in Lanczos for very sparse matrices
-
Consider column masking for operations that only need a subset of the data
License
This crate is licensed under the BSD License, the same as the original SVDLIBC implementation. See the SVDLIBC-LICENSE.txt file for details.
Credits
- Original SVDLIBC implementation by Doug Rohde
- Rust port maintainer of SVDLIBC: Dave Farnham
- Extensions and modifications of the original algorithm: Ian F. Diks