single-algebra 🧮
A powerful linear algebra and machine learning utilities library for Rust, providing efficient matrix operations, dimensionality reduction, and statistical analysis tools.
Features 🚀
- Efficient Matrix Operations: Support for both dense and sparse matrices (CSR/CSC formats)
- Dimensionality Reduction: PCA implementations for both dense and sparse matrices
- SVD Implementations: Multiple SVD backends including LAPACK and Faer
- Statistical Analysis: Comprehensive statistical operations with batch processing support
- Similarity Measures: Collection of distance/similarity metrics for high-dimensional data
- Masking Support: Selective data processing with boolean masks
- Parallel Processing: Efficient multi-threaded implementations using Rayon
- Feature-Rich: Configurable through feature flags for specific needs
Matrix Operations 📊
- SVD Decomposition: Choose between parallel, LAPACK, or Faer implementations
- Sparse Matrix Support: Comprehensive operations for CSR and CSC sparse matrix formats
- Masked Operations: Selective data processing with boolean masks
- Batch Processing: Statistical operations grouped by batch identifiers
- Normalization: Row and column normalization with customizable targets
Dimensionality Reduction ⬇️
- PCA Framework: Flexible implementation with customizable SVD backends
- Dense Matrix PCA: Optimized implementation for dense matrices
- Sparse Matrix PCA: Memory-efficient PCA for sparse matrices
- Masked Sparse PCA: Apply PCA on selected features only
- Incremental Processing: Support for large datasets that don't fit in memory
Similarity Measures 📏
- Cosine Similarity: Measure similarity based on the cosine of the angle between vectors
- Euclidean Similarity: Similarity based on Euclidean distance
- Pearson Similarity: Measure linear correlation between vectors
- Manhattan Similarity: Similarity based on Manhattan distance
- Jaccard Similarity: Measure similarity as intersection over union
Statistical Analysis 📈
- Basic Statistics: Mean, variance, sum, min/max operations
- Batch Statistics: Compute statistics grouped by batch identifiers
- Matrix Variance: Efficient variance calculations for matrices
- Nonzero Counting: Count non-zero elements in sparse matrices
- Masked Statistics: Compute statistics on selected rows/columns only
Installation
Add this to your Cargo.toml:
[]
= "0.5.0"
Feature Flags
Enable optional features based on your needs:
[]
= { = "0.5.0", = ["lapack", "faer"] }
Available features:
smartcore: Enable integration with the SmartCore machine learning librarylapack: Use the LAPACK backend for linear algebra operationsfaer: Use the Faer backend for linear algebra operationssimba: Enable SIMD optimizations via simba
Usage Examples
Basic PCA with LAPACK Backend
use ;
use ;
// Create a sample matrix
let data = array!;
// Build PCA with LAPACK backend
let mut pca = new
.n_components
.center
.scale
.build;
// Fit and transform data
pca.fit.unwrap;
let transformed = pca.transform.unwrap;
// Access results
let components = pca.components.unwrap;
let explained_variance = pca.explained_variance_ratio.unwrap;
Sparse Matrix Operations
use ;
use MatrixSum;
// Create a sparse matrix
let mut coo = new;
coo.push;
coo.push;
coo.push;
let csr: = .into;
// Calculate column sums
let col_sums: = csr.sum_col.unwrap;
Batch Processing
use CsrMatrix;
use BatchMatrixMean;
// Sample data with batch identifiers
let matrix = create_sparse_matrix;
let batches = vec!;
// Calculate mean per batch
let batch_means = matrix.mean_batch_col.unwrap;
// Access results for a specific batch
let batch1_means = batch_means.get.unwrap;
Similarity Measures
use Array1;
use ;
let a = from_vec;
let b = from_vec;
let cosine = CosineSimilarity;
let similarity = cosine.calculate;
Performance Considerations
- For large matrices, consider using sparse representations (CSR/CSC)
- Enable the appropriate backend (
lapackorfaer) based on your needs - Use masked operations when working with subsets of data
- Batch processing can significantly improve performance for grouped operations
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the BSD 3-Clause License - see the LICENSE.md file for details.
Acknowledgments
- The LAPACK integration is built upon the
nalgebra-lapackcrate - Some components are inspired by scikit-learn's implementations
- The Faer backend leverages the high-performance
faercrate