Sparse matrices (cupyx.scipy.sparse
)#
CuPy supports sparse matrices using cuSPARSE. These matrices have the same interfaces of SciPy’s sparse matrices.
Conversion to/from SciPy sparse matrices#
cupyx.scipy.sparse.*_matrix
and scipy.sparse.*_matrix
are not implicitly convertible to each other.
That means, SciPy functions cannot take cupyx.scipy.sparse.*_matrix
objects as inputs, and vice versa.
To convert SciPy sparse matrices to CuPy, pass it to the constructor of each CuPy sparse matrix class.
To convert CuPy sparse matrices to SciPy, use
get
method of each CuPy sparse matrix class.
Note that converting between CuPy and SciPy incurs data transfer between the host (CPU) device and the GPU device, which is costly in terms of performance.
Conversion to/from CuPy ndarrays#
To convert CuPy ndarray to CuPy sparse matrices, pass it to the constructor of each CuPy sparse matrix class.
To convert CuPy sparse matrices to CuPy ndarray, use
toarray
of each CuPy sparse matrix instance (e.g.,cupyx.scipy.sparse.csr_matrix.toarray()
).
Converting between CuPy ndarray and CuPy sparse matrices does not incur data transfer; it is copied inside the GPU device.
Contents#
Sparse matrix classes#
|
COOrdinate format sparse matrix. |
|
Compressed Sparse Column matrix. |
|
Compressed Sparse Row matrix. |
|
Sparse matrix with DIAgonal storage. |
|
Base class of all sparse matrixes. |
Functions#
Building sparse matrices:
|
Creates a sparse matrix with ones on diagonal. |
|
Creates an identity matrix in sparse format. |
|
Kronecker product of sparse matrices A and B. |
|
Kronecker sum of sparse matrices A and B. |
|
Construct a sparse matrix from diagonals. |
|
Creates a sparse matrix from diagonals. |
|
Returns the lower triangular portion of a matrix in sparse format |
|
Returns the upper triangular portion of a matrix in sparse format |
|
Builds a sparse matrix from sparse sub-blocks |
|
Stacks sparse matrices horizontally (column wise) |
|
Stacks sparse matrices vertically (row wise) |
|
Generates a random sparse matrix. |
|
Generates a random sparse matrix. |
Sparse matrix tools:
|
Returns the indices and values of the nonzero elements of a matrix |
Identifying sparse matrices:
|
Checks if a given matrix is a sparse matrix. |
|
Checks if a given matrix is a sparse matrix. |
Checks if a given matrix is of CSC format. |
|
Checks if a given matrix is of CSR format. |
|
Checks if a given matrix is of COO format. |
|
Checks if a given matrix is of DIA format. |