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#

coo_matrix(arg1[, shape, dtype, copy])

COOrdinate format sparse matrix.

csc_matrix(arg1[, shape, dtype, copy])

Compressed Sparse Column matrix.

csr_matrix(arg1[, shape, dtype, copy])

Compressed Sparse Row matrix.

dia_matrix(arg1[, shape, dtype, copy])

Sparse matrix with DIAgonal storage.

spmatrix([maxprint])

Base class of all sparse matrixes.

Functions#

Building sparse matrices:

eye(m[, n, k, dtype, format])

Creates a sparse matrix with ones on diagonal.

identity(n[, dtype, format])

Creates an identity matrix in sparse format.

kron(A, B[, format])

Kronecker product of sparse matrices A and B.

kronsum(A, B[, format])

Kronecker sum of sparse matrices A and B.

diags(diagonals[, offsets, shape, format, dtype])

Construct a sparse matrix from diagonals.

spdiags(data, diags, m, n[, format])

Creates a sparse matrix from diagonals.

tril(A[, k, format])

Returns the lower triangular portion of a matrix in sparse format

triu(A[, k, format])

Returns the upper triangular portion of a matrix in sparse format

bmat(blocks[, format, dtype])

Builds a sparse matrix from sparse sub-blocks

hstack(blocks[, format, dtype])

Stacks sparse matrices horizontally (column wise)

vstack(blocks[, format, dtype])

Stacks sparse matrices vertically (row wise)

rand(m, n[, density, format, dtype, ...])

Generates a random sparse matrix.

random(m, n[, density, format, dtype, ...])

Generates a random sparse matrix.

Sparse matrix tools:

find(A)

Returns the indices and values of the nonzero elements of a matrix

Identifying sparse matrices:

issparse(x)

Checks if a given matrix is a sparse matrix.

isspmatrix(x)

Checks if a given matrix is a sparse matrix.

isspmatrix_csc(x)

Checks if a given matrix is of CSC format.

isspmatrix_csr(x)

Checks if a given matrix is of CSR format.

isspmatrix_coo(x)

Checks if a given matrix is of COO format.

isspmatrix_dia(x)

Checks if a given matrix is of DIA format.

Submodules#

csgraph

linalg

Exceptions#