Skip to main content

CsrMatrix

Struct CsrMatrix 

Source
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_ptr has length m + 1
  • col_indices and values each have length nnz
  • Row i spans indices row_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: usize

Number of rows.

§cols: usize

Number of columns.

Implementations§

Source§

impl<T: Copy + Default + Mul<Output = T> + AddAssign> CsrMatrix<T>

Source

pub fn spmv(&self, x: &[T], y: &mut [T])

Sparse matrix-vector multiply: y = A * x.

§Panics

Debug-asserts that x.len() >= self.cols and y.len() >= self.rows.

Source§

impl CsrMatrix<f32>

Source

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.

Source

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>

Source

pub fn spmv_unchecked(&self, x: &[f64], y: &mut [f64])

High-performance SpMV for f64 with bounds-check elimination.

Source§

impl<T> CsrMatrix<T>

Source

pub fn nnz(&self) -> usize

Number of non-zero entries.

Source

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

pub fn row_entries(&self, row: usize) -> impl Iterator<Item = (usize, &T)>

Iterate over (col_index, &value) pairs for the given row.

Source§

impl<T: Copy + Default> CsrMatrix<T>

Source

pub fn transpose(&self) -> CsrMatrix<T>

Transpose: produces A^T in CSR form.

Uses a two-pass counting sort in O(nnz + rows + cols) time and O(nnz) extra memory. Required by backward push which operates on the reversed adjacency structure.

Source§

impl<T: Copy + Default + AddAssign> CsrMatrix<T>

Source

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).

Source§

impl CsrMatrix<f32>

Source

pub fn from_coo( rows: usize, cols: usize, entries: impl IntoIterator<Item = (usize, usize, f32)>, ) -> Self

Build a CSR matrix from COO (coordinate) triplets.

Entries are sorted by (row, col) internally. Duplicate positions are summed.

Source

pub fn identity(n: usize) -> Self

Build a square identity matrix of dimension n in CSR format.

Source§

impl CsrMatrix<f64>

Source

pub fn from_coo( rows: usize, cols: usize, entries: impl IntoIterator<Item = (usize, usize, f64)>, ) -> Self

Build a CSR matrix from COO (coordinate) triplets (f64 variant).

Entries are sorted by (row, col) internally.

Source

pub fn identity(n: usize) -> Self

Build a square identity matrix of dimension n in CSR format (f64).

Trait Implementations§

Source§

impl<T: Clone> Clone for CsrMatrix<T>

Source§

fn clone(&self) -> CsrMatrix<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for CsrMatrix<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Freeze for CsrMatrix<T>

§

impl<T> RefUnwindSafe for CsrMatrix<T>
where T: RefUnwindSafe,

§

impl<T> Send for CsrMatrix<T>
where T: Send,

§

impl<T> Sync for CsrMatrix<T>
where T: Sync,

§

impl<T> Unpin for CsrMatrix<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for CsrMatrix<T>

§

impl<T> UnwindSafe for CsrMatrix<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more