CsrMatrix

Struct CsrMatrix 

Source
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 row
  • col_indices: Column indices for each non-zero value
  • row_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,

Source

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());
Source

pub fn new(ncols: usize) -> Self

Creates a new empty CSR matrix with the given number of columns.

§Arguments
  • ncols - Number of columns in the matrix
§Examples
use algebra_sparse::CsrMatrix;

let mut csr: CsrMatrix<f64> = CsrMatrix::new(3);
// Now you can add rows using new_row_builder()
Source

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
Source

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.

Source

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 dropped
Source

pub fn get_row_mut(&mut self, row_index: usize) -> CsVecMut<'_, T>

Gets a mutable row as a sparse vector.

§Arguments
  • row_index - Index of the row to retrieve
§Returns

A CsVecMut allowing modification of the row’s values

§Note

This only allows modification of existing values, not structural changes.

Source

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§

Source§

impl<T: Clone> 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: Default> Default for CsrMatrix<T>

Source§

fn default() -> CsrMatrix<T>

Returns the “default value” for a type. Read more
Source§

impl<'a, T> IntoView for &'a CsrMatrix<T>
where T: Real,

Source§

type View = CsrMatrixView<'a, T>

The view type produced by this trait.
Source§

fn into_view(self) -> Self::View

Converts the type into an immutable view. Read more

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

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
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.