lacuna.sparse#
- class lacuna.sparse.COO(row, col, data, shape, dtype=<class 'numpy.float64'>, check=True)[source]#
Bases:
SparseArrayCoordinate (COO) sparse matrix.
- Parameters:
row (array_like of int64) – Row indices for nonzero entries, length
nnz.col (array_like of int64) – Column indices for nonzero entries, length
nnz.data (array_like of float64) – Nonzero values, length
nnz.dtype (numpy.dtype, optional) – Value dtype, defaults to
np.float64.check (bool, optional) – If True, validate invariants in the native layer (may be slower).
- row, col, data
Storage arrays for indices and values.
- Type:
- dtype#
Value dtype.
- Type:
Notes
Backed by Rust kernels through
lacuna._core.Coo64; operations release the GIL.Examples
Construct a small COO and run basic ops:
>>> import numpy as np >>> from lacuna.sparse import COO >>> row = np.array([0, 1, 1]) >>> col = np.array([0, 0, 2]) >>> val = np.array([1.0, 2.0, 3.0]) >>> a = COO(row, col, val, shape=(2, 3)) >>> a.nnz 3 >>> (a @ np.array([1.0, 0.0, 1.0])).tolist() # SpMV [1.0, 3.0] >>> a.sum() 6.0
- property T#
- classmethod from_arrays(row, col, data, shape, check=True)[source]#
Construct from index/value arrays.
- property nnz#
Number of stored values (including duplicates).
- class lacuna.sparse.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.
- class lacuna.sparse.CSC(indptr, indices, data, shape, dtype=<class 'numpy.float64'>, check=True)[source]#
Bases:
SparseMatrixCompressed Sparse Column (CSC) matrix.
- Parameters:
indptr (array_like of int64, shape
(ncols + 1,)) – Column pointer array.indices (array_like of int64, shape
(nnz,)) – Row indices of nonzero values.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 (may be slower).
- indptr, indices, data
Storage arrays for CSC structure and values.
- Type:
- dtype#
Value dtype.
- Type:
Notes
Backed by Rust kernels through
lacuna._core.Csc64; operations release the GIL.Examples
Construct a small CSC and run basic ops:
>>> import numpy as np >>> from lacuna.sparse import CSC >>> indptr = np.array([0, 1, 2, 3]) >>> indices = np.array([0, 1, 1]) >>> data = np.array([1.0, 2.0, 3.0]) >>> a = CSC(indptr, indices, data, shape=(2, 3)) >>> a.nnz 3 >>> (a @ np.array([1.0, 0.0, 1.0])).tolist() # SpMV [1.0, 3.0] >>> a.sum() 6.0
- property T#
- classmethod from_arrays(indptr, indices, data, shape, check=True)[source]#
Construct from CSC arrays.
- property nnz#
Number of stored values.
- class lacuna.sparse.CSR(indptr, indices, data, shape, dtype=<class 'numpy.float64'>, check=True)[source]#
Bases:
SparseMatrixCompressed Sparse Row (CSR) matrix.
- Parameters:
indptr (array-like of int64, shape (n_rows + 1,)) – Row pointer array. Must be non-decreasing, start at 0, end at nnz.
indices (array-like of int64, shape (nnz,)) – Column indices for each non-zero. Must be strictly increasing within each row.
data (array-like of float64, shape (nnz,)) – Non-zero values.
dtype (numpy.dtype, optional (default: np.float64)) – Data dtype (v0.1 supports float64 only).
check (bool, optional (default: True)) – When True, validate structural invariants using the Rust core.
Notes
The constructor may raise if the structure is invalid when check=True.
Examples
Construct a small CSR and run basic ops:
>>> import numpy as np >>> from lacuna.sparse import CSR >>> indptr = np.array([0, 2, 3]) # 2 rows, 3 nnz >>> indices = np.array([0, 2, 1]) >>> data = np.array([1.0, 3.0, 2.0]) >>> a = CSR(indptr, indices, data, shape=(2, 3)) >>> a.nnz 3 >>> (a @ np.array([1.0, 0.0, 1.0])).tolist() # SpMV [4.0, 2.0] >>> a.sum() 6.0
- property T#
Transpose of the matrix (CSR).
- Returns:
Transposed matrix as a new CSR instance.
- Return type:
- Raises:
RuntimeError – If the native core is not available.
- astype(dtype)[source]#
Return a copy converted to the given dtype.
- Parameters:
dtype (numpy.dtype or str) – Target dtype. v0.1 supports float64 only.
- Returns:
A new CSR with data cast to the requested dtype.
- Return type:
- Raises:
NotImplementedError – If dtype is not float64 in v0.1.
- eliminate_zeros()[source]#
Remove exact zeros from the matrix structure.
- Returns:
New CSR with all zero entries removed.
- Return type:
- Raises:
RuntimeError – If the native core is not available.
- classmethod from_arrays(indptr, indices, data, shape, check=True)[source]#
Construct a CSR from raw arrays.
- Parameters:
indptr – See CSR.__init__.
indices – See CSR.__init__.
data – See CSR.__init__.
shape – See CSR.__init__.
check – See CSR.__init__.
- Returns:
New CSR instance.
- Return type:
- property nnz#
Number of stored non-zero entries (int).
- prune(eps)[source]#
Remove entries with absolute value <= eps.
- Parameters:
eps (float) – Threshold for pruning.
- Returns:
New CSR with pruned entries.
- Return type:
- Raises:
RuntimeError – If the native core is not available.
- sum(axis=None)[source]#
Sum elements along the given axis using the Rust core.
- Parameters:
axis ({None, 0, 1}, optional) – None for total sum (scalar), 0 for column sums (length = ncols), 1 for row sums (length = nrows).
- Returns:
Scalar for total sum, 1D array for axis-specific sums.
- Return type:
- Raises:
RuntimeError – If the native core is not available.
ValueError – If axis is not one of {None, 0, 1}.