CuPy: NumPy & SciPy for GPU - A NumPy/SciPy-compatible array library for GPU-accelerated computing with Python, specifically built for CUDA 11.1
—
GPU-accelerated SciPy-compatible functions through the cupyx.scipy module, providing comprehensive scientific computing capabilities. CuPy extends NumPy compatibility to include major SciPy functionality including sparse matrices, signal processing, image processing, special functions, statistics, and advanced linear algebra operations.
Comprehensive sparse matrix support with GPU acceleration for memory-efficient operations on large sparse datasets.
# Sparse matrix formats (cupyx.scipy.sparse)
class csr_matrix:
"""Compressed Sparse Row matrix format"""
def __init__(self, arg1, shape=None, dtype=None, copy=False): ...
def dot(self, other): ...
def multiply(self, other): ...
def transpose(self, axes=None, copy=False): ...
def tocsc(self, copy=False): ...
def tocoo(self, copy=False): ...
def toarray(self, order=None, out=None): ...
def sum(self, axis=None, dtype=None, out=None): ...
class csc_matrix:
"""Compressed Sparse Column matrix format"""
def __init__(self, arg1, shape=None, dtype=None, copy=False): ...
def dot(self, other): ...
def multiply(self, other): ...
def transpose(self, axes=None, copy=False): ...
def tocsr(self, copy=False): ...
def tocoo(self, copy=False): ...
class coo_matrix:
"""Coordinate format sparse matrix"""
def __init__(self, arg1, shape=None, dtype=None, copy=False): ...
def tocsr(self, copy=False): ...
def tocsc(self, copy=False): ...
def sum_duplicates(self): ...
class dia_matrix:
"""Sparse matrix with DIAgonal storage"""
def __init__(self, arg1, shape=None, dtype=None, copy=False): ...
def tocsr(self, copy=False): ...
# Construction functions
def eye(m, n=None, k=0, dtype=float, format=None):
"""Sparse identity matrix"""
def identity(n, dtype=float, format=None):
"""Identity matrix in sparse format"""
def diags(diagonals, offsets=0, shape=None, format=None, dtype=None):
"""Construct sparse matrix from diagonals"""
def spdiags(data, diags, m, n, format=None):
"""Create sparse matrix from diagonals"""
def random(m, n, density=0.01, format='coo', dtype=None, random_state=None):
"""Generate random sparse matrix"""
# Utility functions
def find(A):
"""Find indices and values of nonzero elements"""
def tril(A, k=0, format=None):
"""Lower triangular portion of sparse matrix"""
def triu(A, k=0, format=None):
"""Upper triangular portion of sparse matrix"""
def kron(A, B, format=None):
"""Kronecker product of sparse matrices"""Digital signal processing functions for filtering, convolution, spectral analysis, and signal generation.
# Convolution and correlation (cupyx.scipy.signal)
def convolve(in1, in2, mode='full', method='auto'):
"""
Convolve two N-dimensional arrays.
Parameters:
- in1: array_like, first input array
- in2: array_like, second input array
- mode: {'full', 'valid', 'same'}, convolution mode
- method: {'auto', 'direct', 'fft'}, computation method
Returns:
- ndarray: Convolution of in1 and in2
"""
def correlate(in1, in2, mode='full', method='auto'):
"""
Cross-correlate two N-dimensional arrays.
Parameters:
- in1: array_like, first input array
- in2: array_like, second input array
- mode: {'full', 'valid', 'same'}, correlation mode
- method: {'auto', 'direct', 'fft'}, computation method
Returns:
- ndarray: Cross-correlation of in1 and in2
"""
def convolve2d(in1, in2, mode='full', boundary='fill', fillvalue=0):
"""2D convolution of matrices"""
def correlate2d(in1, in2, mode='full', boundary='fill', fillvalue=0):
"""2D cross-correlation of matrices"""
# Filtering
def lfilter(b, a, x, axis=-1, zi=None):
"""Filter data along one dimension using IIR or FIR filter"""
def filtfilt(b, a, x, axis=-1, padtype='odd', padlen=None, method='pad', irlen=None):
"""Apply filter forward and backward to remove phase shift"""
# Window functions
def get_window(window, Nx, fftbins=True):
"""Return window of given length and type"""
def hann(M, sym=True):
"""Hann window"""
def hamming(M, sym=True):
"""Hamming window"""
def blackman(M, sym=True):
"""Blackman window"""
def bartlett(M, sym=True):
"""Bartlett window"""
# Spectral analysis
def periodogram(x, fs=1.0, window='boxcar', nfft=None, detrend='constant', return_onesided=True, scaling='density', axis=-1):
"""Estimate power spectral density using periodogram"""
def welch(x, fs=1.0, window='hann', nperseg=None, noverlap=None, nfft=None, detrend='constant', return_onesided=True, scaling='density', axis=-1):
"""Estimate power spectral density using Welch's method"""Comprehensive image processing functions for filtering, morphology, measurements, and geometric transformations.
# Filters (cupyx.scipy.ndimage)
def gaussian_filter(input, sigma, order=0, output=None, mode='reflect', cval=0.0, truncate=4.0):
"""
Multidimensional Gaussian filter.
Parameters:
- input: array_like, input array
- sigma: scalar or sequence, standard deviation for Gaussian kernel
- order: int or sequence, order of derivative
- output: array, optional, output array
- mode: {'reflect', 'constant', 'nearest', 'mirror', 'wrap'}, boundary mode
- cval: scalar, value for constant mode
- truncate: float, truncate filter at this sigma
Returns:
- ndarray: Filtered array
"""
def uniform_filter(input, size, output=None, mode='reflect', cval=0.0, origin=0):
"""Multidimensional uniform filter"""
def median_filter(input, size=None, footprint=None, output=None, mode='reflect', cval=0.0, origin=0):
"""Multidimensional median filter"""
def maximum_filter(input, size=None, footprint=None, output=None, mode='reflect', cval=0.0, origin=0):
"""Multidimensional maximum filter"""
def minimum_filter(input, size=None, footprint=None, output=None, mode='reflect', cval=0.0, origin=0):
"""Multidimensional minimum filter"""
def rank_filter(input, rank, size=None, footprint=None, output=None, mode='reflect', cval=0.0, origin=0):
"""Multidimensional rank filter"""
# Morphological operations
def binary_erosion(input, structure=None, iterations=1, mask=None, output=None, border_value=0, origin=0, brute_force=False):
"""Multidimensional binary erosion"""
def binary_dilation(input, structure=None, iterations=1, mask=None, output=None, border_value=0, origin=0, brute_force=False):
"""Multidimensional binary dilation"""
def binary_opening(input, structure=None, iterations=1, output=None, origin=0):
"""Multidimensional binary opening"""
def binary_closing(input, structure=None, iterations=1, output=None, origin=0):
"""Multidimensional binary closing"""
# Geometric transformations
def rotate(input, angle, axes=(1, 0), reshape=True, output=None, order=1, mode='constant', cval=0.0, prefilter=True):
"""Rotate array around given axes"""
def shift(input, shift, output=None, order=1, mode='constant', cval=0.0, prefilter=True):
"""Shift array by given offset"""
def zoom(input, zoom, output=None, order=1, mode='constant', cval=0.0, prefilter=True):
"""Zoom array by given factors"""
def affine_transform(input, matrix, offset=0.0, output_shape=None, output=None, order=1, mode='constant', cval=0.0, prefilter=True):
"""Apply affine transformation"""
# Measurements
def label(input, structure=None, output=None):
"""Label features in binary image"""
def center_of_mass(input, labels=None, index=None):
"""Calculate center of mass of features"""
def find_objects(input, max_label=0):
"""Find objects in labeled array"""
def sum_labels(input, labels=None, index=None):
"""Sum of values for labeled regions"""Mathematical special functions including Bessel functions, gamma functions, error functions, and orthogonal polynomials.
# Error functions and related (cupyx.scipy.special)
def erf(x):
"""Error function"""
def erfc(x):
"""Complementary error function"""
def erfinv(x):
"""Inverse error function"""
def erfcinv(x):
"""Inverse complementary error function"""
# Gamma and related functions
def gamma(x):
"""Gamma function"""
def gammaln(x):
"""Natural logarithm of absolute value of gamma function"""
def digamma(x):
"""Digamma function (derivative of log gamma)"""
def polygamma(n, x):
"""Polygamma function"""
def beta(a, b):
"""Beta function"""
def betaln(a, b):
"""Natural logarithm of beta function"""
# Bessel functions
def j0(x):
"""Bessel function of first kind of order 0"""
def j1(x):
"""Bessel function of first kind of order 1"""
def jn(n, x):
"""Bessel function of first kind of integer order n"""
def y0(x):
"""Bessel function of second kind of order 0"""
def y1(x):
"""Bessel function of second kind of order 1"""
def yn(n, x):
"""Bessel function of second kind of integer order n"""
def i0(x):
"""Modified Bessel function of first kind of order 0"""
def i1(x):
"""Modified Bessel function of first kind of order 1"""
def iv(v, x):
"""Modified Bessel function of first kind of real order"""
def k0(x):
"""Modified Bessel function of second kind of order 0"""
def k1(x):
"""Modified Bessel function of second kind of order 1"""
def kv(v, x):
"""Modified Bessel function of second kind of real order"""
# Hypergeometric functions
def hyp2f1(a, b, c, z):
"""Gaussian hypergeometric function 2F1"""
# Elliptic functions and integrals
def ellipk(m):
"""Complete elliptic integral of first kind"""
def ellipe(m):
"""Complete elliptic integral of second kind"""
# Convenience functions
def expit(x):
"""Expit (inverse logit) function"""
def logit(x):
"""Logit function"""
def softmax(x, axis=None):
"""Softmax function"""
def log_softmax(x, axis=None):
"""Log-softmax function"""Statistical functions for probability distributions, hypothesis testing, and descriptive statistics.
# Distribution statistics (cupyx.scipy.stats)
def entropy(pk, qk=None, base=None, axis=0):
"""Calculate entropy of distribution"""
def kurtosis(a, axis=0, fisher=True, bias=True, nan_policy='propagate'):
"""Compute kurtosis of dataset"""
def skew(a, axis=0, bias=True, nan_policy='propagate'):
"""Compute skewness of dataset"""
def moment(a, moment=1, axis=0, nan_policy='propagate'):
"""Calculate nth moment about mean"""
def variation(a, axis=0, nan_policy='propagate'):
"""Compute coefficient of variation"""
def zscore(a, axis=0, ddof=0, nan_policy='propagate'):
"""Compute z-scores"""
# Correlation functions
def pearsonr(x, y):
"""Pearson correlation coefficient and p-value"""
def spearmanr(a, b=None, axis=0, nan_policy='propagate'):
"""Spearman correlation coefficient and p-value"""
def kendalltau(x, y, initial_lexsort=None, nan_policy='propagate', method='auto'):
"""Kendall's tau correlation coefficient and p-value"""
# Statistical tests
def ttest_1samp(a, popmean, axis=0, nan_policy='propagate', alternative='two-sided'):
"""One-sample t-test"""
def ttest_ind(a, b, axis=0, equal_var=True, nan_policy='propagate', permutations=None, random_state=None, alternative='two-sided'):
"""Independent two-sample t-test"""
def ttest_rel(a, b, axis=0, nan_policy='propagate', alternative='two-sided'):
"""Related/paired sample t-test"""
def kstest(rvs, cdf, args=(), N=20, alternative='two-sided', method='auto'):
"""Kolmogorov-Smirnov test"""
def chisquare(f_obs, f_exp=None, ddof=0, axis=0):
"""Chi-square goodness of fit test"""
# Distribution fitting
def norm():
"""Normal distribution object with methods for statistics"""
def uniform():
"""Uniform distribution object"""
def expon():
"""Exponential distribution object"""
def gamma():
"""Gamma distribution object"""Extended linear algebra functions beyond core CuPy functionality, including advanced decompositions and matrix functions.
# Matrix decompositions (cupyx.scipy.linalg)
def lu(a, permute_l=False, overwrite_a=False, check_finite=True):
"""LU decomposition with partial pivoting"""
def lu_factor(a, overwrite_a=False, check_finite=True):
"""LU decomposition returning factors in compact form"""
def lu_solve(lu_and_piv, b, trans=0, overwrite_b=False, check_finite=True):
"""Solve linear system using LU decomposition"""
def qr(a, overwrite_a=False, lwork=None, mode='full', pivoting=False, check_finite=True):
"""QR decomposition"""
def rq(a, overwrite_a=False, lwork=None, mode='full', check_finite=True):
"""RQ decomposition"""
def schur(a, output='real', lwork=None, overwrite_a=False, sort=None, check_finite=True):
"""Schur decomposition"""
def hessenberg(a, calc_q=True, overwrite_a=False, check_finite=True):
"""Hessenberg decomposition"""
# Matrix functions
def expm(A):
"""Matrix exponential"""
def logm(A, disp=True):
"""Matrix logarithm"""
def sqrtm(A, disp=True, blocksize=64):
"""Matrix square root"""
def fractional_matrix_power(A, t):
"""Fractional matrix power"""
# Eigenvalue problems
def eigvals(a, b=None, overwrite_a=False, check_finite=True, homogeneous_eigvals=False):
"""Eigenvalues of general matrix"""
def eigvalsh(a, b=None, lower=True, overwrite_a=False, overwrite_b=False, turbo=True, eigvals=None, type=1, check_finite=True):
"""Eigenvalues of symmetric/Hermitian matrix"""
def eig(a, b=None, left=False, right=True, overwrite_a=False, overwrite_b=False, check_finite=True, homogeneous_eigvals=False):
"""Eigenvalues and vectors of general matrix"""
def eigh(a, b=None, lower=True, eigvals_only=False, overwrite_a=False, overwrite_b=False, turbo=True, eigvals=None, type=1, check_finite=True):
"""Eigenvalues and vectors of symmetric/Hermitian matrix"""
# Linear system solvers
def solve_triangular(a, b, trans=0, lower=False, unit_diagonal=False, overwrite_b=False, debug=None, check_finite=True):
"""Solve triangular linear system"""
def solve_banded(l_and_u, ab, b, overwrite_ab=False, overwrite_b=False, debug=None, check_finite=True):
"""Solve banded linear system"""
def solveh_banded(ab, b, overwrite_ab=False, overwrite_b=False, lower=False, check_finite=True):
"""Solve symmetric/Hermitian banded linear system"""
# Matrix analysis
def norm(a, ord=None, axis=None, keepdims=False, check_finite=True):
"""Matrix or vector norm"""
def det(a, overwrite_a=False, check_finite=True):
"""Determinant of matrix"""
def slogdet(a, overwrite_a=False, check_finite=True):
"""Sign and log determinant of matrix"""
def cond(x, p=None):
"""Condition number of matrix"""
def matrix_rank(M, tol=None, hermitian=False):
"""Matrix rank using SVD"""Interpolation and approximation functions for data fitting and function approximation.
# 1D interpolation (cupyx.scipy.interpolate)
def interp1d(x, y, kind='linear', axis=-1, copy=True, bounds_error=None, fill_value=float('nan'), assume_sorted=False):
"""
1D interpolation function.
Parameters:
- x: array_like, x-coordinates of data points
- y: array_like, y-coordinates of data points
- kind: str or int, interpolation type ('linear', 'nearest', 'cubic', etc.)
- axis: int, axis along which y is varying
- copy: bool, whether to copy data
- bounds_error: bool, whether to raise error for out-of-bounds
- fill_value: value to use for out-of-bounds points
- assume_sorted: bool, whether x is already sorted
Returns:
- callable: Interpolation function
"""
# Spline interpolation
def splrep(x, y, w=None, xb=None, xe=None, k=3, task=0, s=None, t=None, full_output=0, per=0, quiet=1):
"""Find B-spline representation of 1D curve"""
def splev(x, tck, der=0, ext=0):
"""Evaluate B-spline or its derivatives"""
def sproot(tck, mest=10):
"""Find roots of cubic B-spline"""
def spalde(x, tck):
"""Evaluate all derivatives of B-spline at given points"""
# 2D interpolation
def griddata(points, values, xi, method='linear', fill_value=float('nan'), rescale=False):
"""
Interpolate unstructured D-dimensional data.
Parameters:
- points: ndarray, coordinates of data points
- values: ndarray, data values at points
- xi: ndarray, coordinates where to interpolate
- method: {'linear', 'nearest', 'cubic'}, interpolation method
- fill_value: value for points outside convex hull
- rescale: bool, whether to rescale points to unit cube
Returns:
- ndarray: Interpolated values at xi
"""Spatial data structures and algorithms for computational geometry and spatial analysis.
# Distance computations (cupyx.scipy.spatial)
def distance_matrix(x, y, p=2, threshold=1000000):
"""
Compute distance matrix between arrays.
Parameters:
- x: ndarray, first set of points
- y: ndarray, second set of points
- p: float, which Minkowski norm to use (1, 2, inf)
- threshold: int, maximum size for direct computation
Returns:
- ndarray: Distance matrix
"""
def pdist(X, metric='euclidean', *args, **kwargs):
"""Pairwise distances between observations"""
def cdist(XA, XB, metric='euclidean', *args, **kwargs):
"""Distances between each pair of observations from two collections"""
def squareform(X, force='no', checks=True):
"""Convert between condensed and square distance matrices"""
# Convex hull
class ConvexHull:
"""Convex hull of points"""
def __init__(self, points, incremental=False, qhull_options=None): ...
@property
def vertices(self): ...
@property
def simplices(self): ...
@property
def volume(self): ...
# Voronoi diagrams
class Voronoi:
"""Voronoi diagram of points"""
def __init__(self, points, furthest_site=False, incremental=False, qhull_options=None): ...
# KD-tree for fast nearest neighbor searches
class KDTree:
"""k-dimensional tree for fast nearest neighbor queries"""
def __init__(self, data, leafsize=10, compact_nodes=True, copy_data=False, balanced_tree=True, boxsize=None): ...
def query(self, x, k=1, eps=0, p=2, distance_upper_bound=float('inf'), workers=1): ...
def query_ball_point(self, x, r, p=2.0, eps=0): ...
def query_pairs(self, r, p=2.0, eps=0): ...import cupy as cp
import cupyx.scipy.sparse as sparse
# Create sparse matrices
data = cp.array([1, 2, 3, 4, 5])
row = cp.array([0, 1, 2, 1, 3])
col = cp.array([0, 1, 2, 3, 2])
# COO format
coo = sparse.coo_matrix((data, (row, col)), shape=(4, 4))
# Convert to CSR for efficient arithmetic
csr = coo.tocsr()
# Matrix operations
result = csr.dot(cp.ones(4))
transposed = csr.T
squared = csr.multiply(csr)
# Create identity matrix
identity = sparse.identity(1000, format='csr')import cupy as cp
import cupyx.scipy.signal as signal
# Generate test signal
t = cp.linspace(0, 1, 1000, endpoint=False)
sig = cp.sin(2 * cp.pi * 50 * t) + cp.sin(2 * cp.pi * 120 * t)
sig += 0.1 * cp.random.randn(1000)
# Apply filters
b, a = signal.butter(4, 0.2) # Butterworth filter
filtered = signal.lfilter(b, a, sig)
# Convolution with kernel
kernel = cp.array([0.25, 0.5, 0.25])
convolved = signal.convolve(sig, kernel, mode='same')
# Spectral analysis
freqs, psd = signal.welch(sig, fs=1000, nperseg=256)import cupy as cp
import cupyx.scipy.ndimage as ndimage
# Load image data
image = cp.random.random((512, 512))
# Apply filters
blurred = ndimage.gaussian_filter(image, sigma=2.0)
edges = ndimage.sobel(image)
denoised = ndimage.median_filter(image, size=3)
# Geometric transformations
rotated = ndimage.rotate(image, 45, reshape=False)
zoomed = ndimage.zoom(image, 1.5)
shifted = ndimage.shift(image, (10, -5))
# Binary morphology
binary = image > 0.5
eroded = ndimage.binary_erosion(binary)
dilated = ndimage.binary_dilation(binary)import cupy as cp
import cupyx.scipy.stats as stats
# Generate sample data
data1 = cp.random.normal(0, 1, 1000)
data2 = cp.random.normal(0.5, 1.2, 1000)
# Descriptive statistics
mean_val = cp.mean(data1)
std_val = cp.std(data1)
skewness = stats.skew(data1)
kurt = stats.kurtosis(data1)
# Statistical tests
t_stat, p_val = stats.ttest_ind(data1, data2)
correlation, p_corr = stats.pearsonr(data1[:100], data2[:100])
# Distribution fitting and analysis
z_scores = stats.zscore(data1)
entropy_val = stats.entropy(cp.abs(data1))cupyx.scipy.get_array_module() for writing CPU/GPU generic codeInstall with Tessl CLI
npx tessl i tessl/pypi-cupy-cuda111