bsparse.BCSR#
- class BCSR(rowptr, cols, data, bshape=None, dtype=None, sizes=None, symmetry=None)#
A sparse matrix in Compressed Sparse Row format.
The
BCSRclass represents a sparse matrix using three arrays:rowptr: contains the index of the first element of each row.
cols: contains the column indices of each non-zero element.
data: contains the values of each non-zero element.
- Parameters:
- rowptrarray_like
The index of the first element of each row.
- colsarray_like
The column indices of each non-zero element.
- dataarray_like
The values of each non-zero element.
- bshapetuple, optional
The shape of the matrix container. If not given, it is inferred from
indptrandindices.- dtypenumpy.dtype, optional
The data type of the matrix elements. If not given, it is inferred from the data array.
- 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
>>> rowptr = [0, 2, 3, 5] >>> cols = [0, 1, 1, 2, 2] >>> data = [i * np.eye(2) for i in range(1, 6)] >>> bcsr = BCSR(rowptr, cols, data) >>> bcsr BCSR(bshape=(3, 3), bnnz=5 | shape=(6, 6), nnz=20) >>> bcsr.toarray() array([[1., 0., 2., 0., 0., 0.], [0., 1., 0., 2., 0., 0.], [0., 0., 3., 0., 0., 0.], [0., 0., 0., 3., 0., 0.], [0., 0., 0., 0., 5., 0.], [0., 0., 0., 0., 0., 5.]])
- Attributes:
- rowptrndarray
The index of the first element of each row.
- 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.
shapetuple[int, int]The shape of the equivalent dense matrix.
bshapetuple[int, int]The block shape of the matrix.
row_sizesndarrayThe sizes of the row elements.
col_sizesndarrayThe sizes of the column elements.
dtypedtypeThe data type of the matrix elements.
symmetrystrThe symmetry of the matrix.
nnzintThe number of non-zero elements in the matrix.
bnnzintThe number of non-zero elements in the matrix.
TBCSRThe transpose of the matrix.
HBCSRThe 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 sparse matrix from a
scipy.sparse.sparray.save_npz(filename)Saves the matrix as
.npzarchive.toarray()Converts the matrix to a dense
numpy.ndarray.tocoo()Converts the matrix to
BCOOformat.tocsr()Converts the matrix to
BCSRformat.todia()Converts the matrix to
BDIAformat.