Rust · Sparse Operations

Sparse Operations

use numkong::SparseDot;use numkong::SparseIntersect; 8.0.0-rc.1 · b1f866ab

Sparse helpers cover both sorted-index intersection and weighted sparse dot products.

use numkong::{SparseIntersect, SparseDot};

let a_idx = [1_u32, 3, 5, 7];
let b_idx = [3_u32, 4, 5, 8];
let count = u32::sparse_intersection_size(&a_idx, &b_idx).unwrap();
assert_eq!(count, 2); // indices 3 and 5

let a_weights = [1.0_f32, 2.0, 3.0, 4.0];
let b_weights = [5.0_f32, 6.0, 7.0, 8.0];
let dot = u32::sparse_dot(&a_idx, &b_idx, &a_weights, &b_weights).unwrap();
assert!(dot > 0.0); // weighted dot over shared indices

API

TraitSparseDotComputes sparse dot product between two sorted sparse vectors with weights.3
sparse.rs:257 ↗
pub trait SparseDot: Sized

Each vector consists of sorted indices and corresponding weights. The dot product is computed over the intersection of indices, summing the products of weights.

Errors

Error::KernelFailed when no capability of the current Capabilities has the kernel.

Implementors

WeightWeight type for this sparse dot product.
sparse.rs:259 ↗
type Weight;
OutputOutput type for this sparse dot product.
sparse.rs:261 ↗
type Output;
sparse_dotComputes sparse dot product.
sparse.rs:269 ↗
fn sparse_dot(
    a_indices: &[Self],
    b_indices: &[Self],
    a_weights: &[Self::Weight],
    b_weights: &[Self::Weight],
) -> Result<Self::Output, Error>

Returns the sum of a_weights[i] × b_weights[j] for all pairs where a_indices[i] == b_indices[j]. A sparse vector carries one weight per index, so a weights slice shorter than its index slice describes no vector at all and yields the default rather than reading past its end.

TraitSparseIntersectComputes set operations on sorted sparse vectors.2
sparse.rs:98 ↗
pub trait SparseIntersect: Sized

Errors

Error::KernelFailed when no capability of the current Capabilities has the kernel.

Implementors

sparse_intersection_sizeReturns the intersection size between two sorted sparse vectors.
sparse.rs:100 ↗
fn sparse_intersection_size(a: &[Self], b: &[Self]) -> Result<usize, Error>
sparse_intersect_intoComputes intersection and writes matching elements to output buffer.
sparse.rs:106 ↗
fn sparse_intersect_into(a: &[Self], b: &[Self], result: &mut [Self]) -> Result<usize, Error>

Buffer must be at least min(a.len(), b.len()) in size. Returns the number of elements written, or Error::CapacityExceeded if the buffer is too small.