Sparse matrices

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.

Sparse matrix classes

cupyx.scipy.sparse.coo_matrix

COOrdinate format sparse matrix.

cupyx.scipy.sparse.csc_matrix

Compressed Sparse Column matrix.

cupyx.scipy.sparse.csr_matrix

Compressed Sparse Row matrix.

cupyx.scipy.sparse.dia_matrix

Sparse matrix with DIAgonal storage.

cupyx.scipy.sparse.spmatrix

Base class of all sparse matrixes.

Functions

Building sparse matrices

cupyx.scipy.sparse.bmat

Builds a sparse matrix from sparse sub-blocks

cupyx.scipy.sparse.diags

Construct a sparse matrix from diagonals.

cupyx.scipy.sparse.eye

Creates a sparse matrix with ones on diagonal.

cupyx.scipy.sparse.hstack

Stacks sparse matrices horizontally (column wise)

cupyx.scipy.sparse.identity

Creates an identity matrix in sparse format.

cupyx.scipy.sparse.spdiags

Creates a sparse matrix from diagonals.

cupyx.scipy.sparse.rand

Generates a random sparse matrix.

cupyx.scipy.sparse.random

Generates a random sparse matrix.

cupyx.scipy.sparse.vstack

Stacks sparse matrices vertically (row wise)

Identifying sparse matrices

cupyx.scipy.sparse.issparse

Checks if a given matrix is a sparse matrix.

cupyx.scipy.sparse.isspmatrix

Checks if a given matrix is a sparse matrix.

cupyx.scipy.sparse.isspmatrix_csc

Checks if a given matrix is of CSC format.

cupyx.scipy.sparse.isspmatrix_csr

Checks if a given matrix is of CSR format.

cupyx.scipy.sparse.isspmatrix_coo

Checks if a given matrix is of COO format.

cupyx.scipy.sparse.isspmatrix_dia

Checks if a given matrix is of DIA format.

Linear Algebra

cupyx.scipy.sparse.linalg.lsqr

Solves linear system with QR decomposition.