Skip to main content

CsrMatrix

Struct CsrMatrix 

Source
pub struct CsrMatrix<T> { /* private fields */ }
Expand description

Compressed Sparse Row matrix.

Three-array representation: offsets (len = rows+1), col_indices (len = nnz), values (len = nnz). Row i has nonzeros at positions offsets[i]..offsets[i+1].

All invariants are validated at construction time (provable contract).

Implementations§

Source§

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

Source

pub fn new( rows: usize, cols: usize, offsets: Vec<u32>, col_indices: Vec<u32>, values: Vec<T>, ) -> Result<Self, SparseError>

Create a CSR matrix with full validation.

§Contract: sparse-spmv-v1.yaml / format_validation

Validates all CSR invariants before construction.

§Errors

Returns error if any CSR invariant is violated.

Source

pub fn from_coo(coo: &CooMatrix<T>) -> Self
where T: AddAssign + Copy,

Convert from COO format to CSR.

Sorts triplets by row, then by column within each row. Duplicate entries are summed (standard convention).

Source

pub fn identity(n: usize) -> Self
where T: From<f32>,

Create an identity matrix of size n.

Source

pub fn rows(&self) -> usize

Number of rows.

Source

pub fn cols(&self) -> usize

Number of columns.

Source

pub fn nnz(&self) -> usize

Number of stored nonzero entries.

Source

pub fn offsets(&self) -> &[u32]

Row offsets array (len = rows + 1).

Source

pub fn col_indices(&self) -> &[u32]

Column indices array (len = nnz).

Source

pub fn values(&self) -> &[T]

Values array (len = nnz).

Source

pub fn avg_nnz_per_row(&self) -> f64

Average number of nonzeros per row.

Source

pub fn row_length_variance(&self) -> f64

Variance of row lengths (key metric for algorithm selection).

High variance → merge-based SpMV; low variance → row-split SpMV.

Source

pub fn to_dense(&self) -> Vec<T>
where T: Copy + AddAssign,

Convert to dense matrix (row-major).

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
Source§

impl SparseOps for CsrMatrix<f32>

Source§

fn spmv( &self, alpha: f32, x: &[f32], beta: f32, y: &mut [f32], ) -> Result<(), SparseError>

Sparse matrix-vector multiply: y = α * A * x + β * y Read more
Source§

fn spmm( &self, alpha: f32, b: &[f32], b_cols: usize, beta: f32, c: &mut [f32], ) -> Result<(), SparseError>

Sparse matrix-dense matrix multiply: C = α * A * B + β * C 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, 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.