tang-sparse
Sparse matrices. Three formats, convert between them, multiply against dense vectors.
Formats
COO (coordinate) CSR (compressed row) CSC (compressed column)
(0,0) 1.0 row_ptrs: [0, 2, 3, 5] col_ptrs: [0, 2, 3, 4]
(0,2) 2.0 col_idx: [0, 2, 1, 0, 2] row_idx: [0, 2, 1, 0, 2]
(1,1) 3.0 values: [1, 2, 3, 4, 5] values: [1, 4, 3, 2, 5]
(2,0) 4.0
(2,2) 5.0 ┌ 1 · 2 ┐
│ · 3 · │
assembly format → └ 4 · 5 ┘ → computation format
Usage
Build with COO, convert to CSR for computation:
use ;
use DVec;
let mut coo = new;
coo.push;
coo.push;
coo.push;
coo.push;
coo.push;
let csr = coo.to_csr;
// sparse matrix-vector product
let x = from_vec;
let y = csr.spmv; // [3.0, 3.0, 9.0]
API
| Type | Key methods |
|---|---|
CooMatrix<S> |
new, with_capacity, push, to_csr |
CsrMatrix<S> |
spmv, spmv_add, get, transpose, to_dense |
CscMatrix<S> |
spmv, get, from_csr |
Duplicate entries at the same position are automatically summed during COO → CSR conversion.
Design
#![no_std]withalloc- Generic over
S: Scalar— works withf32,f64,Dual<f64> - COO for assembly, CSR/CSC for computation
- Direct field access on all structs for advanced usage