pub struct SparseTensor<T: TensorElement> { /* private fields */ }Expand description
Sparse tensor in COO (Coordinate) format
COO format stores sparse tensors efficiently by only keeping track of non-zero elements and their coordinates. This is particularly useful for tensors with high sparsity ratios.
§Format
indices: N x D matrix where N is the number of non-zero elements and D is the number of dimensionsvalues: N-length vector containing the non-zero valuesshape: The shape of the full dense tensor
§Example
use torsh_tensor::sparse::SparseTensor;
// Create a 3x3 sparse matrix with values at (0,0)=1.0, (1,2)=2.0, (2,1)=3.0
let indices = vec![vec![0, 0], vec![1, 2], vec![2, 1]];
let values = vec![1.0, 2.0, 3.0];
let shape = vec![3, 3];
let sparse = SparseTensor::from_coo(indices, values, shape).expect("sparse tensor creation should succeed");Implementations§
Source§impl<T: TensorElement> SparseTensor<T>
impl<T: TensorElement> SparseTensor<T>
Sourcepub fn from_coo(
indices: Vec<Vec<usize>>,
values: Vec<T>,
shape: Vec<usize>,
) -> Result<Self>
pub fn from_coo( indices: Vec<Vec<usize>>, values: Vec<T>, shape: Vec<usize>, ) -> Result<Self>
Create a new sparse tensor from COO format
§Arguments
indices- Coordinates of non-zero elements (each inner vector is one coordinate)values- Non-zero values corresponding to the indicesshape- Shape of the full dense tensor
§Returns
A new sparse tensor in COO format
§Errors
Returns error if indices and values have mismatched lengths or if coordinates are out of bounds
Sourcepub fn from_dense(dense: &Tensor<T>, tolerance: T) -> Result<Self>
pub fn from_dense(dense: &Tensor<T>, tolerance: T) -> Result<Self>
Sourcepub fn to_dense(&self) -> Result<Tensor<T>>
pub fn to_dense(&self) -> Result<Tensor<T>>
Convert this sparse tensor to a dense tensor
§Returns
A dense tensor with zeros filled in for missing elements
Sourcepub fn device(&self) -> DeviceType
pub fn device(&self) -> DeviceType
Get the device where the tensor resides
Sourcepub fn memory_usage(&self) -> usize
pub fn memory_usage(&self) -> usize
Get the memory footprint of the sparse representation in bytes
Sourcepub fn memory_efficiency(&self) -> f64
pub fn memory_efficiency(&self) -> f64
Compare memory usage with equivalent dense representation
Sourcepub fn mul(&self, other: &Self) -> Result<Self>
pub fn mul(&self, other: &Self) -> Result<Self>
Element-wise multiplication with another sparse tensor
For sparse tensors, multiplication only produces non-zero results where both tensors have non-zero elements at the same location.
Sourcepub fn mul_scalar(&self, scalar: T) -> Result<Self>
pub fn mul_scalar(&self, scalar: T) -> Result<Self>
Scalar multiplication
Source§impl<T: TensorElement> SparseTensor<T>
Conversion utilities for sparse tensors
impl<T: TensorElement> SparseTensor<T>
Conversion utilities for sparse tensors
Trait Implementations§
Source§impl<T: Clone + TensorElement> Clone for SparseTensor<T>
impl<T: Clone + TensorElement> Clone for SparseTensor<T>
Source§fn clone(&self) -> SparseTensor<T>
fn clone(&self) -> SparseTensor<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl<T> Freeze for SparseTensor<T>
impl<T> RefUnwindSafe for SparseTensor<T>where
T: RefUnwindSafe,
impl<T> Send for SparseTensor<T>
impl<T> Sync for SparseTensor<T>
impl<T> Unpin for SparseTensor<T>where
T: Unpin,
impl<T> UnsafeUnpin for SparseTensor<T>
impl<T> UnwindSafe for SparseTensor<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
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>
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>
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