CSC#

class brainunit.sparse.CSC(args, *, shape)[source]#

Unit-aware Compressed Sparse Column (CSC) matrix.

Stores a 2-D sparse matrix in CSC format with optional physical-unit support via Quantity.

Parameters:
  • args (tuple of (data, indices, indptr)) – data contains the non-zero values (jax.Array or Quantity), indices contains the row indices, and indptr contains the column pointer array.

  • shape (tuple of int) – The (nrows, ncols) shape of the matrix.

data#

Non-zero values of shape (nse,).

Type:

jax.Array or Quantity

indices#

Row indices of shape (nse,).

Type:

jax.Array

indptr#

Column pointer array of shape (ncols + 1,).

Type:

jax.Array

shape#

Shape of the matrix (nrows, ncols).

Type:

tuple of int

nse#

Number of stored elements.

Type:

int

dtype#

Data type of the stored values.

Type:

dtype

See also

CSR

Unit-aware Compressed Sparse Row matrix.

csc_fromdense

Create a CSC matrix from a dense array.

csc_todense

Convert a CSC matrix to a dense array.

Examples

>>> import jax.numpy as jnp
>>> import saiunit as u
>>> import saiunit.sparse as susparse
>>> dense = jnp.array([[1., 0., 2.], [0., 0., 3.]])
>>> csc = susparse.CSC.fromdense(dense)
>>> csc.shape
(2, 3)
>>> csc.todense()
Array([[1., 0., 2.],
       [0., 0., 3.]], dtype=float32)
todense()[source]#

Convert this CSC matrix to a dense array.

Returns:

Dense 2-D array equivalent to this sparse matrix.

Return type:

jax.Array or Quantity

Examples

>>> import jax.numpy as jnp
>>> import saiunit as u
>>> import saiunit.sparse as susparse
>>> dense = jnp.array([[0., 3.], [4., 0.]])
>>> csc = susparse.CSC.fromdense(dense)
>>> csc.todense()
Array([[0., 3.],
       [4., 0.]], dtype=float32)
transpose(axes=None)[source]#

Return the transpose of this CSC matrix as a CSR matrix.

Parameters:

axes (None, optional) – Not supported. Must be None.

Returns:

The transposed matrix in CSR format.

Return type:

CSR

Raises:

NotImplementedError – If axes is not None.

with_data(data)[source]#

Create a new CSC 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 current self.data.

Returns:

A new CSC matrix sharing the same indices and indptr but holding the provided data.

Return type:

CSC

Examples

>>> import jax.numpy as jnp
>>> import saiunit as u
>>> import saiunit.sparse as susparse
>>> dense = jnp.array([[1., 0.], [0., 2.]])
>>> csc = susparse.CSC.fromdense(dense)
>>> new_csc = csc.with_data(csc.data * 5)
>>> new_csc.todense()
Array([[ 5., 0.],
       [ 0., 10.]], dtype=float32)