sparse.dia

Sparse DIAgonal format

Module Contents

Classes

dia_matrix(self,arg1,shape=None,dtype=None,copy=False) Sparse matrix with DIAgonal storage

Functions

isspmatrix_dia(x) Is x of dia_matrix type?
class dia_matrix(arg1, shape=None, dtype=None, copy=False)

Sparse matrix with DIAgonal storage

This can be instantiated in several ways:
dia_matrix(D)
with a dense matrix
dia_matrix(S)
with another sparse matrix S (equivalent to S.todia())
dia_matrix((M, N), [dtype])
to construct an empty matrix with shape (M, N), dtype is optional, defaulting to dtype=’d’.
dia_matrix((data, offsets), shape=(M, N))
where the data[k,:] stores the diagonal entries for diagonal offsets[k] (See example below)
dtype : dtype
Data type of the matrix
shape : 2-tuple
Shape of the matrix
ndim : int
Number of dimensions (this is always 2)
nnz
Number of nonzero elements
data
DIA format data array of the matrix
offsets
DIA format offset array of the matrix

Sparse matrices can be used in arithmetic operations: they support addition, subtraction, multiplication, division, and matrix power.

>>> import numpy as np
>>> from scipy.sparse import dia_matrix
>>> dia_matrix((3, 4), dtype=np.int8).toarray()
array([[0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0]], dtype=int8)
>>> data = np.array([[1, 2, 3, 4]]).repeat(3, axis=0)
>>> offsets = np.array([0, -1, 2])
>>> dia_matrix((data, offsets), shape=(4, 4)).toarray()
array([[1, 0, 3, 0],
       [1, 2, 0, 4],
       [0, 2, 3, 0],
       [0, 0, 3, 4]])
__init__(arg1, shape=None, dtype=None, copy=False)
__repr__()
_data_mask()

Returns a mask of the same shape as self.data, where mask[i,j] is True when data[i,j] corresponds to a stored element.

count_nonzero()
getnnz(axis=None)
sum(axis=None, dtype=None, out=None)
_mul_vector(other)
_mul_multimatrix(other)
_setdiag(values, k=0)
todia(copy=False)
transpose(axes=None, copy=False)
diagonal(k=0)
tocsc(copy=False)
tocoo(copy=False)
_with_data(data, copy=True)

Returns a matrix with the same sparsity structure as self, but with different data. By default the structure arrays are copied.

isspmatrix_dia(x)

Is x of dia_matrix type?

x
object to check for being a dia matrix
bool
True if x is a dia matrix, False otherwise
>>> from scipy.sparse import dia_matrix, isspmatrix_dia
>>> isspmatrix_dia(dia_matrix([[5]]))
True
>>> from scipy.sparse import dia_matrix, csr_matrix, isspmatrix_dia
>>> isspmatrix_dia(csr_matrix([[5]]))
False