bsparse.sparse.DIA#

class DIA(offsets, data, shape=None, dtype=None, symmetry=None)#

A sparse matrix in DIAgonal format.

The DIA class represents a sparse matrix using two arrays:

  • offsets: the offsets of the diagonals, where zero indicates the main diagonal, positive values are above the main diagonal, and negative values are below the main diagonal.

  • data: the values of the diagonals, padded with zeros so that each diagonal has the same length.

../../_images/dia.jpg
Parameters:
offsetsarray_like

The offsets of the diagonals.

dataarray_like

The values of the diagonals.

shapetuple[int, int], optional

The shape of the matrix. If not given, it is inferred from the offsets and data.

dtypedtype, optional

The data type of the matrix. If not given, it is inferred from the data.

symmetrystr, optional

The symmetry of the matrix. If not given, no symmetry is assumed. This is only applicable for square matrices, where possible values are 'symmetric' and 'hermitian'. Note that when setting a symmetry, the lower triangular part of the matrix is discarded.

Examples

Construct a sparse matrix from dense arrays:

>>> import numpy as np
>>> dia = DIA.from_array(np.eye(3) + np.eye(3, k=1))
>>> dia
DIA(shape=(3, 3), nnz=6, dtype=float64)

Convert the matrix to a dense array:

>>> dia.toarray() 
array([[1., 1., 0.], [0., 1., 1.], [0., 0., 1.]])

Look at the underlying data:

>>> dia.offsets 
array([0, 1])
>>> dia.data 
array([[1., 1., 1.], [0., 1., 1.]])
Attributes:
offsetsndarray

The offsets of the diagonals.

datandarray

The values of the diagonals.

shapetuple[int, int]

The shape of the matrix.

dtypedtype

The data type of the matrix elements.

symmetrystr

The symmetry of the matrix.

nnzint

The number of stored elements in the matrix.

TDIA

The transpose of the matrix.

HDIA

The conjugate transpose of the matrix.

Methods

astype(dtype)

Returns a copy of the matrix with a different data type.

conj()

The complex conjugate of the matrix.

conjugate()

The complex conjugate of the matrix.

copy()

Returns a copy of the matrix.

diagonal([offset])

Returns the diagonal of the matrix.

from_array(arr[, symmetry])

Creates a sparse matrix from a dense numpy.ndarray.

from_sparray(mat[, symmetry])

Creates a sparse matrix from a scipy.sparse.sparray.

save_npz(filename)

Saves the matrix as .npz archive.

toarray()

Returns a dense matrix.

tocoo()

Returns a COOrdinate representation of the matrix.

tocsr()

Returns a Compressed Sparse Row representation of the matrix.

todia()

Returns a DIAgonal representation of the matrix.