bsparse.BDIA#
- class BDIA(offsets, data, bshape=None, dtype=None, sizes=None, symmetry=None)#
A sparse matrix in DIAgonal format.
The
BDIA
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, not padded with zeros. Diagonals have varying lengths.
- Parameters:
- offsetsarray_like
The offsets of the block diagonals.
- dataarray_like
The values of the block diagonals.
- bshapetuple[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.
- sizestuple[array_like, array_like], optional
The sizes of the row and column elements. If not given, they are 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
>>> import numpy as np >>> offsets = [0, 1, -1] >>> data = [ ... [np.array([[1, 0], [0, 2]]), np.array([[3, 0], [0, 4]])], ... [np.array([[5, 0], [0, 6]])], ... [np.array([[7, 0], [0, 8]])], ... ] >>> bdia = BDIA(offsets, data) >>> bdia BDIA(bshape=(2, 2), bnnz=4 | shape=(4, 4), nnz=16) >>> bdia.toarray() array([[1, 0, 5, 0], [0, 2, 0, 6], [7, 0, 3, 0], [0, 8, 0, 4]])
- Attributes:
- offsetsndarray
The offsets of the block diagonals.
- datalist of lists of ndarray, scipy.sparse, …
The values of the block diagonals. The inner lists represent the block diagonals, and the inner arrays represent the blocks.
shape
tuple[int, int]The shape of the equivalent dense matrix.
bshape
tuple[int, int]The block shape of the matrix.
row_sizes
ndarrayThe sizes of the row elements.
col_sizes
ndarrayThe sizes of the column elements.
dtype
dtypeThe data type of the matrix elements.
symmetry
strThe symmetry of the matrix.
nnz
intThe number of non-zero elements in the matrix.
bnnz
intThe number of non-zero elements in the matrix.
T
BDIAThe transpose of the matrix.
H
BDIAThe conjugate transpose of the matrix.
Methods
asbformat
(format)Converts the underlying matrix blocks to a given format.
astype
(dtype)Returns a copy of the matrix with a different data type.
bapply
(func[, copy])Applies a function to each matrix block.
conj
()The complex conjugate of the matrix.
The complex conjugate of the matrix.
copy
()Returns a copy of the matrix.
diagonal
([offset])Returns the diagonal of the matrix.
from_array
(arr, sizes[, symmetry])Creates a sparse matrix from a dense
numpy.ndarray
.from_sparray
(mat, sizes[, symmetry])Creates a
BDIA
matrix from ascipy.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.