Skip to main content

SparseArray

Trait SparseArray 

Source
pub trait SparseArray<T>: Any
where T: SparseElement + Div<Output = T> + 'static,
{
Show 34 methods // Required methods fn shape(&self) -> (usize, usize); fn nnz(&self) -> usize; fn dtype(&self) -> &str; fn to_array(&self) -> Array2<T>; fn toarray(&self) -> Array2<T>; fn to_coo(&self) -> SparseResult<Box<dyn SparseArray<T>>>; fn to_csr(&self) -> SparseResult<Box<dyn SparseArray<T>>>; fn to_csc(&self) -> SparseResult<Box<dyn SparseArray<T>>>; fn to_dok(&self) -> SparseResult<Box<dyn SparseArray<T>>>; fn to_lil(&self) -> SparseResult<Box<dyn SparseArray<T>>>; fn to_dia(&self) -> SparseResult<Box<dyn SparseArray<T>>>; fn to_bsr(&self) -> SparseResult<Box<dyn SparseArray<T>>>; fn add( &self, other: &dyn SparseArray<T>, ) -> SparseResult<Box<dyn SparseArray<T>>>; fn sub( &self, other: &dyn SparseArray<T>, ) -> SparseResult<Box<dyn SparseArray<T>>>; fn mul( &self, other: &dyn SparseArray<T>, ) -> SparseResult<Box<dyn SparseArray<T>>>; fn div( &self, other: &dyn SparseArray<T>, ) -> SparseResult<Box<dyn SparseArray<T>>>; fn dot( &self, other: &dyn SparseArray<T>, ) -> SparseResult<Box<dyn SparseArray<T>>>; fn dot_vector(&self, other: &ArrayView1<'_, T>) -> SparseResult<Array1<T>>; fn transpose(&self) -> SparseResult<Box<dyn SparseArray<T>>>; fn copy(&self) -> Box<dyn SparseArray<T>>; fn get(&self, i: usize, j: usize) -> T; fn set(&mut self, i: usize, j: usize, value: T) -> SparseResult<()>; fn eliminate_zeros(&mut self); fn sort_indices(&mut self); fn sorted_indices(&self) -> Box<dyn SparseArray<T>>; fn has_sorted_indices(&self) -> bool; fn sum(&self, axis: Option<usize>) -> SparseResult<SparseSum<T>>; fn max(&self) -> T; fn min(&self) -> T; fn find(&self) -> (Array1<usize>, Array1<usize>, Array1<T>); fn slice( &self, row_range: (usize, usize), col_range: (usize, usize), ) -> SparseResult<Box<dyn SparseArray<T>>>; fn as_any(&self) -> &dyn Any; // Provided methods fn get_indptr(&self) -> Option<&Array1<usize>> { ... } fn indptr(&self) -> Option<&Array1<usize>> { ... }
}
Expand description

Trait for sparse array types.

This trait defines the common interface for all sparse array implementations. It is designed to align with SciPy’s sparse array API, providing array-like semantics rather than matrix-like semantics.

§Notes

The sparse array API differs from the sparse matrix API in the following ways:

  • * operator performs element-wise multiplication, not matrix multiplication
  • Matrix multiplication is done with the dot method or @ operator in Python
  • Operations like sum produce arrays, not matrices
  • Sparse arrays use array-style slicing operations

§Compatibility

This trait now supports both integer and floating-point element types through the SparseElement trait, enabling use cases like graph adjacency matrices (integers) and numerical computation (floats).

Required Methods§

Source

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

Returns the shape of the sparse array.

Source

fn nnz(&self) -> usize

Returns the number of stored (non-zero) elements.

Source

fn dtype(&self) -> &str

Returns the data type of the sparse array.

Source

fn to_array(&self) -> Array2<T>

Returns a view of the sparse array as a dense ndarray.

Source

fn toarray(&self) -> Array2<T>

Returns a dense copy of the sparse array.

Source

fn to_coo(&self) -> SparseResult<Box<dyn SparseArray<T>>>

Returns a sparse array in COO format.

Source

fn to_csr(&self) -> SparseResult<Box<dyn SparseArray<T>>>

Returns a sparse array in CSR format.

Source

fn to_csc(&self) -> SparseResult<Box<dyn SparseArray<T>>>

Returns a sparse array in CSC format.

Source

fn to_dok(&self) -> SparseResult<Box<dyn SparseArray<T>>>

Returns a sparse array in DOK format.

Source

fn to_lil(&self) -> SparseResult<Box<dyn SparseArray<T>>>

Returns a sparse array in LIL format.

Source

fn to_dia(&self) -> SparseResult<Box<dyn SparseArray<T>>>

Returns a sparse array in DIA format.

Source

fn to_bsr(&self) -> SparseResult<Box<dyn SparseArray<T>>>

Returns a sparse array in BSR format.

