pub struct CsMatrix<T> { /* private fields */ }Expand description
Compressed Sparse Matrix in either CSR or CSC format.
This is the core sparse matrix structure that can represent either:
- CSR (Compressed Sparse Row) format when rows are compressed
- CSC (Compressed Sparse Column) format when columns are compressed
The matrix stores only non-zero elements and their indices, making it memory-efficient for matrices with few non-zero elements.
§Format
The matrix uses three internal arrays:
secondary_indices: Column indices for CSR or row indices for CSCprimary_offsets: Offsets for each row (CSR) or column (CSC)values: Non-zero values stored in row-major (CSR) or column-major (CSC) order
§Examples
use algebra_sparse::{CsMatrix, 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 sparse = CsrMatrix::from_dense(dense.as_view());Implementations§
Source§impl<T> CsMatrix<T>where
T: Real,
impl<T> CsMatrix<T>where
T: Real,
Sourcepub fn from_dense(
dense_mat: DMatrixView<'_, T>,
row_sparse: bool,
zero_threshold: T,
) -> Self
pub fn from_dense( dense_mat: DMatrixView<'_, T>, row_sparse: bool, zero_threshold: T, ) -> Self
Create a CsMatrix from a dense matrix.
§Arguments
dense_mat- The dense matrix to convertrow_sparse- If true, creates CSR format; if false, creates CSC formatzero_threshold- Values below this threshold are treated as zero and not stored
§Examples
use algebra_sparse::CsMatrix;
use nalgebra::DMatrix;
let dense = DMatrix::from_row_slice(2, 3, &[
1.0, 0.0, 2.0,
0.0, 3.0, 0.0,
]);
// Create CSR format
let csr = CsMatrix::from_dense(dense.as_view(), true, 1e-10);
// Create CSC format
let csc = CsMatrix::from_dense(dense.as_view(), false, 1e-10);Sourcepub fn new_lane_builder(&mut self, zero_threshold: T) -> CsVecBuilder<'_, T>
pub fn new_lane_builder(&mut self, zero_threshold: T) -> CsVecBuilder<'_, T>
Creates a new lane (row or column) builder for this matrix.
Returns a CsVecBuilder that can be used to efficiently add non-zero elements
to the next lane of the matrix. When the builder is dropped, the lane is finalized.
§Arguments
zero_threshold- Values below this threshold are filtered out
§Examples
use algebra_sparse::CsMatrix;
let mut matrix = CsMatrix::new(3);
let mut builder = matrix.new_lane_builder(1e-10);
builder.push(0, 1.0); // Add element at column 0
builder.push(2, 2.0); // Add element at column 2Sourcepub fn reset(&mut self, secondary_size: usize)
pub fn reset(&mut self, secondary_size: usize)
Resets the matrix to empty state with a new secondary axis size.
This clears all data and allows reuse of the matrix with different dimensions.
§Arguments
secondary_size- New secondary axis size
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears all data from the matrix while preserving the secondary axis size.
This removes all lanes but keeps the matrix ready for reuse with the same dimensions.
Sourcepub fn num_primary(&self) -> usize
pub fn num_primary(&self) -> usize
Returns the number of primary axes (rows for CSR, columns for CSC).
Sourcepub fn num_secondary(&self) -> usize
pub fn num_secondary(&self) -> usize
Returns the number of secondary axes (columns for CSR, rows for CSC).
Sourcepub fn get_lane_mut(&mut self, lane_index: usize) -> CsVecMut<'_, T>
pub fn get_lane_mut(&mut self, lane_index: usize) -> CsVecMut<'_, T>
Sourcepub fn as_view(&self) -> CsMatrixView<'_, T>
pub fn as_view(&self) -> CsMatrixView<'_, T>
Returns an immutable view of this matrix.
The view allows efficient read-only access without allocation.
Sourcepub fn dense_rate(&self) -> f32
pub fn dense_rate(&self) -> f32
Returns the density rate of the matrix.
This is the ratio of non-zero elements to total elements. A value of 0.1 means 10% of elements are non-zero.
Trait Implementations§
Auto Trait Implementations§
impl<T> Freeze for CsMatrix<T>
impl<T> RefUnwindSafe for CsMatrix<T>where
T: RefUnwindSafe,
impl<T> Send for CsMatrix<T>where
T: Send,
impl<T> Sync for CsMatrix<T>where
T: Sync,
impl<T> Unpin for CsMatrix<T>where
T: Unpin,
impl<T> UnwindSafe for CsMatrix<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.