pub struct CsrMatrix<T> {
pub row_ptr: Vec<usize>,
pub col_indices: Vec<usize>,
pub values: Vec<T>,
pub rows: usize,
pub cols: usize,
}Expand description
Compressed Sparse Row (CSR) matrix.
Stores only non-zero entries for efficient sparse matrix-vector multiplication in O(nnz) time with excellent cache locality.
§Layout
For a matrix with m rows and nnz non-zeros:
row_ptrhas lengthm + 1col_indicesandvalueseach have lengthnnz- Row
ispans indicesrow_ptr[i]..row_ptr[i+1]
Fields§
§row_ptr: Vec<usize>Row pointers: row_ptr[i] is the start index in col_indices/values
for row i.
col_indices: Vec<usize>Column indices for each non-zero entry.
values: Vec<T>Values for each non-zero entry.
rows: usizeNumber of rows.
cols: usizeNumber of columns.
Implementations§
Source§impl CsrMatrix<f32>
impl CsrMatrix<f32>
Sourcepub fn spmv_unchecked(&self, x: &[f32], y: &mut [f32])
pub fn spmv_unchecked(&self, x: &[f32], y: &mut [f32])
High-performance SpMV with bounds-check elimination.
Identical to spmv but uses unsafe indexing to
eliminate per-element bounds checks in the inner loop, which is the
single hottest path in all iterative solvers.
§Safety contract
The caller must ensure the CSR structure is valid (use
validate_csr_matrix once
before entering the solve loop). The x and y slices must have
lengths >= cols and >= rows respectively.
Sourcepub fn fused_residual_norm_sq(
&self,
x: &[f32],
rhs: &[f32],
residual: &mut [f32],
) -> f64
pub fn fused_residual_norm_sq( &self, x: &[f32], rhs: &[f32], residual: &mut [f32], ) -> f64
Fused SpMV + residual computation: computes r[j] = rhs[j] - (A*x)[j]
and returns ||r||^2 in a single pass, avoiding a separate allocation
for Ax.
This eliminates one full memory traversal per iteration compared to
separate spmv + vector subtraction.
Source§impl CsrMatrix<f64>
impl CsrMatrix<f64>
Sourcepub fn spmv_unchecked(&self, x: &[f64], y: &mut [f64])
pub fn spmv_unchecked(&self, x: &[f64], y: &mut [f64])
High-performance SpMV for f64 with bounds-check elimination.
Source§impl<T> CsrMatrix<T>
impl<T> CsrMatrix<T>
Sourcepub fn row_degree(&self, row: usize) -> usize
pub fn row_degree(&self, row: usize) -> usize
Number of non-zeros in a specific row (i.e. the row degree for an adjacency matrix).
Source§impl<T: Copy + Default + AddAssign> CsrMatrix<T>
impl<T: Copy + Default + AddAssign> CsrMatrix<T>
Sourcepub fn from_coo_generic(
rows: usize,
cols: usize,
entries: impl IntoIterator<Item = (usize, usize, T)>,
) -> Self
pub fn from_coo_generic( rows: usize, cols: usize, entries: impl IntoIterator<Item = (usize, usize, T)>, ) -> Self
Build a CSR matrix from COO (coordinate) triplets.
Entries are sorted by (row, col) internally. Duplicate positions at the same (row, col) are kept as separate entries (caller should pre-merge if needed).