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: usizeNumber of rows
num_cols: usizeNumber 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>
impl<T: ComplexField> CsrMatrix<T>
Sourcepub fn with_capacity(
num_rows: usize,
num_cols: usize,
nnz_estimate: usize,
) -> Self
pub fn with_capacity( num_rows: usize, num_cols: usize, nnz_estimate: usize, ) -> Self
Create a CSR matrix with pre-allocated capacity
Sourcepub fn from_raw_parts(
num_rows: usize,
num_cols: usize,
row_ptrs: Vec<usize>,
col_indices: Vec<usize>,
values: Vec<T>,
) -> Self
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_ptrsmust have lengthnum_rows + 1col_indicesandvaluesmust have the same lengthrow_ptrs[num_rows]must equalvalues.len()
Sourcepub fn from_dense(dense: &Array2<T>, threshold: T::Real) -> Self
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
Sourcepub fn from_triplets(
num_rows: usize,
num_cols: usize,
triplets: Vec<(usize, usize, T)>,
) -> Self
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.
Sourcepub fn row_range(&self, row: usize) -> Range<usize>
pub fn row_range(&self, row: usize) -> Range<usize>
Get the range of indices in values/col_indices for a given row
Sourcepub fn row_entries(&self, row: usize) -> impl Iterator<Item = (usize, T)> + '_
pub fn row_entries(&self, row: usize) -> impl Iterator<Item = (usize, T)> + '_
Get the (col, value) pairs for a row
Sourcepub fn matvec(&self, x: &Array1<T>) -> Array1<T>
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.
Sourcepub fn matvec_add(&self, x: &Array1<T>, y: &mut Array1<T>)
pub fn matvec_add(&self, x: &Array1<T>, y: &mut Array1<T>)
Matrix-vector product with accumulation: y += A * x
Sourcepub fn matvec_transpose(&self, x: &Array1<T>) -> Array1<T>
pub fn matvec_transpose(&self, x: &Array1<T>) -> Array1<T>
Transpose matrix-vector product: y = A^T * x
Sourcepub fn matvec_hermitian(&self, x: &Array1<T>) -> Array1<T>
pub fn matvec_hermitian(&self, x: &Array1<T>) -> Array1<T>
Hermitian (conjugate transpose) matrix-vector product: y = A^H * x
Sourcepub fn add_diagonal(&mut self, scalar: T)
pub fn add_diagonal(&mut self, scalar: T)
Add a scalar to the diagonal
Sourcepub fn from_diagonal(diag: &Array1<T>) -> Self
pub fn from_diagonal(diag: &Array1<T>) -> Self
Create diagonal matrix from vector
Source§impl<T: ComplexField> CsrMatrix<T>
Optimized sparse matrix-matrix multiplication: C = A * B
impl<T: ComplexField> CsrMatrix<T>
Optimized sparse matrix-matrix multiplication: C = A * B
Uses a sorted accumulation approach instead of HashMap for better cache locality, providing 2-4x speedup for the AMG Galerkin product.
For CSR matrices A (m×k) and B (k×n), computes C (m×n).
Trait Implementations§
Source§impl<T: ComplexField> LinearOperator<T> for CsrMatrix<T>
impl<T: ComplexField> LinearOperator<T> for CsrMatrix<T>
Source§fn apply_transpose(&self, x: &Array1<T>) -> Array1<T>
fn apply_transpose(&self, x: &Array1<T>) -> Array1<T>
Source§fn apply_hermitian(&self, x: &Array1<T>) -> Array1<T>
fn apply_hermitian(&self, x: &Array1<T>) -> Array1<T>
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> UnsafeUnpin for CsrMatrix<T>
impl<T> UnwindSafe for CsrMatrix<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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