saiunit.sparse.csr_fromdense

Contents

saiunit.sparse.csr_fromdense#

saiunit.sparse.csr_fromdense(mat, *, nse=None, index_dtype=<class 'numpy.int32'>)[source]#

Create a CSR-format sparse matrix from a dense matrix.

Parameters:
  • mat (Array | saiunit.Quantity) – Dense 2-D array to be converted to CSR format.

  • nse (int | None) – Number of specified (non-zero) entries in mat. If None (default), it is computed automatically from the input matrix.

  • index_dtype (str | type[Any] | dtype | SupportsDType) – Data type for the sparse index arrays. Default is numpy.int32.

Returns:

The CSR representation of the input matrix.

Return type:

CSR

Examples

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