utilities.sparse

Extra functionality for sparse and scipy.sparse

source

sparse2d

def sparse2d(
    a
):

Create a 2D sparse array in specified format.

arrays = [np.identity(2), sps.eye(2), ss.eye(2), ss.eye(2, format='gcxs')]
[sparse2d(args) for args in arrays]
[<Compressed Sparse Row sparse array of dtype 'float64'
    with 2 stored elements and shape (2, 2)>,
 <Compressed Sparse Row sparse array of dtype 'float64'
    with 2 stored elements and shape (2, 2)>,
 <Compressed Sparse Row sparse array of dtype 'float64'
    with 2 stored elements and shape (2, 2)>,
 <Compressed Sparse Row sparse array of dtype 'float64'
    with 2 stored elements and shape (2, 2)>]

source

sparse

def sparse(
    a, format:str='coo', shape:NoneType=None, *, has_duplicates:bool=True, sorted:bool=False, prune:bool=False,
    cache:bool=False, fill_value:NoneType=None, idx_dtype:NoneType=None
)->sparse.numba_backend._sparse_array.SparseArray:

Create an N-D sparse array in specified format.

arrays = [np.identity(2), sps.eye(2), ss.eye(2), [[[0, 1], [0, 1]], [1., 1.]]]
[sparse(*args) for args in it.product(arrays, ['coo', 'gcxs'])]
[<COO: shape=(2, 2), dtype=float64, nnz=2, fill_value=0.0>,
 <GCXS: shape=(2, 2), dtype=float64, nnz=2, fill_value=0.0, compressed_axes=(np.int64(0),)>,
 <COO: shape=(2, 2), dtype=float64, nnz=2, fill_value=0.0>,
 <GCXS: shape=(2, 2), dtype=float64, nnz=2, fill_value=0.0, compressed_axes=(0,)>,
 <COO: shape=(2, 2), dtype=float64, nnz=2, fill_value=0.0>,
 <GCXS: shape=(2, 2), dtype=float64, nnz=2, fill_value=0.0, compressed_axes=(np.int64(0),)>,
 <COO: shape=(2, 2), dtype=float64, nnz=2, fill_value=0.0>,
 <GCXS: shape=(2, 2), dtype=float64, nnz=2, fill_value=0.0, compressed_axes=(np.int64(0),)>]

source

sparse2d_identity

def sparse2d_identity(
    n
):
sparse2d_identity(2)
<DIAgonal sparse array of dtype 'float64'
    with 2 stored elements (1 diagonals) and shape (2, 2)>

source

sparse_identity

def sparse_identity(
    n, format:str='coo'
)->sparse.numba_backend._sparse_array.SparseArray:
[sparse_identity(2, format=fmt) for fmt in ['coo', 'gcxs']]
[<COO: shape=(2, 2), dtype=float64, nnz=2, fill_value=0.0>,
 <GCXS: shape=(2, 2), dtype=float64, nnz=2, fill_value=0.0, compressed_axes=(np.int64(0),)>]

source

sparse2d_diag

def sparse2d_diag(
    diags, offsets:int=0
):
sparse2d_diag([1, 1])
/var/folders/zd/dnb1yww92mn4qyg7_lww7g9m0000gn/T/ipykernel_33348/2086511523.py:3: FutureWarning: Input has data type int64, but the output has been cast to float64.  In the future, the output data type will match the input. To avoid this warning, set the `dtype` parameter to `None` to have the output dtype match the input, or set it to the desired output data type.
  return sps.diags_array(diags, offsets=offsets)
<DIAgonal sparse array of dtype 'float64'
    with 2 stored elements (1 diagonals) and shape (2, 2)>

source

sparse_diag

def sparse_diag(
    diag, format:str='coo'
)->sparse.numba_backend._sparse_array.SparseArray:
sparse_diag([1, 1])
/var/folders/zd/dnb1yww92mn4qyg7_lww7g9m0000gn/T/ipykernel_33348/2086511523.py:3: FutureWarning: Input has data type int64, but the output has been cast to float64.  In the future, the output data type will match the input. To avoid this warning, set the `dtype` parameter to `None` to have the output dtype match the input, or set it to the desired output data type.
  return sps.diags_array(diags, offsets=offsets)
