lacuna.sparse.coo#

Classes

COO(row, col, data, shape[, dtype, check])

Coordinate (COO) sparse matrix.

class lacuna.sparse.coo.COO(row, col, data, shape, dtype=<class 'numpy.float64'>, check=True)[source]#

Bases: SparseArray

Coordinate (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.

  • shape (tuple of int) – Matrix shape (nrows, ncols).

  • 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:

numpy.ndarray

shape#

Matrix dimensions.

Type:

tuple[int, int]

dtype#

Value dtype.

Type:

numpy.dtype

nnz#

Number of stored elements (with duplicates allowed).

Type:

int

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#
eliminate_zeros()[source]#

Remove explicit zeros. Returns a new COO.

classmethod from_arrays(row, col, data, shape, check=True)[source]#

Construct from index/value arrays.

Parameters:
  • row (array_like) – Coordinate indices and values.

  • col (array_like) – Coordinate indices and values.

  • data (array_like) – Coordinate indices and values.

  • shape (tuple[int, int]) – Matrix shape.

  • check (bool, optional) – Validate invariants in the native layer.

property nnz#

Number of stored values (including duplicates).

prune(eps)[source]#

Drop entries with abs(value) <= eps.

Returns a new COO.

sum(axis=None)[source]#

Sum of entries.

Parameters:

axis ({None, 0, 1}, optional) – None for global sum; 0 for column sums; 1 for row sums.

toarray()[source]#

Convert to a dense NumPy ndarray of shape (nrows, ncols).