CsrMatrix

Struct CsrMatrix 

Source
pub struct CsrMatrix<T: ComplexField> {
    pub num_rows: usize,
    pub num_cols: usize,
    pub values: Vec<T>,
    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<T>

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<T: ComplexField> CsrMatrix<T>

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_raw_parts( num_rows: usize, num_cols: usize, row_ptrs: Vec<usize>, col_indices: Vec<usize>, values: Vec<T>, ) -> Self

Create a CSR matrix from raw components

This is useful for converting between different CSR matrix representations that share the same internal structure.

§Panics

Panics if the input arrays are inconsistent:

  • row_ptrs must have length num_rows + 1
  • col_indices and values must have the same length
  • row_ptrs[num_rows] must equal values.len()
Source

pub fn from_dense(dense: &Array2<T>, threshold: T::Real) -> 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, T)>, ) -> 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, T)> + '_

Get the (col, value) pairs for a row

Source

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

Matrix-vector product: y = A * x

Uses parallel processing when the rayon feature is enabled and the matrix is large enough to benefit from parallelization.

Source

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

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

Source

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

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

Source

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

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

Source

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

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

Source

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

Extract diagonal elements

Source

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

Scale all values by a scalar

Source

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

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<T>) -> Self

Create diagonal matrix from vector

Source

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

Convert to dense matrix (for debugging/small matrices)

Trait Implementations§

Source§

impl<T: Clone + ComplexField> 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 + ComplexField> Debug for CsrMatrix<T>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<T: ComplexField> LinearOperator<T> for CsrMatrix<T>

Source§

fn num_rows(&self) -> usize

Number of rows in the operator
Source§

fn num_cols(&self) -> usize

Number of columns in the operator
Source§

fn apply(&self, x: &Array1<T>) -> Array1<T>

Apply the operator: y = A * x
Source§

fn apply_transpose(&self, x: &Array1<T>) -> Array1<T>

Apply the transpose: y = A^T * x
Source§

fn apply_hermitian(&self, x: &Array1<T>) -> Array1<T>

Apply the Hermitian (conjugate transpose): y = A^H * x
Source§

fn is_square(&self) -> bool

Check if the operator is square

Auto Trait Implementations§

§

impl<T> Freeze for CsrMatrix<T>

§

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

§

impl<T> Send for CsrMatrix<T>

§

impl<T> Sync for CsrMatrix<T>

§

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

§

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

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V