pub struct SparseCSR<T: TensorElement> { /* private fields */ }Expand description
Sparse tensor in CSR (Compressed Sparse Row) format
CSR format is optimized for row-wise operations and matrix-vector multiplication. It stores:
row_ptr: Array of size (num_rows + 1) indicating where each row startscol_indices: Column indices for each non-zero valuevalues: Non-zero values in row-major order
§Example
use torsh_tensor::sparse::SparseCSR;
// Create a 3x3 sparse matrix in CSR format
// [[1.0, 0.0, 2.0],
// [0.0, 3.0, 0.0],
// [4.0, 0.0, 5.0]]
let row_ptr = vec![0, 2, 3, 5]; // Row pointers
let col_indices = vec![0, 2, 1, 0, 2]; // Column indices
let values = vec![1.0, 2.0, 3.0, 4.0, 5.0]; // Values
let shape = vec![3, 3];
let sparse = SparseCSR::new(row_ptr, col_indices, values, shape).expect("CSR creation should succeed");Implementations§
Source§impl<T: TensorElement> SparseCSR<T>
impl<T: TensorElement> SparseCSR<T>
Sourcepub fn new(
row_ptr: Vec<usize>,
col_indices: Vec<usize>,
values: Vec<T>,
shape: Vec<usize>,
) -> Result<Self>
pub fn new( row_ptr: Vec<usize>, col_indices: Vec<usize>, values: Vec<T>, shape: Vec<usize>, ) -> Result<Self>
Create a new CSR sparse tensor
§Arguments
row_ptr- Row pointers (length: num_rows + 1)col_indices- Column indices for non-zero valuesvalues- Non-zero valuesshape- Shape [num_rows, num_cols]
Sourcepub fn from_coo(coo: &SparseTensor<T>) -> Result<Self>where
T: Copy,
pub fn from_coo(coo: &SparseTensor<T>) -> Result<Self>where
T: Copy,
Convert from COO format to CSR format
Sourcepub fn to_coo(&self) -> Result<SparseTensor<T>>where
T: Copy,
pub fn to_coo(&self) -> Result<SparseTensor<T>>where
T: Copy,
Convert to COO format
Sourcepub fn matvec(&self, vec: &[T]) -> Result<Vec<T>>
pub fn matvec(&self, vec: &[T]) -> Result<Vec<T>>
Matrix-vector multiplication (optimized for CSR)
Sourcepub fn get_row(&self, row: usize) -> Result<(Vec<usize>, Vec<T>)>where
T: Copy,
pub fn get_row(&self, row: usize) -> Result<(Vec<usize>, Vec<T>)>where
T: Copy,
Get a specific row as a sparse vector
pub fn shape(&self) -> &[usize]
pub fn device(&self) -> DeviceType
pub fn row_ptr(&self) -> &[usize]
pub fn col_indices(&self) -> &[usize]
pub fn values(&self) -> &[T]
Trait Implementations§
Auto Trait Implementations§
impl<T> Freeze for SparseCSR<T>
impl<T> RefUnwindSafe for SparseCSR<T>where
T: RefUnwindSafe,
impl<T> Send for SparseCSR<T>
impl<T> Sync for SparseCSR<T>
impl<T> Unpin for SparseCSR<T>where
T: Unpin,
impl<T> UnsafeUnpin for SparseCSR<T>
impl<T> UnwindSafe for SparseCSR<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
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more