CsMatrix

Struct CsMatrix 

Source
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 CSC
  • primary_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,

Source

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 convert
  • row_sparse - If true, creates CSR format; if false, creates CSC format
  • zero_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);
Source

pub fn new(secondary_size: usize) -> Self

Creates a new empty CsMatrix with the given secondary axis size.

§Arguments
  • secondary_size - Number of columns for CSR format or rows for CSC format
§Examples
use algebra_sparse::CsMatrix;

// Create an empty CSR matrix with 3 columns
let csr: CsMatrix<f64> = CsMatrix::new(3);
Source

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

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
Source

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.

Source

pub fn num_primary(&self) -> usize

Returns the number of primary axes (rows for CSR, columns for CSC).

Source

pub fn num_secondary(&self) -> usize

Returns the number of secondary axes (columns for CSR, rows for CSC).

Source

pub fn get_lane(&self, lane_index: usize) -> CsVecRef<'_, T>

Gets a lane (row for CSR, column for CSC) as an immutable sparse vector.

§Arguments
  • lane_index - Index of the lane to retrieve
§Returns

A CsVecRef representing the sparse vector for this lane

Source

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

Gets a lane (row for CSR, column for CSC) as a mutable sparse vector.

§Arguments
  • lane_index - Index of the lane to retrieve
§Returns

A CsVecMut allowing modification of the lane’s values

Source

pub fn as_view(&self) -> CsMatrixView<'_, T>

Returns an immutable view of this matrix.

The view allows efficient read-only access without allocation.

Source

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§

Source§

impl<T: Clone> Clone for CsMatrix<T>

Source§

fn clone(&self) -> CsMatrix<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> Debug for CsMatrix<T>

Source§

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

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

impl<T: Default> Default for CsMatrix<T>

Source§

fn default() -> CsMatrix<T>

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

impl<'a, T> IntoView for &'a CsMatrix<T>

Source§

type View = CsMatrixView<'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 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> 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.