0
# SciPy Extensions
1
2
Extended scientific computing functions from cupyx.scipy for advanced mathematical operations beyond NumPy compatibility. These GPU-accelerated implementations provide SciPy-equivalent functionality for scientific computing workflows.
3
4
## Capabilities
5
6
### Signal Processing
7
8
Digital signal processing functions from `cupyx.scipy.signal`.
9
10
```python { .api }
11
def convolve(in1, in2, mode='full', method='auto'):
12
"""
13
Convolution of two arrays.
14
15
Parameters:
16
- in1, in2: array-like, input arrays
17
- mode: {'full', 'valid', 'same'}, output size
18
- method: {'auto', 'direct', 'fft'}, computation method
19
20
Returns:
21
cupy.ndarray: Convolution result
22
"""
23
24
def correlate(in1, in2, mode='full', method='auto'):
25
"""Cross-correlation of two arrays."""
26
27
def fftconvolve(in1, in2, mode='full', axes=None):
28
"""FFT-based convolution."""
29
30
def convolve2d(in1, in2, mode='full', boundary='fill', fillvalue=0):
31
"""2D convolution."""
32
33
def correlate2d(in1, in2, mode='full', boundary='fill', fillvalue=0):
34
"""2D cross-correlation."""
35
```
36
37
### Image Processing
38
39
Image processing functions from `cupyx.scipy.ndimage`.
40
41
```python { .api }
42
def gaussian_filter(input, sigma, order=0, output=None, mode='reflect', cval=0.0, truncate=4.0):
43
"""
44
Gaussian filter.
45
46
Parameters:
47
- input: array-like, input image
48
- sigma: scalar or sequence, standard deviation
49
- order: int or sequence, derivative order
50
- output: array, output array
51
- mode: str, boundary condition
52
- cval: scalar, fill value for constant mode
53
- truncate: scalar, filter truncation
54
55
Returns:
56
cupy.ndarray: Filtered image
57
"""
58
59
def sobel(input, axis=-1, output=None, mode='reflect', cval=0.0):
60
"""Sobel edge detection filter."""
61
62
def gaussian_filter1d(input, sigma, axis=-1, order=0, output=None, mode='reflect', cval=0.0, truncate=4.0):
63
"""1D Gaussian filter."""
64
65
def uniform_filter(input, size=3, output=None, mode='reflect', cval=0.0, origin=0):
66
"""Uniform filter (box filter)."""
67
68
def median_filter(input, size=None, footprint=None, output=None, mode='reflect', cval=0.0, origin=0):
69
"""Median filter."""
70
```
71
72
### Sparse Linear Algebra
73
74
Sparse matrix linear algebra from `cupyx.scipy.sparse.linalg`.
75
76
```python { .api }
77
def spsolve(A, b, permc_spec=None, use_umfpack=True):
78
"""
79
Solve sparse linear system Ax = b.
80
81
Parameters:
82
- A: sparse matrix, coefficient matrix
83
- b: array-like, right-hand side
84
- permc_spec: str, permutation strategy
85
- use_umfpack: bool, use UMFPACK solver
86
87
Returns:
88
cupy.ndarray: Solution vector
89
"""
90
91
def norm(x, ord=None, axis=None):
92
"""Sparse matrix or vector norm."""
93
94
def inv(A):
95
"""Sparse matrix inverse."""
96
```
97
98
### Optimization
99
100
Optimization functions from `cupyx.scipy.optimize`.
101
102
```python { .api }
103
def minimize(fun, x0, args=(), method=None, jac=None, bounds=None, constraints=()):
104
"""
105
Minimize scalar function.
106
107
Parameters:
108
- fun: callable, objective function
109
- x0: array-like, initial guess
110
- args: tuple, extra arguments to fun
111
- method: str, optimization method
112
- jac: callable, gradient function
113
- bounds: sequence, variable bounds
114
- constraints: dict or sequence, constraints
115
116
Returns:
117
OptimizeResult: Optimization result
118
"""
119
120
def minimize_scalar(fun, bounds=None, args=(), method='brent'):
121
"""Minimize scalar function of one variable."""
122
```
123
124
### Special Functions
125
126
Mathematical special functions from `cupyx.scipy.special`.
127
128
```python { .api }
129
def erf(x):
130
"""Error function."""
131
132
def erfc(x):
133
"""Complementary error function."""
134
135
def gamma(x):
136
"""Gamma function."""
137
138
def gammaln(x):
139
"""Log of gamma function."""
140
141
def beta(a, b):
142
"""Beta function."""
143
144
def j0(x):
145
"""Bessel function of first kind, order 0."""
146
147
def j1(x):
148
"""Bessel function of first kind, order 1."""
149
150
def y0(x):
151
"""Bessel function of second kind, order 0."""
152
```
153
154
### Utilities
155
156
Additional scientific computing utilities.
157
158
```python { .api }
159
def get_runtime_info(full=False):
160
"""
161
Get CuPy runtime information.
162
163
Parameters:
164
- full: bool, include detailed information
165
166
Returns:
167
str: Runtime information string
168
"""
169
170
def scatter_add(a, indices, b, axis=None):
171
"""
172
Scatter add operation.
173
174
Parameters:
175
- a: array-like, target array
176
- indices: array-like, indices to scatter to
177
- b: array-like, values to add
178
- axis: int, axis to scatter along
179
180
Returns:
181
cupy.ndarray: Result of scatter add
182
"""
183
184
def scatter_max(a, indices, b, axis=None):
185
"""Scatter max operation."""
186
187
def scatter_min(a, indices, b, axis=None):
188
"""Scatter min operation."""
189
```
190
191
## Usage Examples
192
193
### Signal Processing
194
195
```python
196
import cupy as cp
197
import cupyx.scipy.signal as signal
198
199
# Create test signals
200
t = cp.linspace(0, 1, 1000)
201
sig1 = cp.sin(2 * cp.pi * 5 * t)
202
sig2 = cp.exp(-t) * cp.sin(2 * cp.pi * 10 * t)
203
204
# Convolution
205
convolved = signal.convolve(sig1, sig2, mode='same')
206
207
# Cross-correlation
208
correlated = signal.correlate(sig1, sig2, mode='same')
209
```
210
211
### Image Processing
212
213
```python
214
import cupyx.scipy.ndimage as ndimage
215
216
# Create test image
217
image = cp.random.random((512, 512))
218
219
# Gaussian blur
220
blurred = ndimage.gaussian_filter(image, sigma=2.0)
221
222
# Edge detection
223
edges = ndimage.sobel(image)
224
225
# Median filtering for noise reduction
226
denoised = ndimage.median_filter(image, size=3)
227
```
228
229
### Optimization
230
231
```python
232
import cupyx.scipy.optimize as optimize
233
234
# Define objective function
235
def objective(x):
236
return (x[0] - 1)**2 + (x[1] - 2)**2
237
238
# Minimize function
239
result = optimize.minimize(objective, x0=[0, 0], method='BFGS')
240
print(f"Minimum at: {result.x}")
241
```