pub struct CsrMatrix<T>(/* private fields */);Expand description
Compressed Sparse Row (CSR) Matrix.
CSR format is optimized for row-wise operations and matrix-vector products. It stores non-zero elements row by row, making it efficient for:
- Row access and iteration
- Sparse matrix-vector multiplication
- Row-based computations
§Format
The CSR format uses three arrays:
values: Non-zero values stored row by rowcol_indices: Column indices for each non-zero valuerow_offsets: Starting index in values/col_indices for each row
§Examples
use algebra_sparse::CsrMatrix;
use nalgebra::DMatrix;
// Create from dense matrix
let dense = DMatrix::from_row_slice(2, 3, &[
1.0, 0.0, 2.0,
0.0, 3.0, 0.0,
]);
let csr = CsrMatrix::from_dense(dense.as_view());
// Get row as sparse vector
let row = csr.as_view().get_row(0);
for (col, val) in row.iter() {
println!("({}, {})", col, val);
}Implementations§
Source§impl<T> CsrMatrix<T>where
T: Real,
impl<T> CsrMatrix<T>where
T: Real,
Sourcepub fn from_dense(dense_mat: DMatrixView<'_, T>) -> Self
pub fn from_dense(dense_mat: DMatrixView<'_, T>) -> Self
Creates a CSR matrix from a dense matrix.
Values below the zero threshold are automatically filtered out.
The zero threshold is obtained from the Real trait implementation.
§Arguments
dense_mat- The dense matrix to convert
§Examples
use algebra_sparse::CsrMatrix;
use nalgebra::DMatrix;
let dense = DMatrix::from_row_slice(2, 2, &[1.0, 0.0, 0.0, 2.0]);
let csr = CsrMatrix::from_dense(dense.as_view());Sourcepub fn reset(&mut self, ncols: usize)
pub fn reset(&mut self, ncols: usize)
Resets the CSR matrix to empty state with the given number of columns.
This clears all data and allows reuse of the matrix.
§Arguments
ncols- New number of columns
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears all rows in the CSR matrix while preserving the number of columns.
This removes all rows but keeps the matrix ready for reuse.
Sourcepub fn new_row_builder(&mut self, zero_threshold: T) -> CsVecBuilder<'_, T>
pub fn new_row_builder(&mut self, zero_threshold: T) -> CsVecBuilder<'_, T>
Creates a new row builder for adding elements to the next row.
The builder allows efficient construction of sparse rows. When the builder is dropped, the row is finalized and added to the matrix.
§Arguments
zero_threshold- Values below this threshold are filtered out
§Examples
use algebra_sparse::CsrMatrix;
let mut csr = CsrMatrix::new(3);
let mut builder = csr.new_row_builder(1e-10);
builder.push(0, 1.0); // Add element at column 0
builder.push(2, 2.0); // Add element at column 2
// Row is automatically added when builder is droppedSourcepub fn get_row_mut(&mut self, row_index: usize) -> CsVecMut<'_, T>
pub fn get_row_mut(&mut self, row_index: usize) -> CsVecMut<'_, T>
Sourcepub fn as_view(&self) -> CsrMatrixView<'_, T>
pub fn as_view(&self) -> CsrMatrixView<'_, T>
Returns an immutable view of this CSR matrix.
The view allows efficient read-only access without allocation and can be used for matrix operations.
Trait Implementations§
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> 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<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.