Format coo
Data Type float64
Shape (2, 2)
nnz 2
Density 0.5
Read-only True
Size 32
Storage ratio 1.00

source

dense_toeplitz

def dense_toeplitz(
    diags:numpy.ndarray, # Value on each diagonal, starting at lower left.
):

Return a toeplitz array given the value on each diagonal (starting at lower left).

dense_toeplitz([1, 2, 3])
array([[2, 3],
       [1, 2]])

source

sparse_toeplitz

def sparse_toeplitz(
    diags:numpy.ndarray, # Value on each diagonal, starting at lower left.
    format:str='coo'
)->sparse.numba_backend._sparse_array.SparseArray:

Return a toeplitz sparse array in specified format given the value on each diagonal (starting at lower left).

sparse_toeplitz([1, 2, 3, 4, 5])
Format coo
Data Type int64
Shape (3, 3)
nnz 9
Density 1.0
Read-only True
Size 216
Storage ratio 3.00
_.todense()
array([[3, 4, 5],
       [2, 3, 4],
       [1, 2, 3]])

source

sparse2d_kronecker_matrix

def sparse2d_kronecker_matrix(
    n, k:int=0
):

The nxn sparse kronecker matrix delta_(i,j-k) in specified format.

sparse2d_kronecker_matrix(3, 2)
<DIAgonal sparse array of dtype 'float64'
    with 1 stored elements (1 diagonals) and shape (3, 3)>
_.todense()
array([[0., 0., 1.],
       [0., 0., 0.],
       [0., 0., 0.]])

source

sparse_kronecker_matrix

def sparse_kronecker_matrix(
    n, k:int=0, format:str='coo'
)->sparse.numba_backend._sparse_array.SparseArray:

The nxn sparse kronecker matrix delta_(i,j-k) in specified format.

sparse_kronecker_matrix(3, 2)
Format coo
Data Type float64
Shape (3, 3)
nnz 1
Density 0.1111111111111111
Read-only True
Size 16
Storage ratio 0.22

source

kron

def kron(
    a, b, *rest
):
kron(np.eye(2), np.eye(2), np.eye(2))
array([[1., 0., 0., 0., 0., 0., 0., 0.],
       [0., 1., 0., 0., 0., 0., 0., 0.],
       [0., 0., 1., 0., 0., 0., 0., 0.],
       [0., 0., 0., 1., 0., 0., 0., 0.],
       [0., 0., 0., 0., 1., 0., 0., 0.],
       [0., 0., 0., 0., 0., 1., 0., 0.],
       [0., 0., 0., 0., 0., 0., 1., 0.],
       [0., 0., 0., 0., 0., 0., 0., 1.]])
kron(*[sparse2d_identity(2)] * 3)
<Compressed Sparse Row sparse array of dtype 'float64'
    with 8 stored elements and shape (8, 8)>
kron(sparse2d_identity(2), np.identity(2), sparse2d_identity(2))
<Compressed Sparse Row sparse array of dtype 'float64'
    with 8 stored elements and shape (8, 8)>
sparse2d_rand([2, 3])
<COOrdinate sparse array of dtype 'float64'
    with 0 stored elements and shape (2, 3)>

source

restrict_bandwidth

def restrict_bandwidth(
    a_sparray:scipy.sparse._base.sparray, width
):

Make a scipy sparse array banded by setting all elements outside the bandwidth to zero.

restrict_bandwidth(sps.csr_array(np.ones((5, 5))), 1)
<Compressed Sparse Row sparse array of dtype 'float64'
    with 13 stored elements and shape (5, 5)>
_.toarray()
array([[1., 1., 0., 0., 0.],
       [1., 1., 1., 0., 0.],
       [0., 1., 1., 1., 0.],
       [0., 0., 1., 1., 1.],
       [0., 0., 0., 1., 1.]])