Source

fn add( &self, other: &dyn SparseArray<T>, ) -> SparseResult<Box<dyn SparseArray<T>>>

Element-wise addition.

Source

fn sub( &self, other: &dyn SparseArray<T>, ) -> SparseResult<Box<dyn SparseArray<T>>>

Element-wise subtraction.

Source

fn mul( &self, other: &dyn SparseArray<T>, ) -> SparseResult<Box<dyn SparseArray<T>>>

Element-wise multiplication.

Source

fn div( &self, other: &dyn SparseArray<T>, ) -> SparseResult<Box<dyn SparseArray<T>>>

Element-wise division.

Source

fn dot( &self, other: &dyn SparseArray<T>, ) -> SparseResult<Box<dyn SparseArray<T>>>

Matrix multiplication.

Source

fn dot_vector(&self, other: &ArrayView1<'_, T>) -> SparseResult<Array1<T>>

Matrix-vector multiplication.

Source

fn transpose(&self) -> SparseResult<Box<dyn SparseArray<T>>>

Transpose the sparse array.

Source

fn copy(&self) -> Box<dyn SparseArray<T>>

Return a copy of the sparse array with the specified elements.

Source

fn get(&self, i: usize, j: usize) -> T

Get a value at the specified position.

Source

fn set(&mut self, i: usize, j: usize, value: T) -> SparseResult<()>

Set a value at the specified position.

Source

fn eliminate_zeros(&mut self)

Eliminate zeros from the sparse array.

Source

fn sort_indices(&mut self)

Sort indices of the sparse array.

Source

fn sorted_indices(&self) -> Box<dyn SparseArray<T>>

Return a sorted copy of this sparse array.

Source

fn has_sorted_indices(&self) -> bool

Check if indices are sorted.

Source

fn sum(&self, axis: Option<usize>) -> SparseResult<SparseSum<T>>

Sum the sparse array elements.

Parameters:

  • axis: The axis along which to sum. If None, sum over both axes.

Returns a sparse array if summing over a single axis, or a scalar if summing over both axes.

Source

fn max(&self) -> T

Compute the maximum value of the sparse array elements.

Source

fn min(&self) -> T

Compute the minimum value of the sparse array elements.

Source

fn find(&self) -> (Array1<usize>, Array1<usize>, Array1<T>)

Return the indices and values of the nonzero elements.

Source

fn slice( &self, row_range: (usize, usize), col_range: (usize, usize), ) -> SparseResult<Box<dyn SparseArray<T>>>

Return a slice of the sparse array.

Source

fn as_any(&self) -> &dyn Any

Returns the concrete type of the array for downcasting.

Provided Methods§

Source

fn get_indptr(&self) -> Option<&Array1<usize>>

Returns the indptr array for CSR/CSC formats. For formats that don’t have indptr, returns None.

Source

fn indptr(&self) -> Option<&Array1<usize>>

Returns the indptr array for CSR/CSC formats. For formats that don’t have indptr, returns None.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl<T> SparseArray<T> for BandedArray<T>
where T: Float + SparseElement + Debug + Display + Copy + Zero + One + Send + Sync + 'static + AddAssign,

Source§

impl<T> SparseArray<T> for BsrArray<T>
where T: SparseElement + Div<Output = T> + Float + 'static + AddAssign,

Source§

impl<T> SparseArray<T> for CooArray<T>
where T: SparseElement + Div<Output = T> + PartialOrd + Zero + 'static,

Source§

impl<T> SparseArray<T> for CscArray<T>
where T: SparseElement + Div<Output = T> + Float + 'static,

Source§

impl<T> SparseArray<T> for CsrArray<T>
where T: SparseElement + Div<Output = T> + PartialOrd + Zero + 'static,

Source§

impl<T> SparseArray<T> for DiaArray<T>
where T: SparseElement + Div<Output = T> + Float + 'static + AddAssign,

Source§

impl<T> SparseArray<T> for DokArray<T>
where T: SparseElement + Div<Output = T> + Float + PartialOrd + 'static,

Source§

impl<T> SparseArray<T> for LilArray<T>
where T: SparseElement + Div<Output = T> + Float + 'static,

Source§

impl<T> SparseArray<T> for SparseArrayBase<T>
where T: SparseElement + Div<Output = T> + PartialOrd + Zero + 'static,

Source§

impl<T> SparseArray<T> for SymCooArray<T>
where T: Float + SparseElement + Debug + Copy + 'static + Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Div<Output = T> + SimdUnifiedOps + Send + Sync,

Implementation of SparseArray for SymCooArray

Source§

impl<T> SparseArray<T> for SymCsrArray<T>
where T: Float + SparseElement + Debug + Copy + 'static + Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Div<Output = T> + SimdUnifiedOps + Send + Sync,

Implementation of SparseArray for SymCsrArray