11# nalgebra-sparse-linalg
22
3- Sparse linear algebra iterative solvers for [ nalgebra_sparse] ( https://crates.io/crates/nalgebra-sparse ) .
3+ [ ![ Crates.io] ( https://img.shields.io/crates/v/nalgebra-sparse-linalg.svg )] ( https://crates.io/crates/nalgebra-sparse-linalg )
4+ [ ![ Documentation] ( https://docs.rs/nalgebra-sparse-linalg/badge.svg )] ( https://docs.rs/nalgebra-sparse-linalg )
5+ [ ![ License: MIT OR Apache-2.0] ( https://img.shields.io/crates/l/nalgebra-sparse-linalg.svg )] ( #license )
6+
7+ ** High-performance sparse linear algebra algorithms for Rust** - Iterative solvers, matrix decompositions, and numerical methods for large-scale sparse matrices using [ nalgebra_sparse] ( https://crates.io/crates/nalgebra-sparse ) .
48
59## Overview
610
7- ` nalgebra-sparse-linalg ` provides iterative methods for solving large, sparse linear systems of equations, with a focus on compatibility with the ` nalgebra_sparse ` crate. The library currently includes Jacobi and Conjugate Gradient iterative solvers for sparse matrices.
11+ ` nalgebra-sparse-linalg ` provides efficient numerical algorithms for sparse linear algebra computations in Rust. Built on top of ` nalgebra_sparse ` , this library offers:
12+
13+ - ** Fast iterative solvers** for large sparse linear systems
14+ - ** Matrix decompositions** including truncated SVD for dimensionality reduction
15+ - ** Memory-efficient algorithms** optimized for sparse matrices
16+ - ** Generic implementations** supporting multiple precision types (` f32 ` , ` f64 ` )
17+ - ** Production-ready** with comprehensive test coverage
18+
19+ Perfect for scientific computing, machine learning, data analysis, and numerical simulation applications.
820
921## Features
1022
11- - Jacobi iterative solver for sparse matrices in CSR format.
12- - Conjugate Gradient solver for symmetric positive-definite matrices in CSR and CSC formats.
13- - BiConjugate Gradient solver for general (possibly non-symmetric)
14- - Generic over scalar types (e.g., ` f32 ` , ` f64 ` ).
15- - Simple API compatible with ` nalgebra_sparse ` 's matrix and vector types.
23+ ### Iterative Linear System Solvers
24+ - ** Jacobi** - Simple and parallelizable iterative solver
25+ - ** Gauss-Seidel** - Faster convergence for well-conditioned systems
26+ - ** Successive Over-Relaxation (SOR)** - Accelerated convergence with relaxation parameter
27+ - ** Conjugate Gradient (CG)** - Optimal for symmetric positive-definite matrices
28+ - ** BiConjugate Gradient (BiCG)** - General solver for non-symmetric systems
1629
17- ## Usage
30+ ### Matrix Decompositions
31+ - ** Truncated SVD** - Randomized algorithm for efficient singular value decomposition
32+ - ** Dimensionality reduction** for large-scale data analysis
33+ - ** Low-rank approximations** for matrix compression
34+
35+ ### ⚡ Performance Features
36+ - ** CSR and CSC matrix support** - Industry-standard sparse formats
37+ - ** Memory-efficient algorithms** - Minimal memory footprint
38+ - ** Parallel computation** - Multi-threaded where applicable
39+ - ** Generic scalar types** - Support for ` f32 ` , ` f64 ` , and complex numbers
40+ - ** Zero-copy operations** - Efficient memory usage
41+
42+ ## Quick Start
1843
1944Add this to your ` Cargo.toml ` :
2045
@@ -24,75 +49,203 @@ nalgebra-sparse-linalg = "0.1"
2449nalgebra-sparse = " 0.9"
2550```
2651
27- Example (Jacobi, CSR):
52+ ### Solving Linear Systems
2853
54+ ``` rust
55+ use nalgebra_sparse :: {na :: DVector , CsrMatrix };
56+ use nalgebra_sparse_linalg :: iteratives :: conjugate_gradient :: solve;
57+
58+ // Create a sparse matrix and right-hand side
59+ let a = CsrMatrix :: identity (1000 );
60+ let b = DVector :: from_vec (vec! [1.0 ; 1000 ]);
61+
62+ // Solve Ax = b
63+ let solution = solve (& a , & b , 1000 , 1e - 10 ). unwrap ();
64+ ```
65+
66+ ### Truncated SVD for Dimensionality Reduction
67+
68+ ``` rust
69+ use nalgebra_sparse :: CsrMatrix ;
70+ use nalgebra_sparse_linalg :: svd :: TruncatedSVD ;
71+
72+ // Create or load your data matrix
73+ let matrix = CsrMatrix :: from (/* your sparse matrix */ );
74+
75+ // Compute top 50 singular vectors and values
76+ let svd = TruncatedSVD :: new (& matrix , 50 );
77+
78+ // Access results
79+ println! (" Singular values: {:?}" , svd . singular_values);
80+ println! (" Left singular vectors shape: {:?}" , svd . u. shape ());
81+ ```
82+
83+ ## Examples
84+
85+ ### Linear System Solvers
86+
87+ #### Jacobi Method
2988``` rust
3089use nalgebra_sparse :: {na :: DVector , CsrMatrix };
3190use nalgebra_sparse_linalg :: iteratives :: jacobi :: solve;
3291
3392let a = CsrMatrix :: identity (3 );
34- let b = DVector :: from_vec (vec! [1.0 ; 3 ]);
93+ let b = DVector :: from_vec (vec! [1.0 , 2.0 , 3.0 ]);
3594let result = solve (& a , & b , 100 , 1e - 10 );
3695assert! (result . is_some ());
3796```
3897
39- Exxample (Gauss-Seidel, CSR):
40-
98+ #### Gauss-Seidel Method
4199``` rust
42100use nalgebra_sparse :: {na :: DVector , CsrMatrix };
43101use nalgebra_sparse_linalg :: iteratives :: gauss_seidel :: solve;
44102
45103let a = CsrMatrix :: identity (3 );
46- let b = DVector :: from_vec (vec! [1.0 ; 3 ]);
104+ let b = DVector :: from_vec (vec! [1.0 , 2.0 , 3.0 ]);
47105let result = solve (& a , & b , 100 , 1e - 10 );
48106assert! (result . is_some ());
49107```
50108
51- Example (Relaxation, CSR):
52-
109+ #### Successive Over-Relaxation (SOR)
53110``` rust
54111use nalgebra_sparse :: {na :: DVector , CsrMatrix };
55112use nalgebra_sparse_linalg :: iteratives :: relaxation :: solve;
56113
57114let a = CsrMatrix :: identity (3 );
58- let b = DVector :: from_vec (vec! [1.0 ; 3 ]);
59- let result = solve (& a , & b , 100 , 0.8 , 1e - 10 );
115+ let b = DVector :: from_vec (vec! [1.0 , 2.0 , 3.0 ]);
116+ let omega = 0.8 ; // Relaxation parameter
117+ let result = solve (& a , & b , 100 , omega , 1e - 10 );
60118assert! (result . is_some ());
61119```
62120
63- Example (Conjugate Gradient, CSC or CSR):
64-
121+ #### Conjugate Gradient
65122``` rust
66123use nalgebra_sparse :: {na :: DVector , CsrMatrix };
67124use nalgebra_sparse_linalg :: iteratives :: conjugate_gradient :: solve;
68125
126+ // Works with both CSR and CSC matrices
69127let a = CsrMatrix :: identity (3 );
70- let b = DVector :: from_vec (vec! [2.0 ; 3 ]);
128+ let b = DVector :: from_vec (vec! [2.0 , 4.0 , 6.0 ]);
71129let result = solve (& a , & b , 100 , 1e - 10 );
72130assert! (result . is_some ());
73131```
74132
75- Example (BiConjugate Gradient, CSR or CSC):
76-
133+ #### BiConjugate Gradient
77134``` rust
78135use nalgebra_sparse :: {na :: DVector , CsrMatrix };
79136use nalgebra_sparse_linalg :: iteratives :: biconjugate_gradient :: solve;
80137
138+ // Suitable for non-symmetric matrices
81139let a = CsrMatrix :: identity (3 );
82- let b = DVector :: from_vec (vec! [2.0 ; 3 ]);
140+ let b = DVector :: from_vec (vec! [2.0 , 4.0 , 6.0 ]);
83141let result = solve (& a , & b , 100 , 1e - 10 );
84142assert! (result . is_some ());
85143```
86144
145+ ### Matrix Decompositions
146+
147+ #### Truncated SVD for Large Matrices
148+ ``` rust
149+ use nalgebra_sparse :: {CsrMatrix , na :: DMatrix };
150+ use nalgebra_sparse_linalg :: svd :: TruncatedSVD ;
151+
152+ // Create a large data matrix (e.g., document-term matrix)
153+ let dense_matrix = DMatrix :: from_row_slice (1000 , 500 , & [/* your data */ ]);
154+ let sparse_matrix = CsrMatrix :: from (& dense_matrix );
155+
156+ // Compute top 100 components for dimensionality reduction
157+ let svd = TruncatedSVD :: new (& sparse_matrix , 100 );
158+
159+ // The result contains:
160+ // - svd.u: Left singular vectors (1000 x 100)
161+ // - svd.singular_values: Singular values in descending order (100)
162+ ```
163+
164+ #### Principal Component Analysis (PCA) Example
165+ ``` rust
166+ use nalgebra_sparse :: {CsrMatrix , na :: DMatrix };
167+ use nalgebra_sparse_linalg :: svd :: TruncatedSVD ;
168+
169+ // Center your data matrix (subtract mean)
170+ let centered_data = /* your centered data matrix */ ;
171+ let sparse_data = CsrMatrix :: from (& centered_data );
172+
173+ // Compute principal components
174+ let n_components = 10 ;
175+ let svd = TruncatedSVD :: new (& sparse_data , n_components );
176+
177+ // svd.u contains the principal component loadings
178+ // svd.singular_values contains the explained variance (after squaring)
179+ ```
180+
181+ ## Use Cases
182+
183+ ### 🔬 Scientific Computing
184+ - Finite element analysis
185+ - Computational fluid dynamics
186+ - Structural analysis
187+ - Physics simulations
188+
189+ ### 🤖 Machine Learning & Data Science
190+ - Large-scale linear regression
191+ - Principal component analysis (PCA)
192+ - Recommendation systems
193+ - Natural language processing (LSA/LSI)
194+ - Image processing and computer vision
195+
196+ ### 📈 Numerical Analysis
197+ - Solving partial differential equations
198+ - Optimization problems
199+ - Signal processing
200+ - Graph algorithms
201+
202+ ## Performance
203+
204+ This library is optimized for:
205+ - ** Large sparse matrices** (>10,000 dimensions)
206+ - ** Memory efficiency** - Minimal overhead beyond input data
207+ - ** Numerical stability** - Robust algorithms with proven convergence
208+ - ** Parallel computation** - Multi-threaded operations where beneficial
209+
210+ ## Algorithm Selection Guide
211+
212+ | Problem Type | Recommended Solver | Notes |
213+ | --------------| -------------------| -------|
214+ | Symmetric positive-definite | Conjugate Gradient | Fastest convergence |
215+ | General square matrices | BiConjugate Gradient | Good for non-symmetric |
216+ | Diagonally dominant | Jacobi or Gauss-Seidel | Simple and robust |
217+ | Large-scale PCA/SVD | Truncated SVD | Memory efficient |
218+ | Ill-conditioned systems | Relaxation methods | Adjustable convergence |
219+
87220## Documentation
88221
89- See [ docs.rs] ( https://docs.rs/nalgebra-sparse-linalg ) for full API documentation.
222+ See [ API Documentation] ( https://docs.rs/nalgebra-sparse-linalg ) - Complete API reference
223+
224+ ## Contributing
225+
226+ We welcome contributions! Please see our [ contributing guidelines] ( CONTRIBUTING.md ) for details on:
227+ - Bug reports and feature requests
228+ - Code contributions and pull requests
229+ - Documentation improvements
230+ - Performance optimizations
231+
232+ ## Roadmap
233+
234+ - [ ] Preconditioned iterative methods
235+ - [ ] Additional matrix decompositions (QR, LU)
236+ - [ ] GPU acceleration support
237+ - [ ] Distributed computing capabilities
90238
91239## License
92240
93- Licensed under either of
241+ Licensed under either of:
94242
95- - Apache License, Version 2.0
96- - MIT license
243+ - [ Apache License, Version 2.0] ( LICENSE-APACHE )
244+ - [ MIT License ] ( LICENSE-MIT )
97245
98246at your option.
247+
248+ ## Related Crates
249+
250+ - [ ` nalgebra ` ] ( https://crates.io/crates/nalgebra ) - Dense linear algebra
251+ - [ ` nalgebra-sparse ` ] ( https://crates.io/crates/nalgebra-sparse ) - Sparse matrix formats
0 commit comments