brainevent.binary_csrmv#
- brainevent.binary_csrmv(data, indices, indptr, v, *, shape, transpose=False, backend=None, weighted=False, max_row_nnz=None, workspace=None)[source]#
Product of a CSR sparse matrix and a dense vector using event-driven (binary) computation.
Computes
y = A @ v(ory = A.T @ vwhentranspose=True) whereAis stored in Compressed Sparse Row format andvis treated as a binary event vector. Elements ofvthat areTrue(boolean) or non-zero (numeric, including negative values and NaN) are considered active events; only those contribute to the result, enabling efficient event-driven sparse–dense products commonly used in spiking neural networks.The function supports physical units via
brainunit. Ifdataorvcarry units, the result is returned in the corresponding product unit.- Parameters:
data (
Array|ndarray|Quantity|Number) – Non-zero weight values of the CSR matrix. Shape(nse,)for heterogeneous weights or(1,)for a single homogeneous weight shared across all connections.indices (
Array|ndarray) – Column indices of the non-zero elements. Shape(nse,)with integer dtype (int32,int64,uint32, oruint64).indptr (
Array|ndarray) – Row index pointer array. Shape(shape[0] + 1,)and same dtype asindices.indptr[i]andindptr[i+1]delimit the non-zero entries of rowi.v (
Array|ndarray|Quantity|Number) – Dense event vector. Shape(shape[0],)whentranspose=Trueor(shape[1],)whentranspose=False. Dtype may be boolean (events indicated byTrue) or numeric (events indicated by non-zero values, including negative values and NaN).shape (
Tuple[int,int]) – Two-element tuple(m, k)giving the logical shape of the sparse matrixA.transpose (
bool) – IfTrue, the sparse matrix is transposed before multiplication, i.e. computeA.T @ v. Default isFalse.backend (
str|None) – Compute backend to use. One of'numba','pallas', orNone(auto-select). Default isNone.weighted (
bool) – IfTrue,vholds value-weighted events (WeightedArray): active entries (v != 0) multiply the weights by their value,e(v[j]) = v[j], so the result is the exact productA @ v. Default isFalse(binary events).max_row_nnz (
int|None) – Upper bound on the number of stored entries in any row ofindptr. Lets thecuda_rawscatter kernel size its task queue exactly;None(default) uses a bound from the array sizes alone. A value below the true longest row raisesValueErrorwhenindptris concrete and gives wrong results oncuda_rawunder a trace.workspace (optional) – Deprecated and ignored; passing it emits a
DeprecationWarning.
- Returns:
y – Result vector. Shape
(shape[1],)whentranspose=Trueor(shape[0],)whentranspose=False.- Return type:
Array|ndarray|Quantity|Number
See also
binary_csrmmBinary CSR matrix–matrix multiplication.
csrmvStandard (non-event-driven) CSR matrix–vector multiplication.
Notes
This operation is event-driven: instead of performing a full sparse–dense product, it skips columns (or rows, when transposed) for which the corresponding entry in
vis inactive (Falseor0; negative values and NaN are active). This yields significant speed-ups when the event vector is sparse. Inactive events contribute exactly0on every backend, even when the weights they would multiply are NaN or +-Inf, and the weight cotangent of such entries is exactly0.Mathematically, the non-transposed operation computes:
y[i] = sum_{j in nz(i)} A[i, j] * e(v[j])where
nz(i)denotes the set of column indices with non-zero entries in rowi, ande(v[j])is the event indicator:e(v[j]) = 1 if v[j] is True (bool) or v[j] != 0 (numeric)e(v[j]) = 0 otherwiseWhen
transpose=True, the transposed operation computes:y[j] = sum_{i in nz_col(j)} A[i, j] * e(v[i])where
nz_col(j)denotes the set of row indices with non-zero entries in columnj.For homogeneous weights (
dataof shape(1,)),A[i, j]is the constantdata[0]for all non-zero positions.The operation is differentiable with respect to both
dataandvvia custom JVP and transpose rules.References
Examples
>>> import jax.numpy as jnp >>> from brainevent import binary_csrmv >>> data = jnp.array([0.5]) # homogeneous weight >>> indices = jnp.array([0, 2, 1, 2], dtype=jnp.int32) >>> indptr = jnp.array([0, 2, 4], dtype=jnp.int32) >>> v = jnp.array([True, False, True]) # binary event vector >>> binary_csrmv(data, indices, indptr, v, shape=(2, 3)) Array([1. , 0.5], dtype=float32)