0
# Sparse Matrix Operations
1
2
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.
3
4
## Capabilities
5
6
### Sparse Matrix Formats
7
8
Different sparse matrix representations for various use cases.
9
10
```python { .api }
11
class csr_matrix:
12
"""
13
Compressed Sparse Row matrix.
14
15
Parameters:
16
- arg1: array-like, sparse matrix data, indices, indptr, or dense matrix
17
- shape: tuple, matrix shape
18
- dtype: data type, matrix data type
19
- copy: bool, whether to copy data
20
"""
21
def __init__(self, arg1, shape=None, dtype=None, copy=False): ...
22
23
def dot(self, other):
24
"""Matrix multiplication."""
25
26
def transpose(self, axes=None, copy=False):
27
"""Matrix transpose."""
28
29
def tocsc(self, copy=False):
30
"""Convert to CSC format."""
31
32
def tocoo(self, copy=False):
33
"""Convert to COO format."""
34
35
def toarray(self):
36
"""Convert to dense array."""
37
38
@property
39
def data(self): ...
40
@property
41
def indices(self): ...
42
@property
43
def indptr(self): ...
44
45
class csc_matrix:
46
"""
47
Compressed Sparse Column matrix.
48
49
Parameters:
50
- arg1: array-like, sparse matrix data or dense matrix
51
- shape: tuple, matrix shape
52
- dtype: data type, matrix data type
53
- copy: bool, whether to copy data
54
"""
55
def __init__(self, arg1, shape=None, dtype=None, copy=False): ...
56
57
def dot(self, other): ...
58
def transpose(self, axes=None, copy=False): ...
59
def tocsr(self, copy=False): ...
60
def tocoo(self, copy=False): ...
61
def toarray(self): ...
62
63
class coo_matrix:
64
"""
65
COOrdinate sparse matrix.
66
67
Parameters:
68
- arg1: array-like, sparse matrix data or dense matrix
69
- shape: tuple, matrix shape
70
- dtype: data type, matrix data type
71
- copy: bool, whether to copy data
72
"""
73
def __init__(self, arg1, shape=None, dtype=None, copy=False): ...
74
75
def tocsr(self, copy=False): ...
76
def tocsc(self, copy=False): ...
77
def toarray(self): ...
78
79
@property
80
def data(self): ...
81
@property
82
def row(self): ...
83
@property
84
def col(self): ...
85
```
86
87
### Sparse Matrix Creation
88
89
Functions for creating sparse matrices.
90
91
```python { .api }
92
def eye(m, n=None, k=0, dtype=float, format=None):
93
"""
94
Sparse identity matrix.
95
96
Parameters:
97
- m: int, number of rows
98
- n: int, number of columns (default m)
99
- k: int, diagonal offset
100
- dtype: data type
101
- format: str, sparse format
102
103
Returns:
104
sparse matrix: Identity matrix
105
"""
106
107
def diags(diagonals, offsets=0, shape=None, format=None, dtype=None):
108
"""
109
Construct sparse matrix from diagonals.
110
111
Parameters:
112
- diagonals: array-like, diagonal values
113
- offsets: int or array-like, diagonal offsets
114
- shape: tuple, matrix shape
115
- format: str, sparse format
116
- dtype: data type
117
118
Returns:
119
sparse matrix: Diagonal matrix
120
"""
121
122
def random(m, n, density=0.01, format='coo', dtype=None, random_state=None):
123
"""
124
Generate random sparse matrix.
125
126
Parameters:
127
- m, n: int, matrix dimensions
128
- density: float, density of non-zero values
129
- format: str, sparse format
130
- dtype: data type
131
- random_state: int, random seed
132
133
Returns:
134
sparse matrix: Random sparse matrix
135
"""
136
```
137
138
## Usage Examples
139
140
### Basic Sparse Operations
141
142
```python
143
import cupy as cp
144
from cupy import sparse
145
146
# Create sparse matrix from dense
147
dense = cp.array([[1, 0, 2], [0, 0, 3], [4, 5, 6]])
148
sparse_csr = sparse.csr_matrix(dense)
149
150
# Convert between formats
151
sparse_csc = sparse_csr.tocsc()
152
sparse_coo = sparse_csr.tocoo()
153
154
# Matrix operations
155
result = sparse_csr.dot(dense)
156
transposed = sparse_csr.transpose()
157
```
158
159
### Large-scale Linear Algebra
160
161
```python
162
# Create large sparse matrix
163
n = 100000
164
# Tridiagonal matrix
165
row = cp.concatenate([cp.arange(n-1), cp.arange(n), cp.arange(1, n)])
166
col = cp.concatenate([cp.arange(1, n), cp.arange(n), cp.arange(n-1)])
167
data = cp.concatenate([-cp.ones(n-1), 2*cp.ones(n), -cp.ones(n-1)])
168
169
sparse_matrix = sparse.coo_matrix((data, (row, col)), shape=(n, n))
170
sparse_csr = sparse_matrix.tocsr()
171
172
# Sparse matrix-vector multiplication
173
x = cp.random.random(n)
174
y = sparse_csr.dot(x)
175
```