Skip to content

Commit 42a784e

Browse files
authored
Merge pull request #6 from DimitriTimoz/svd
SVD
2 parents a58a760 + 188f30d commit 42a784e

5 files changed

Lines changed: 524 additions & 37 deletions

File tree

Cargo.lock

Lines changed: 25 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
[package]
22
name = "nalgebra-sparse-linalg"
3-
description = "Sparse linear algebra library for Rust using nalgebra"
3+
description = "Sparse linear algebra library for Rust using nalgebra including linear solvers and SVD."
44
authors = ["Dimitri Timoz <dimitri.timoz@protonmail.com>"]
55
license = "MIT"
6-
version = "0.1.8"
6+
version = "0.1.9"
77
edition = "2024"
88

99
[features]
@@ -12,10 +12,11 @@ amg = []
1212
[lib]
1313

1414
[dependencies]
15-
log = "0.4.27"
15+
log = "0.4"
1616
nalgebra-sparse = "0.10"
1717
rayon = "1.10"
1818
rand = { version = "0.9" }
19+
rand_distr = "0.5.1"
1920

2021
[dev-dependencies]
2122
criterion = { version = "0.6" }

readme.md

Lines changed: 180 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,45 @@
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

1944
Add this to your `Cargo.toml`:
2045

@@ -24,75 +49,203 @@ nalgebra-sparse-linalg = "0.1"
2449
nalgebra-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
3089
use nalgebra_sparse::{na::DVector, CsrMatrix};
3190
use nalgebra_sparse_linalg::iteratives::jacobi::solve;
3291

3392
let 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]);
3594
let result = solve(&a, &b, 100, 1e-10);
3695
assert!(result.is_some());
3796
```
3897

39-
Exxample (Gauss-Seidel, CSR):
40-
98+
#### Gauss-Seidel Method
4199
```rust
42100
use nalgebra_sparse::{na::DVector, CsrMatrix};
43101
use nalgebra_sparse_linalg::iteratives::gauss_seidel::solve;
44102

45103
let 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]);
47105
let result = solve(&a, &b, 100, 1e-10);
48106
assert!(result.is_some());
49107
```
50108

51-
Example (Relaxation, CSR):
52-
109+
#### Successive Over-Relaxation (SOR)
53110
```rust
54111
use nalgebra_sparse::{na::DVector, CsrMatrix};
55112
use nalgebra_sparse_linalg::iteratives::relaxation::solve;
56113

57114
let 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);
60118
assert!(result.is_some());
61119
```
62120

63-
Example (Conjugate Gradient, CSC or CSR):
64-
121+
#### Conjugate Gradient
65122
```rust
66123
use nalgebra_sparse::{na::DVector, CsrMatrix};
67124
use nalgebra_sparse_linalg::iteratives::conjugate_gradient::solve;
68125

126+
// Works with both CSR and CSC matrices
69127
let 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]);
71129
let result = solve(&a, &b, 100, 1e-10);
72130
assert!(result.is_some());
73131
```
74132

75-
Example (BiConjugate Gradient, CSR or CSC):
76-
133+
#### BiConjugate Gradient
77134
```rust
78135
use nalgebra_sparse::{na::DVector, CsrMatrix};
79136
use nalgebra_sparse_linalg::iteratives::biconjugate_gradient::solve;
80137

138+
// Suitable for non-symmetric matrices
81139
let 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]);
83141
let result = solve(&a, &b, 100, 1e-10);
84142
assert!(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

98246
at 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

src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
11
pub mod iteratives;
22
pub mod preconditioners;
3+
pub mod svd;
4+
5+
/// Re-exporting nalgebra_sparse for convenience
6+
pub use nalgebra_sparse as na_sparse;
7+
pub use na_sparse::{CscMatrix, CsrMatrix};

0 commit comments

Comments
 (0)