Python · Sparse Operations

Sparse Operations

import numkong 8.0.0-rc.1 · b1f866ab

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

import numpy as np
import numkong as nk

idx_a, idx_b = np.array([1, 3, 5, 7], dtype=np.uint32), np.array([3, 4, 5, 8], dtype=np.uint32)
intersection_size = nk.intersect(idx_a, idx_b) # len(np.intersect1d(idx_a, idx_b))
assert intersection_size == 2, "indices 3 and 5"

val_a, val_b = np.array([1.0, 2.0, 3.0, 4.0], dtype=np.float32), np.array([5.0, 6.0, 7.0, 8.0], dtype=np.float32)
sparse_dot = nk.sparse_dot(idx_a, val_a, idx_b, val_b)
assert sparse_dot > 0, "weighted dot over shared indices"

API

Functions

intersectCount the elements two sorted integer arrays share.
__init__.pyi:1380 ↗
def intersect(
    a: _BufferType,
    b: _BufferType,
    /,
    *,
    capabilities: Capability | int | None = ...,
    stream: int | None = ...,
) -> int

Similar to numpy.intersect1d, returning the size of the intersection rather than its elements.

Parameters

capabilitiesCapability | int | NoneCapabilities to run within: every CPU one by default, or for packed operands the mask that packed them.
streamint | NoneA GPU stream pointer, left None on the CPU.
sparse_dotCompute the weighted dot product of two sparse vectors over their shared indices.
__init__.pyi:1387 ↗
def sparse_dot(
    a_indices: _BufferType,
    a_values: _BufferType,
    b_indices: _BufferType,
    b_values: _BufferType,
    /,
    *,
    capabilities: Capability | int | None = ...,
    stream: int | None = ...,
) -> float

Parameters

a_indices_BufferTypeFirst sorted index array, uint16 or uint32.
a_values_BufferTypeWeights of a_indices, bfloat16 or float32.
b_indices_BufferTypeSecond sorted index array, of the dtype of a_indices.
b_values_BufferTypeWeights of b_indices, of the dtype of a_values.
capabilitiesCapability | int | NoneCapabilities to run within: every CPU one by default, or for packed operands the mask that packed them.
streamint | NoneA GPU stream pointer, left None on the CPU.