| Crates.io | algebra-sparse |
| lib.rs | algebra-sparse |
| version | 0.4.0-beta.1 |
| created_at | 2025-10-24 03:16:55.705319+00 |
| updated_at | 2025-10-24 03:16:55.705319+00 |
| description | Efficient sparse linear algebra library built on nalgebra with CSR/CSC formats and block diagonal matrix support |
| homepage | |
| repository | |
| max_upload_size | |
| id | 1897941 |
| size | 111,240 |
Efficient sparse linear algebra library built on top of [nalgebra], providing high-performance sparse matrix and vector operations for scientific computing and physics simulations.
Add this to your Cargo.toml:
[dependencies]
algebra-sparse = "*"
use algebra_sparse::CsrMatrix;
use nalgebra::DMatrix;
// Create a dense matrix and convert to sparse
let dense = DMatrix::from_row_slice(3, 3, &[
1.0, 0.0, 2.0,
0.0, 3.0, 0.0,
4.0, 0.0, 5.0,
]);
// Convert to CSR format (automatically filters zeros)
let sparse = CsrMatrix::from_dense(dense.as_view());
// Perform sparse matrix-vector multiplication
let vector = nalgebra::DVector::from_vec(vec![1.0, 2.0, 3.0]);
let result = sparse.as_view() * vector;
println!("Result: {:?}", result);
use algebra_sparse::DiagonalBlockMatrix;
// Create a block diagonal matrix with 2x2 and 3x3 blocks
// Blocks are stored in column-major order
let block_values = vec![
1.0, 3.0, 2.0, 4.0, // 2x2 block [1 2; 3 4]
5.0, 8.0, 11.0, 6.0, 9.0, 12.0, 7.0, 10.0, 13.0 // 3x3 block
];
let block_sizes = [2, 3];
let matrix = DiagonalBlockMatrix::from_block_values(block_values, &block_sizes);
println!("Matrix shape: {}x{}", matrix.nrows(), matrix.ncols());
use algebra_sparse::CsrMatrix;
let mut matrix = CsrMatrix::new(4); // 4 columns
// Add first row
let mut builder = matrix.new_row_builder(1e-10);
builder.push(0, 1.0);
builder.push(2, 2.0);
// Row is automatically added when builder is dropped
// Add second row
let mut builder = matrix.new_row_builder(1e-10);
builder.push(1, 3.0);
builder.push(3, 4.0);
println!("Number of rows: {}", matrix.as_view().nrows());
use algebra_sparse::CsrMatrix;
use nalgebra::DMatrix;
let a_dense = DMatrix::from_row_slice(2, 3, &[1.0, 0.0, 2.0, 0.0, 3.0, 0.0]);
let b_dense = DMatrix::from_row_slice(3, 2, &[1.0, 2.0, 3.0, 4.0, 5.0, 6.0]);
let a = CsrMatrix::from_dense(a_dense.as_view());
let b = CsrMatrix::from_dense(b_dense.transpose().as_view());
let result = a.as_view() * b.transpose();
println!("Sparse multiplication result:");
println!("{}", result.to_dense());
use algebra_sparse::CsrMatrix;
use nalgebra::DMatrix;
let dense = DMatrix::from_row_slice(2, 3, &[1.0, 2.0, 3.0, 4.0, 5.0, 6.0]);
let csr = CsrMatrix::from_dense(dense.as_view());
// Transpose CSR to CSC
let csc = csr.as_view().transpose();
println!("Original shape: {:?}", csr.as_view().shape());
println!("Transposed shape: {:?}", csc.shape());
For comprehensive API documentation, visit docs.rs/algebra-sparse.