NumPy & SciPy-compatible array library for GPU-accelerated computing with Python
—
GPU-accelerated sparse matrix operations for large-scale scientific computing. CuPy provides sparse matrix formats and operations compatible with SciPy.sparse but optimized for GPU execution.
Different sparse matrix representations for various use cases.
class csr_matrix:
"""
Compressed Sparse Row matrix.
Parameters:
- arg1: array-like, sparse matrix data, indices, indptr, or dense matrix
- shape: tuple, matrix shape
- dtype: data type, matrix data type
- copy: bool, whether to copy data
"""
def __init__(self, arg1, shape=None, dtype=None, copy=False): ...
def dot(self, other):
"""Matrix multiplication."""
def transpose(self, axes=None, copy=False):
"""Matrix transpose."""
def tocsc(self, copy=False):
"""Convert to CSC format."""
def tocoo(self, copy=False):
"""Convert to COO format."""
def toarray(self):
"""Convert to dense array."""
@property
def data(self): ...
@property
def indices(self): ...
@property
def indptr(self): ...
class csc_matrix:
"""
Compressed Sparse Column matrix.
Parameters:
- arg1: array-like, sparse matrix data or dense matrix
- shape: tuple, matrix shape
- dtype: data type, matrix data type
- copy: bool, whether to copy data
"""
def __init__(self, arg1, shape=None, dtype=None, copy=False): ...
def dot(self, other): ...
def transpose(self, axes=None, copy=False): ...
def tocsr(self, copy=False): ...
def tocoo(self, copy=False): ...
def toarray(self): ...
class coo_matrix:
"""
COOrdinate sparse matrix.
Parameters:
- arg1: array-like, sparse matrix data or dense matrix
- shape: tuple, matrix shape
- dtype: data type, matrix data type
- copy: bool, whether to copy data
"""
def __init__(self, arg1, shape=None, dtype=None, copy=False): ...
def tocsr(self, copy=False): ...
def tocsc(self, copy=False): ...
def toarray(self): ...
@property
def data(self): ...
@property
def row(self): ...
@property
def col(self): ...Functions for creating sparse matrices.
def eye(m, n=None, k=0, dtype=float, format=None):
"""
Sparse identity matrix.
Parameters:
- m: int, number of rows
- n: int, number of columns (default m)
- k: int, diagonal offset
- dtype: data type
- format: str, sparse format
Returns:
sparse matrix: Identity matrix
"""
def diags(diagonals, offsets=0, shape=None, format=None, dtype=None):
"""
Construct sparse matrix from diagonals.
Parameters:
- diagonals: array-like, diagonal values
- offsets: int or array-like, diagonal offsets
- shape: tuple, matrix shape
- format: str, sparse format
- dtype: data type
Returns:
sparse matrix: Diagonal matrix
"""
def random(m, n, density=0.01, format='coo', dtype=None, random_state=None):
"""
Generate random sparse matrix.
Parameters:
- m, n: int, matrix dimensions
- density: float, density of non-zero values
- format: str, sparse format
- dtype: data type
- random_state: int, random seed
Returns:
sparse matrix: Random sparse matrix
"""import cupy as cp
from cupy import sparse
# Create sparse matrix from dense
dense = cp.array([[1, 0, 2], [0, 0, 3], [4, 5, 6]])
sparse_csr = sparse.csr_matrix(dense)
# Convert between formats
sparse_csc = sparse_csr.tocsc()
sparse_coo = sparse_csr.tocoo()
# Matrix operations
result = sparse_csr.dot(dense)
transposed = sparse_csr.transpose()# Create large sparse matrix
n = 100000
# Tridiagonal matrix
row = cp.concatenate([cp.arange(n-1), cp.arange(n), cp.arange(1, n)])
col = cp.concatenate([cp.arange(1, n), cp.arange(n), cp.arange(n-1)])
data = cp.concatenate([-cp.ones(n-1), 2*cp.ones(n), -cp.ones(n-1)])
sparse_matrix = sparse.coo_matrix((data, (row, col)), shape=(n, n))
sparse_csr = sparse_matrix.tocsr()
# Sparse matrix-vector multiplication
x = cp.random.random(n)
y = sparse_csr.dot(x)Install with Tessl CLI
npx tessl i tessl/pypi-cupy