Skip to main content

array2_to_csr

Function array2_to_csr 

Source
pub fn array2_to_csr<T: Scalar + Clone + Field>(arr: &Array2<T>) -> CsrMatrix<T>
Expand description

Converts a dense Array2 to CSR (Compressed Sparse Row) format using exact-zero sparsification.

An entry is dropped only if it compares exactly equal to zero (value == 0). This is the mathematically correct default: a value of 1e-300 is not the same as a mathematical zero, so it is always kept, and the numerical content of the matrix is never silently altered by this conversion.

NaN entries are never silently dropped: because a NaN never compares equal to zero (or to anything else), NaN-containing entries are always treated as non-zero and stored explicitly in the sparse result.

If you instead want tolerance-based (approximate) sparsification - e.g. to prune round-off noise below some magnitude - use array2_to_csr_with_tolerance and pass an explicit tolerance. This crate never applies a silent tolerance-based default.

§Arguments

  • arr - Dense 2D array

§Returns

CSR matrix containing every entry that is not exactly zero

§Example

use ndarray::array;
use oxiblas_ndarray::sparse::array2_to_csr;

let a = array![[1.0, 0.0], [0.0, 2.0]];
let csr = array2_to_csr(&a);
assert_eq!(csr.nnz(), 2);