| Crates.io | nalgebra-sparse-linalg |
| lib.rs | nalgebra-sparse-linalg |
| version | 0.1.10 |
| created_at | 2025-05-14 21:43:19.068135+00 |
| updated_at | 2025-06-03 15:44:13.464481+00 |
| description | Sparse linear algebra library for Rust using nalgebra including linear solvers and SVD. |
| homepage | |
| repository | |
| max_upload_size | |
| id | 1674011 |
| size | 132,355 |
High-performance sparse linear algebra algorithms for Rust - Iterative solvers, matrix decompositions, and numerical methods for large-scale sparse matrices using nalgebra_sparse.
nalgebra-sparse-linalg provides efficient numerical algorithms for sparse linear algebra computations in Rust. Built on top of nalgebra_sparse, this library offers:
f32, f64)Perfect for scientific computing, machine learning, data analysis, and numerical simulation applications.
f32, f64, and complex numbersAdd this to your Cargo.toml:
[dependencies]
nalgebra-sparse-linalg = "0.1"
nalgebra-sparse = "0.9"
For advanced multigrid methods, enable the AMG feature:
[dependencies]
nalgebra-sparse-linalg = { version = "0.1", features = ["amg"] }
nalgebra-sparse = "0.9"
use nalgebra_sparse::{na::DVector, CsrMatrix};
use nalgebra_sparse_linalg::iteratives::conjugate_gradient::solve;
// Create a sparse matrix and right-hand side
let a = CsrMatrix::identity(1000);
let b = DVector::from_vec(vec![1.0; 1000]);
// Solve Ax = b
let solution = solve(&a, &b, 1000, 1e-10).unwrap();
use nalgebra_sparse::CsrMatrix;
use nalgebra_sparse_linalg::svd::TruncatedSVD;
// Create or load your data matrix
let matrix = CsrMatrix::from(/* your sparse matrix */);
// Compute top 50 singular vectors and values
let svd = TruncatedSVD::new(&matrix, 50);
// Access results
println!("Singular values: {:?}", svd.singular_values);
println!("Left singular vectors shape: {:?}", svd.u.shape());
use nalgebra_sparse::{na::DVector, CsrMatrix};
use nalgebra_sparse_linalg::iteratives::jacobi::solve;
let a = CsrMatrix::identity(3);
let b = DVector::from_vec(vec![1.0, 2.0, 3.0]);
let result = solve(&a, &b, 100, 1e-10);
assert!(result.is_some());
use nalgebra_sparse::{na::DVector, CsrMatrix};
use nalgebra_sparse_linalg::iteratives::gauss_seidel::solve;
let a = CsrMatrix::identity(3);
let b = DVector::from_vec(vec![1.0, 2.0, 3.0]);
let result = solve(&a, &b, 100, 1e-10);
assert!(result.is_some());
use nalgebra_sparse::{na::DVector, CsrMatrix};
use nalgebra_sparse_linalg::iteratives::relaxation::solve;
let a = CsrMatrix::identity(3);
let b = DVector::from_vec(vec![1.0, 2.0, 3.0]);
let omega = 0.8; // Relaxation parameter
let result = solve(&a, &b, 100, omega, 1e-10);
assert!(result.is_some());
use nalgebra_sparse::{na::DVector, CsrMatrix};
use nalgebra_sparse_linalg::iteratives::conjugate_gradient::solve;
// Works with both CSR and CSC matrices
let a = CsrMatrix::identity(3);
let b = DVector::from_vec(vec![2.0, 4.0, 6.0]);
let result = solve(&a, &b, 100, 1e-10);
assert!(result.is_some());
use nalgebra_sparse::{na::DVector, CsrMatrix};
use nalgebra_sparse_linalg::iteratives::biconjugate_gradient::solve;
// Suitable for non-symmetric matrices
let a = CsrMatrix::identity(3);
let b = DVector::from_vec(vec![2.0, 4.0, 6.0]);
let result = solve(&a, &b, 100, 1e-10);
assert!(result.is_some());
Note: Requires the amg feature flag
use nalgebra_sparse::{na::DVector, CsrMatrix};
use nalgebra_sparse_linalg::iteratives::amg::solve_amg;
// AMG is particularly effective for problems arising from PDEs
// Create a sparse matrix (e.g., from finite difference discretization)
let a = /* your sparse matrix from PDE discretization */;
let b = DVector::from_vec(vec![1.0; a.nrows()]);
// AMG parameters: max_iterations, tolerance, strength_threshold
let result = solve_amg(&a, &b, 100, 1e-10, 0.25);
assert!(result.is_some());
Note: Requires the amg feature flag
use nalgebra_sparse::{na::DVector, CsrMatrix};
use nalgebra_sparse_linalg::iteratives::amg::Amg;
use nalgebra_sparse_linalg::iteratives::IterativeSolver;
// Create AMG solver with custom parameters
let mut amg_solver = Amg::new(
1e-10, // tolerance
0.25, // strength threshold
100 // max iterations
);
// Initialize with matrix and RHS
amg_solver.init(&a, &b, None);
// Solve iteratively
let converged = amg_solver.solve_iterations(&a, &b, 100);
if converged {
let solution = amg_solver.solution().clone();
println!("Converged in {} iterations", amg_solver.iterations());
}
use nalgebra_sparse::{CsrMatrix, na::DMatrix};
use nalgebra_sparse_linalg::svd::TruncatedSVD;
// Create a large data matrix (e.g., document-term matrix)
let dense_matrix = DMatrix::from_row_slice(1000, 500, &[/* your data */]);
let sparse_matrix = CsrMatrix::from(&dense_matrix);
// Compute top 100 components for dimensionality reduction
let svd = TruncatedSVD::new(&sparse_matrix, 100);
// The result contains:
// - svd.u: Left singular vectors (1000 x 100)
// - svd.singular_values: Singular values in descending order (100)
use nalgebra_sparse::{CsrMatrix, na::DMatrix};
use nalgebra_sparse_linalg::svd::TruncatedSVD;
// Center your data matrix (subtract mean)
let centered_data = /* your centered data matrix */;
let sparse_data = CsrMatrix::from(¢ered_data);
// Compute principal components
let n_components = 10;
let svd = TruncatedSVD::new(&sparse_data, n_components);
// svd.u contains the principal component loadings
// svd.singular_values contains the explained variance (after squaring)
This library is optimized for:
| Problem Type | Recommended Solver | Notes |
|---|---|---|
| Symmetric positive-definite | Conjugate Gradient | Fastest convergence |
| General square matrices | BiConjugate Gradient | Good for non-symmetric |
| Diagonally dominant | Jacobi or Gauss-Seidel | Simple and robust |
| Large-scale PCA/SVD | Truncated SVD | Memory efficient |
| Ill-conditioned systems | Relaxation methods | Adjustable convergence |
| DE discretizations | Algebraic Multigrid (AMG) | Optimal for grid-based problems |
See API Documentation - Complete API reference
We welcome contributions! Please see our contributing guidelines for details on:
Licensed under either of:
at your option.
nalgebra - Dense linear algebranalgebra-sparse - Sparse matrix formats