bsparse.BCOO#
- class BCOO(rows, cols, data, bshape=None, dtype=None, sizes=None, symmetry=None)#
A sparse matrix container in COOrdinate format.
The
BCOO
class represents a sparse matrix using three arrays:rows: contains the row coordinates of the non-zero elements.
cols: contains the column coordinates of the non-zero elements.
data: contains the values of the non-zero elements.
Upon creation, the matrix is sorted (lexicographically) by rows and columns.
Duplicate elements are not allowed.
- Parameters:
- rowsarray_like
The row coordinates of the non-zero elements.
- colsarray_like
The column coordinates of the non-zero elements.
- dataarray_like
The values of the non-zero constituents. This is an array of dtype object. Each element of the array can be either a 2D dense array, a scipy sparse matrix, or any bsparse matrix.
- bshapetuple, optional
The shape of the matrix container. If not given, it is inferred from
rows
andcols
.- dtypedtype, optional
The data type of the matrix elements. If not given, it is inferred from
data
.- sizestuple[np.ndarray, np.ndarray], optional
The sizes of the blocks. If not given, they are inferred from
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 >>> rows = [0, 1, 2, 1, 2, 2] >>> cols = [0, 1, 2, 2, 0, 1] >>> data = [i * np.ones((2, 2)) for i in range(1, 7)] >>> bcoo = BCOO(rows, cols, data) >>> bcoo BCOO(bshape=(3, 3), bnnz=6 | shape=(6, 6), nnz=24) >>> bcoo.toarray() array([[1., 1., 0., 0., 0., 0.], [1., 1., 0., 0., 0., 0.], [0., 0., 2., 2., 4., 4.], [0., 0., 2., 2., 4., 4.], [5., 5., 6., 6., 3., 3.], [5., 5., 6., 6., 3., 3.]])
- Attributes:
- rowsndarray
The row coordinates of the non-zero elements.
- colsndarray
The column coordinates of the non-zero elements.
- datalist of ndarray, scipy.sparse, sparse.Sparse, or BSparse
The values of the non-zero elements.
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
BCOOThe transpose of the matrix.
H
BCOOThe 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 block diagonal of the matrix.
from_array
(arr, sizes[, symmetry])Creates a
BCOO
matrix from a densenumpy.ndarray
.from_sparray
(mat, sizes[, symmetry])Creates a
BCOO
matrix from ascipy.sparse.sparray
.save_npz
(filename)Saves the matrix to a numpy.npz file.
toarray
()Converts the matrix to a dense
numpy.ndarray
.tocoo
()Converts the matrix to
BCOO
format.tocsr
()Converts the matrix to
BCSR
format.todia
()Converts the matrix to
BDIA
format.