Skip to main content

SparseMatrix

Struct SparseMatrix 

Source
pub struct SparseMatrix { /* private fields */ }
Expand description

A sparse matrix in COO (coordinate) format.

Implementations§

Source§

impl SparseMatrix

Source

pub fn new(n_rows: usize, n_cols: usize) -> Self

Create an empty sparse matrix with the given dimensions.

Source

pub fn from_triplets( rows: Vec<usize>, cols: Vec<usize>, values: Vec<f64>, n_rows: usize, n_cols: usize, ) -> Result<Self>

Create a sparse matrix from triplet vectors.

All three vectors must have the same length, and all indices must be within bounds.

Source

pub fn insert(&mut self, row: usize, col: usize, value: f64) -> Result<()>

Insert a single entry. Returns an error if indices are out of bounds.

Source

pub fn get(&self, row: usize, col: usize) -> f64

Get the value at (row, col). Returns 0.0 if no entry is stored.

This is an O(nnz) scan.

Source

pub fn nnz(&self) -> usize

Number of stored (non-zero) entries.

Source

pub fn density(&self) -> f64

Fraction of entries that are stored: nnz / (n_rows * n_cols).

Source

pub fn shape(&self) -> (usize, usize)

(n_rows, n_cols).

Source

pub fn to_dense(&self) -> Vec<Vec<f64>>

Convert to a dense 2D vector.

Source

pub fn from_dense(data: &[Vec<f64>], threshold: f64) -> Self

Create a sparse matrix from dense data, storing only values where |value| > threshold.

Source

pub fn row_nnz(&self, row: usize) -> usize

Number of stored entries in a given row.

Source

pub fn col_nnz(&self, col: usize) -> usize

Number of stored entries in a given column.

Source

pub fn to_csr(&self) -> (Vec<f64>, Vec<usize>, Vec<usize>)

Convert COO to CSR format.

Returns (data, indices, indptr) where:

  • data contains the non-zero values
  • indices contains the column index for each value
  • indptr[i]..indptr[i+1] gives the range of entries for row i
Source

pub fn from_csr( data: Vec<f64>, indices: Vec<usize>, indptr: Vec<usize>, n_rows: usize, n_cols: usize, ) -> Result<Self>

Create a sparse matrix from CSR format.

  • data — non-zero values
  • indices — column index for each value
  • indptr — row pointer array (length n_rows + 1)
Source

pub fn iter(&self) -> impl Iterator<Item = (usize, usize, f64)> + '_

Iterate over stored triplets (row, col, value).

Source

pub fn column_sums(&self) -> Vec<f64>

Sum of values in each column.

Source

pub fn column_means(&self) -> Vec<f64>

Mean value of each column (dividing by n_rows, treating missing as zero).

Source

pub fn row_sums(&self) -> Vec<f64>

Sum of values in each row.

Source

pub fn scale_rows(&mut self, factors: &[f64])

Multiply each row’s values by the corresponding factor.

factors must have length n_rows.

Source

pub fn map_values(&mut self, f: impl Fn(f64) -> f64)

Apply a function to every stored value in place.

Source

pub fn n_rows(&self) -> usize

Number of rows.

Source

pub fn n_cols(&self) -> usize

Number of columns.

Trait Implementations§

Source§

impl Clone for SparseMatrix

Source§

fn clone(&self) -> SparseMatrix

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 Debug for SparseMatrix

Source§

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

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

impl Summarizable for SparseMatrix

Source§

fn summary(&self) -> String

A one-line summary suitable for display.

Auto Trait Implementations§

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<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.