bsparse.sparse.COO#
- class COO(rows, cols, data, shape=None, dtype=None, symmetry=None)#
A sparse matrix in coordinate format.
The
COO
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.
During instantiation, 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 elements.
- shapetuple, optional
The shape of the matrix. If not given, it is inferred from the row and column indices.
- dtypenumpy.dtype, optional
The data type of the matrix elements. If not given, it is inferred from the data array.
- 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
>>> rows = [0, 1, 2, 2, 3] >>> cols = [1, 2, 2, 3, 4] >>> data = [1, 2, 3, 4, 5] >>> coo = COO(rows, cols, data, shape=(4, 5)) >>> coo.toarray() array([[0, 1, 0, 0, 0], [0, 0, 2, 0, 0], [0, 0, 3, 4, 0], [0, 0, 0, 0, 5]])
- Attributes:
- rowsndarray
The row coordinates of the non-zero elements.
- colsndarray
The column coordinates of the non-zero elements.
- datandarray
The values of the diagonals.
shape
tuple[int, int]The shape of the matrix.
dtype
dtypeThe data type of the matrix elements.
symmetry
strThe symmetry of the matrix.
nnz
intThe number of stored elements in the matrix.
T
COOThe transpose of the matrix.
H
COOThe 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.
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
()Converts the matrix to a dense
numpy.ndarray
.tocoo
()Converts the matrix to coordinate storage.
tocsr
()Converts the matrix to compressed row storage.
todia
()Converts the matrix to diagonal storage.