pub struct SparseMatrix { /* private fields */ }Expand description
A sparse matrix in COO (coordinate) format.
Implementations§
Source§impl SparseMatrix
impl SparseMatrix
Sourcepub fn new(n_rows: usize, n_cols: usize) -> Self
pub fn new(n_rows: usize, n_cols: usize) -> Self
Create an empty sparse matrix with the given dimensions.
Sourcepub fn from_triplets(
rows: Vec<usize>,
cols: Vec<usize>,
values: Vec<f64>,
n_rows: usize,
n_cols: usize,
) -> Result<Self>
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.
Sourcepub fn insert(&mut self, row: usize, col: usize, value: f64) -> Result<()>
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.
Sourcepub fn get(&self, row: usize, col: usize) -> f64
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.
Sourcepub fn from_dense(data: &[Vec<f64>], threshold: f64) -> Self
pub fn from_dense(data: &[Vec<f64>], threshold: f64) -> Self
Create a sparse matrix from dense data, storing only values where |value| > threshold.
Sourcepub fn to_csr(&self) -> (Vec<f64>, Vec<usize>, Vec<usize>)
pub fn to_csr(&self) -> (Vec<f64>, Vec<usize>, Vec<usize>)
Convert COO to CSR format.
Returns (data, indices, indptr) where:
datacontains the non-zero valuesindicescontains the column index for each valueindptr[i]..indptr[i+1]gives the range of entries for rowi
Sourcepub fn from_csr(
data: Vec<f64>,
indices: Vec<usize>,
indptr: Vec<usize>,
n_rows: usize,
n_cols: usize,
) -> Result<Self>
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 valuesindices— column index for each valueindptr— row pointer array (lengthn_rows + 1)
Sourcepub fn iter(&self) -> impl Iterator<Item = (usize, usize, f64)> + '_
pub fn iter(&self) -> impl Iterator<Item = (usize, usize, f64)> + '_
Iterate over stored triplets (row, col, value).
Sourcepub fn column_sums(&self) -> Vec<f64>
pub fn column_sums(&self) -> Vec<f64>
Sum of values in each column.
Sourcepub fn column_means(&self) -> Vec<f64>
pub fn column_means(&self) -> Vec<f64>
Mean value of each column (dividing by n_rows, treating missing as zero).
Sourcepub fn scale_rows(&mut self, factors: &[f64])
pub fn scale_rows(&mut self, factors: &[f64])
Multiply each row’s values by the corresponding factor.
factors must have length n_rows.
Sourcepub fn map_values(&mut self, f: impl Fn(f64) -> f64)
pub fn map_values(&mut self, f: impl Fn(f64) -> f64)
Apply a function to every stored value in place.
Trait Implementations§
Source§impl Clone for SparseMatrix
impl Clone for SparseMatrix
Source§fn clone(&self) -> SparseMatrix
fn clone(&self) -> SparseMatrix
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more