COO#
- class brainunit.sparse.COO(args, *, shape, rows_sorted=False, cols_sorted=False)[source]#
Unit-aware Coordinate (COO) sparse matrix.
Stores a 2-D sparse matrix in COO (coordinate) format with optional physical-unit support via
Quantity.- Parameters:
args (
Tuple[Array| saiunit.Quantity,Array,Array]) –datacontains the non-zero values (jax.ArrayorQuantity),rowcontains the row indices, andcolcontains the column indices.shape (
tuple[int,...]) – The(nrows, ncols)shape of the matrix.rows_sorted (
bool) – Whether the row indices are sorted. Default isFalse.cols_sorted (
bool) – Whether the column indices are sorted. Default isFalse.
- row#
Row indices of shape
(nse,).- Type:
jax.Array
- col#
Column indices of shape
(nse,).- Type:
jax.Array
See also
CSRUnit-aware Compressed Sparse Row matrix.
CSCUnit-aware Compressed Sparse Column matrix.
coo_fromdenseCreate a COO matrix from a dense array.
coo_todenseConvert a COO matrix to a dense array.
Notes
This class has minimal compatibility with JAX transforms such as
gradandjit, and offers limited functionality compared tojax.experimental.sparse.BCOO. Additionally, there are known failures whennseis larger than the true number of non-zeros in the represented matrix.Examples
>>> import jax.numpy as jnp >>> import saiunit as u >>> import saiunit.sparse as susparse >>> dense = jnp.array([[1., 0., 2.], [0., 0., 3.]]) >>> coo = susparse.COO.fromdense(dense) >>> coo.shape (2, 3) >>> coo.todense() Array([[1., 0., 2.], [0., 0., 3.]], dtype=float32)
- todense()[source]#
Convert this COO matrix to a dense array.
- Returns:
Dense 2-D array equivalent to this sparse matrix.
- Return type:
Array
Examples
>>> import jax.numpy as jnp >>> import saiunit as u >>> import saiunit.sparse as susparse >>> dense = jnp.array([[0., 3.], [4., 0.]]) >>> coo = susparse.COO.fromdense(dense) >>> coo.todense() Array([[0., 3.], [4., 0.]], dtype=float32)
- with_data(data)[source]#
Create a new COO matrix with the same sparsity structure but different data.
- Parameters:
data (
Array| saiunit.Quantity) – New non-zero values. Must have the same shape, dtype, and unit as the currentself.data.- Returns:
A new COO matrix sharing the same
rowandcolindices but holding the provideddata.- Return type:
Examples
>>> import jax.numpy as jnp >>> import saiunit as u >>> import saiunit.sparse as susparse >>> dense = jnp.array([[1., 0.], [0., 2.]]) >>> coo = susparse.COO.fromdense(dense) >>> new_coo = coo.with_data(coo.data * 5) >>> new_coo.todense() Array([[ 5., 0.], [ 0., 10.]], dtype=float32)