lacuna.sparse.coond#
N-dimensional COO sparse array (COOND) backed by a Rust core.
This module exposes the COOND class, a Pythonic façade over ND-COO kernels
implemented in Rust and bound via PyO3. Indices are stored as int64 and
flattened to length nnz * ndim (or accepted as a 2D (nnz, ndim) array),
and values are float64 in v0.1.
Notes
Operations release the GIL and call from-parts bindings in
lacuna._core.Duplicates in indices are allowed; aggregation is performed by operations (e.g., reductions or toarray materialization).
When
check=True, basic invariants are validated via the native layer.
Contents#
COOND: construction, sum/mean, axis reductions/permute, reshape, broadcasting Hadamard, unfolding to CSR/CSC.
Classes
|
N-dimensional sparse array in COO format. |
- class lacuna.sparse.coond.COOND(shape, indices, data, dtype=<class 'numpy.float64'>, check=True)[source]#
Bases:
SparseArrayN-dimensional sparse array in COO format.
- Parameters:
shape (tuple[int, ...]) – Overall tensor shape of length
ndim.indices (array_like of int64, shape
(nnz * ndim,)or(nnz, ndim)) – Concatenated per-nnz indices or a 2D array; values must be within bounds ofshape. Duplicates are allowed (aggregated by ops).data (array_like of float64, shape
(nnz,)) – Nonzero values.dtype (numpy.dtype, optional) – Value dtype, defaults to
np.float64.check (bool, optional) – If True, validate invariants in the native layer where applicable.
- indices#
Flattened indices of length
nnz * ndim.- Type:
numpy.ndarray (int64)
- data#
Nonzero values of length
nnz.- Type:
numpy.ndarray (float64)
Notes
Backed by Rust ND-COO kernels via
lacuna._core. Operations release the GIL. Key operations includesum,mean, axis reductions/permutations,reshape, broadcasting Hadamard multiply, and unfolding to CSR/CSC.Examples
Construct a small 3D tensor and run basic ops:
>>> import numpy as np >>> from lacuna.sparse import COOND >>> shape = (2, 3, 4) >>> # 2 nonzeros at positions (0,1,2) and (1,2,3) >>> idx = np.array([ ... 0, 1, 2, ... 1, 2, 3, ... ], dtype=np.int64) >>> val = np.array([1.0, 3.0]) >>> a = COOND(shape, idx, val) >>> a.nnz 2 >>> a.sum() 4.0 >>> a.reduce_sum_axes([2]).shape # sum over last axis (2, 3) >>> a.permute_axes([2, 1, 0]).shape (4, 3, 2) >>> b = a.reshape((3, 2, 4)) >>> (a.hadamard_broadcast(b)).nnz 2 >>> a.mode_unfold_to_csr(axis=0).shape (2, 12)
- axes_unfold_to_csc(row_axes)[source]#
Unfold by grouping selected axes as CSC rows (the rest become columns).
- axes_unfold_to_csr(row_axes)[source]#
Unfold by grouping selected axes as CSR rows and the rest as columns.
- property nnz#
Number of stored values.