Expand description
Sparse module
This module provides implementations of various sparse matrix and array formats and operations,
similar to SciPy’s sparse
module.
§Overview
- Sparse formats (CSR, CSC, COO, DOK, LIL, DIA, BSR, etc.)
- Specialized sparse formats (Symmetric CSR, Symmetric COO)
- Basic operations (addition, multiplication, etc.)
- Sparse linear system solvers
- Sparse eigenvalue computation
- Conversion between different formats
§Matrix vs. Array API
This module provides both a matrix-based API and an array-based API, following SciPy’s transition to a more NumPy-compatible array interface.
When using the array interface (e.g., CsrArray
), please note that:
*
performs element-wise multiplication, not matrix multiplication- Use
dot()
method for matrix multiplication - Operations like
sum
produce arrays, not matrices - Array-style slicing operations return scalars, 1D, or 2D arrays
For new code, we recommend using the array interface, which is more consistent with the rest of the numerical ecosystem.
§Examples
§Matrix API (Legacy)
use scirs2_sparse::csr::CsrMatrix;
// Create a sparse matrix in CSR format
let rows = vec![0, 0, 1, 2, 2];
let cols = vec![0, 2, 2, 0, 1];
let data = vec![1.0, 2.0, 3.0, 4.0, 5.0];
let shape = (3, 3);
let matrix = CsrMatrix::new(data, rows, cols, shape).unwrap();
§Array API (Recommended)
use scirs2_sparse::csr_array::CsrArray;
// Create a sparse array in CSR format
let rows = vec![0, 0, 1, 2, 2];
let cols = vec![0, 2, 2, 0, 1];
let data = vec![1.0, 2.0, 3.0, 4.0, 5.0];
let shape = (3, 3);
// From triplets (COO-like construction)
let array = CsrArray::from_triplets(&rows, &cols, &data, shape, false).unwrap();
// Or directly from CSR components
// let array = CsrArray::new(...);
Re-exports§
pub use error::SparseError;
pub use error::SparseResult;
pub use sparray::is_sparse;
pub use sparray::SparseArray;
pub use sparray::SparseSum;
pub use sym_sparray::SymSparseArray;
pub use csr_array::CsrArray;
pub use csc_array::CscArray;
pub use coo_array::CooArray;
pub use dok_array::DokArray;
pub use lil_array::LilArray;
pub use dia_array::DiaArray;
pub use bsr_array::BsrArray;
pub use sym_csr::SymCsrArray;
pub use sym_csr::SymCsrMatrix;
pub use sym_coo::SymCooArray;
pub use sym_coo::SymCooMatrix;
pub use csr::CsrMatrix;
pub use csc::CscMatrix;
pub use coo::CooMatrix;
pub use dok::DokMatrix;
pub use lil::LilMatrix;
pub use dia::DiaMatrix;
pub use bsr::BsrMatrix;
pub use linalg::expm;
pub use linalg::inv;
pub use linalg::matrix_power;
pub use linalg::spsolve;
pub use combine::block_diag;
pub use combine::bmat;
pub use combine::hstack;
pub use combine::kron;
pub use combine::kronsum;
pub use combine::tril;
pub use combine::triu;
pub use combine::vstack;
Modules§
- bsr
- Block Sparse Row (BSR) matrix format
- bsr_
array - combine
- construct
- construct_
sym - Construction utilities for symmetric sparse matrices
- convert
- Conversion utilities for sparse matrices
- coo
- Coordinate (COO) matrix format
- coo_
array - csc
- Compressed Sparse Column (CSC) matrix format
- csc_
array - csr
- Compressed Sparse Row (CSR) matrix format
- csr_
array - dia
- Diagonal (DIA) matrix format
- dia_
array - dok
- Dictionary of Keys (DOK) matrix format
- dok_
array - error
- Error types for the SciRS2 sparse module
- lil
- List of Lists (LIL) matrix format
- lil_
array - linalg
- Linear algebra operations for sparse matrices
- sparray
- sym_coo
- Symmetric Coordinate (SymCOO) module
- sym_csr
- Symmetric Compressed Sparse Row (SymCSR) module
- sym_
sparray - Symmetric Sparse Array trait
- utils
- Utility functions for sparse matrices
Structs§
Functions§
- is_
sparse_ array - Check if an object is a sparse array
- is_
sparse_ matrix - Check if an object is a sparse matrix (legacy API)
- is_
sym_ sparse_ array - Check if an object is a symmetric sparse array