Skip to main content

CsrMatrix

Struct CsrMatrix 

Source
pub struct CsrMatrix {
    pub num_rows: usize,
    pub num_cols: usize,
    pub values: Vec<Complex64>,
    pub col_indices: Vec<usize>,
    pub row_ptrs: Vec<usize>,
}
Expand description

Compressed Sparse Row (CSR) matrix format

Memory-efficient storage for sparse matrices with O(nnz) space complexity. Matrix-vector products are O(nnz) instead of O(n²) for dense matrices.

Fields§

§num_rows: usize

Number of rows

§num_cols: usize

Number of columns

§values: Vec<Complex64>

Non-zero values in row-major order

§col_indices: Vec<usize>

Column indices for each value

§row_ptrs: Vec<usize>

Row pointers: row_ptrs[i] is the start index in values/col_indices for row i row_ptrs[num_rows] = nnz (total number of non-zeros)

Implementations§

Source§

impl CsrMatrix

Source

pub fn new(num_rows: usize, num_cols: usize) -> Self

Create a new empty CSR matrix

Source

pub fn with_capacity( num_rows: usize, num_cols: usize, nnz_estimate: usize, ) -> Self

Create a CSR matrix with pre-allocated capacity

Source

pub fn from_dense(dense: &Array2<Complex64>, threshold: f64) -> Self

Create a CSR matrix from a dense matrix

Only stores entries with magnitude > threshold

Source

pub fn from_triplets( num_rows: usize, num_cols: usize, triplets: Vec<(usize, usize, Complex64)>, ) -> Self

Create a CSR matrix from COO (Coordinate) format triplets

Triplets are (row, col, value). Duplicate entries are summed.

Source

pub fn nnz(&self) -> usize

Number of non-zero entries

Source

pub fn sparsity(&self) -> f64

Sparsity ratio (fraction of non-zero entries)

Source

pub fn row_range(&self, row: usize) -> Range<usize>

Get the range of indices in values/col_indices for a given row

Source

pub fn row_entries( &self, row: usize, ) -> impl Iterator<Item = (usize, Complex64)> + '_

Get the (col, value) pairs for a row

Source

pub fn matvec(&self, x: &Array1<Complex64>) -> Array1<Complex64>

Matrix-vector product: y = A * x

Source

pub fn matvec_add(&self, x: &Array1<Complex64>, y: &mut Array1<Complex64>)

Matrix-vector product with accumulation: y += A * x

Source

pub fn matvec_transpose(&self, x: &Array1<Complex64>) -> Array1<Complex64>

Transpose matrix-vector product: y = A^T * x

Source

pub fn matvec_hermitian(&self, x: &Array1<Complex64>) -> Array1<Complex64>

Hermitian (conjugate transpose) matrix-vector product: y = A^H * x

Source

pub fn get(&self, i: usize, j: usize) -> Complex64

Get element at (i, j), returns 0 if not stored

Source

pub fn diagonal(&self) -> Array1<Complex64>

Extract diagonal elements

Source

pub fn scale(&mut self, scalar: Complex64)

Scale all values by a scalar

Source

pub fn add_diagonal(&mut self, scalar: Complex64)

Add a scalar to the diagonal

Source

pub fn identity(n: usize) -> Self

Create identity matrix in CSR format

Source

pub fn from_diagonal(diag: &Array1<Complex64>) -> Self

Create diagonal matrix from vector

Source

pub fn to_dense(&self) -> Array2<Complex64>

Convert to dense matrix (for debugging/small matrices)

Trait Implementations§

Source§

impl Clone for CsrMatrix

Source§

fn clone(&self) -> CsrMatrix

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 Debug for CsrMatrix

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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.