pub struct SparseTensor<T: Scalar> { /* private fields */ }Expand description
An N-dimensional sparse tensor stored in COO (coordinate) format.
Each non-zero element is represented by its coordinates (one index per dimension) and the corresponding value.
§Examples
let st = SparseTensor::new(
vec![vec![0, 1], vec![1, 2]],
vec![3.0_f64, 7.0],
vec![3, 4],
).unwrap();
assert_eq!(st.nnz(), 2);
assert_eq!(st.ndim(), 2);Implementations§
Source§impl<T: Scalar> SparseTensor<T>
impl<T: Scalar> SparseTensor<T>
Sourcepub fn new(
indices: Vec<Vec<usize>>,
values: Vec<T>,
shape: Vec<usize>,
) -> Result<Self>
pub fn new( indices: Vec<Vec<usize>>, values: Vec<T>, shape: Vec<usize>, ) -> Result<Self>
Create a sparse tensor from explicit index lists, values, and shape.
indices must have one entry per dimension, and each inner vec must
have the same length as values. All indices must be in-bounds for the
corresponding dimension of shape.
§Errors
Returns CoreError::InvalidArgument when lengths are inconsistent or
indices are out of bounds.
Sourcepub fn from_dense(tensor: &Tensor<T>) -> Self
pub fn from_dense(tensor: &Tensor<T>) -> Self
Sourcepub fn zeros(shape: Vec<usize>) -> Self
pub fn zeros(shape: Vec<usize>) -> Self
Create an empty sparse tensor (no stored elements) with the given shape.
§Examples
let st = SparseTensor::<f64>::zeros(vec![4, 5]);
assert_eq!(st.nnz(), 0);
assert_eq!(st.shape(), &[4, 5]);Sourcepub fn density(&self) -> f64
pub fn density(&self) -> f64
Fraction of elements that are stored: nnz / total_elements.
Returns 0.0 for a tensor with zero total elements.
Sourcepub fn add(&self, other: &SparseTensor<T>) -> Result<SparseTensor<T>>
pub fn add(&self, other: &SparseTensor<T>) -> Result<SparseTensor<T>>
Element-wise addition of two sparse tensors with the same shape.
The result may contain duplicate indices; call coalesce
to merge them.
§Errors
Returns CoreError::DimensionMismatch if shapes differ.
Sourcepub fn scalar_mul(&self, scalar: T) -> SparseTensor<T>
pub fn scalar_mul(&self, scalar: T) -> SparseTensor<T>
Multiply every stored value by a scalar.
§Examples
let st = SparseTensor::new(
vec![vec![0, 1], vec![0, 1]],
vec![2.0_f64, 3.0],
vec![2, 2],
).unwrap();
let scaled = st.scalar_mul(10.0);
assert_eq!(scaled.values(), &[20.0, 30.0]);Sourcepub fn sparse_matmul(&self, other: &SparseTensor<T>) -> Result<SparseTensor<T>>
pub fn sparse_matmul(&self, other: &SparseTensor<T>) -> Result<SparseTensor<T>>
Sparse-sparse matrix multiplication (2D tensors only).
Computes self @ other where self is (M, K) and other is (K, N).
The result is an (M, N) sparse tensor.
§Errors
Returns an error if either tensor is not 2D or inner dimensions do not match.
Sourcepub fn sparse_dense_matmul(&self, dense: &Tensor<T>) -> Result<Tensor<T>>
pub fn sparse_dense_matmul(&self, dense: &Tensor<T>) -> Result<Tensor<T>>
Sourcepub fn transpose(&self) -> Result<SparseTensor<T>>
pub fn transpose(&self) -> Result<SparseTensor<T>>
Transpose a 2D sparse tensor (swap row and column indices).
§Errors
Returns CoreError::InvalidArgument if the tensor is not 2D.
Trait Implementations§
Source§impl<T: Clone + Scalar> Clone for SparseTensor<T>
impl<T: Clone + Scalar> 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 more