utilities.sparse

Extra functionality for sparse and scipy.sparse

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)>]

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
)->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),)>]

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)>

sparse_identity


def sparse_identity(
    n, format:str='coo'
)->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),)>]

sparse2d_diag


def sparse2d_diag(
    diags, offsets:int=0
):
sparse2d_diag([1, 1])
<DIAgonal sparse array of dtype 'float64'
    with 2 stored elements (1 diagonals) and shape (2, 2)>

sparse_diag


def sparse_diag(
    diag, format:str='coo'
)->SparseArray:
sparse_diag([1, 1])
Format coo
Data Type float64
Shape (2, 2)
nnz 2
Density 0.5
Read-only True
Size 32
Storage ratio 1.00

dense_toeplitz


def dense_toeplitz(
    diags: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]])

sparse_toeplitz


def sparse_toeplitz(
    diags:ndarray, # Value on each diagonal, starting at lower left.
    format:str='coo'
)->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]])

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.]])

sparse_kronecker_matrix


def sparse_kronecker_matrix(
    n, k:int=0, format:str='coo'
)->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

kron


def kron(
    a, b, rest:VAR_POSITIONAL
):
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)>

restrict_bandwidth


def restrict_bandwidth(
    a_sparray: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.]])