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.

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.

Linear Algebra (cupyx.scipy.sparse.linalg)

Abstract linear operators

LinearOperator(shape, matvec[, rmatvec, …])

Common interface for performing matrix vector products

aslinearoperator(A)

Return A as a LinearOperator.

Matrix norms

cupyx.scipy.sparse.linalg.norm(x[, ord, axis])

Norm of a cupy.scipy.spmatrix

Solving linear problems

Direct methods for linear equation systems:

spsolve(A, b)

Solves a sparse linear system A x = b

spsolve_triangular(A, b[, lower, …])

Solves a sparse triangular system A x = b.

factorized(A)

Return a function for solving a sparse linear system, with A pre-factorized.

Iterative methods for linear equation systems:

cg(A, b[, x0, tol, maxiter, M, callback, atol])

Uses Conjugate Gradient iteration to solve Ax = b.

gmres(A, b[, x0, tol, restart, maxiter, M, …])

Uses Generalized Minimal RESidual iteration to solve Ax = b.

cgs(A, b[, x0, tol, maxiter, M, callback, atol])

Use Conjugate Gradient Squared iteration to solve Ax = b.

Iterative methods for least-squares problems:

lsqr(A, b)

Solves linear system with QR decomposition.

lsmr(A, b[, x0, damp, atol, btol, conlim, …])

Iterative solver for least-squares problems.

Matrix factorizations

Eigenvalue problems:

eigsh(a[, k, which, ncv, maxiter, tol, …])

Finds k eigenvalues and eigenvectors of the real symmetric matrix.

lobpcg(A, X[, B, M, Y, tol, maxiter, …])

Locally Optimal Block Preconditioned Conjugate Gradient Method (LOBPCG)

Singular values problems:

svds(a[, k, ncv, tol, which, maxiter, …])

Finds the largest k singular values/vectors for a sparse matrix.

Complete or incomplete LU factorizations:

splu(A[, permc_spec, diag_pivot_thresh, …])

Computes the LU decomposition of a sparse square matrix.

spilu(A[, drop_tol, fill_factor, drop_rule, …])

Computes the incomplete LU decomposition of a sparse square matrix.