NumPy & SciPy for GPU - CUDA 11.0 compatible package providing GPU-accelerated computing with Python through a NumPy/SciPy-compatible array library
—
Comprehensive SciPy-compatible functions through cupyx.scipy, providing GPU acceleration for scientific computing workflows. Covers linear algebra, signal processing, sparse matrices, FFT, and specialized mathematical functions.
Advanced linear algebra operations beyond core CuPy functionality.
def solve(a, b, assume_a='gen', lower=False, overwrite_a=False, overwrite_b=False, debug=None, check_finite=True):
"""
Solve linear system ax = b for general matrices.
Parameters:
- a: array_like, coefficient matrix
- b: array_like, dependent variable values
- assume_a: str, properties of matrix a
- lower: bool, use lower triangular part only
- overwrite_a: bool, discard data in a
- overwrite_b: bool, discard data in b
- check_finite: bool, check for infinite/NaN values
Returns:
cupy.ndarray: Solution to the system ax = b
"""
def solve_triangular(a, b, trans=0, lower=False, unit_diagonal=False, overwrite_b=False, debug=None, check_finite=True):
"""Solve triangular system ax = b."""
def inv(a, overwrite_a=False, check_finite=True):
"""Compute matrix inverse."""
def pinv(a, cond=None, rcond=None, return_rank=False, check_finite=True):
"""Compute Moore-Penrose pseudoinverse."""
def det(a, overwrite_a=False, check_finite=True):
"""Compute matrix determinant."""
def lu_factor(a, permute_l=False, overwrite_a=False, check_finite=True):
"""Compute LU decomposition with partial pivoting."""
def lu_solve(lu_and_piv, b, trans=0, overwrite_b=False, check_finite=True):
"""Solve linear system using LU decomposition."""
def cholesky(a, lower=False, overwrite_a=False, check_finite=True):
"""Compute Cholesky decomposition."""
def cholesky_solve(cho_fac, b, overwrite_b=False, check_finite=True):
"""Solve linear system using Cholesky decomposition."""
def svd(a, full_matrices=True, compute_uv=True, overwrite_a=False, check_finite=True, lapack_driver='gesdd'):
"""Compute singular value decomposition."""
def svdvals(a, overwrite_a=False, check_finite=True):
"""Compute singular values."""
def qr(a, overwrite_a=False, lwork=None, mode='full', pivoting=False, check_finite=True):
"""Compute QR decomposition."""
def eig(a, b=None, left=False, right=True, overwrite_a=False, overwrite_b=False, check_finite=True, homogeneous_eigvals=False):
"""Solve general eigenvalue problem."""
def eigh(a, b=None, lower=True, eigvals_only=False, overwrite_a=False, overwrite_b=False, type=1, check_finite=True):
"""Solve symmetric/Hermitian eigenvalue problem."""
def eigvals(a, b=None, overwrite_a=False, check_finite=True, homogeneous_eigvals=False):
"""Compute eigenvalues of general matrix."""
def eigvalsh(a, b=None, lower=True, overwrite_a=False, overwrite_b=False, type=1, check_finite=True):
"""Compute eigenvalues of symmetric/Hermitian matrix."""GPU-accelerated sparse matrix operations for efficient computation with sparse data.
class csr_matrix:
"""
Compressed Sparse Row matrix.
Parameters:
- arg1: array_like/tuple, matrix data
- shape: tuple, matrix dimensions
- dtype: data type
- copy: bool, copy input data
"""
def __init__(self, arg1, shape=None, dtype=None, copy=False): ...
def dot(self, other):
"""Matrix multiplication."""
def multiply(self, other):
"""Element-wise 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."""
def todense(self):
"""Convert to dense matrix."""
class csc_matrix:
"""Compressed Sparse Column matrix."""
def __init__(self, arg1, shape=None, dtype=None, copy=False): ...
# Similar methods as csr_matrix
class coo_matrix:
"""Coordinate format sparse matrix."""
def __init__(self, arg1, shape=None, dtype=None, copy=False): ...
# Similar methods as csr_matrix
class dia_matrix:
"""Sparse matrix with DIAgonal storage."""
def __init__(self, arg1, shape=None, dtype=None, copy=False): ...
def identity(n, dtype='d', format=None):
"""Sparse identity matrix."""
def eye(m, n=None, k=0, dtype=float, format=None):
"""Sparse matrix with ones on diagonal."""
def diags(diagonals, offsets=0, shape=None, format=None, dtype=None):
"""Construct sparse matrix from diagonals."""
def spdiags(data, diags, m, n, format=None):
"""Return sparse matrix from diagonals."""
def random(m, n, density=0.01, format='coo', dtype=None, random_state=None):
"""Generate random sparse matrix."""
def spsolve(A, b, permc_spec=None, use_umfpack=True):
"""Solve sparse linear system Ax = b."""
def spsolve_triangular(A, b, lower=True, overwrite_A=False, overwrite_b=False):
"""Solve triangular sparse system."""GPU-accelerated FFT operations using cuFFT library.
def fft(x, n=None, axis=-1, norm=None, overwrite_x=False):
"""
Compute 1-D discrete Fourier Transform.
Parameters:
- x: array_like, input array
- n: int, length of FFT
- axis: int, axis along which FFT is computed
- norm: str, normalization mode
- overwrite_x: bool, allow overwriting input
Returns:
cupy.ndarray: FFT of input array
"""
def ifft(x, n=None, axis=-1, norm=None, overwrite_x=False):
"""Compute 1-D inverse discrete Fourier Transform."""
def fft2(x, s=None, axes=(0, 1), norm=None, overwrite_x=False):
"""Compute 2-D discrete Fourier Transform."""
def ifft2(x, s=None, axes=(0, 1), norm=None, overwrite_x=False):
"""Compute 2-D inverse discrete Fourier Transform."""
def fftn(x, s=None, axes=None, norm=None, overwrite_x=False):
"""Compute N-D discrete Fourier Transform."""
def ifftn(x, s=None, axes=None, norm=None, overwrite_x=False):
"""Compute N-D inverse discrete Fourier Transform."""
def rfft(x, n=None, axis=-1, norm=None, overwrite_x=False):
"""Compute real 1-D discrete Fourier Transform."""
def irfft(x, n=None, axis=-1, norm=None, overwrite_x=False):
"""Compute inverse of rfft."""
def rfft2(x, s=None, axes=(0, 1), norm=None, overwrite_x=False):
"""Compute real 2-D discrete Fourier Transform."""
def irfft2(x, s=None, axes=(0, 1), norm=None, overwrite_x=False):
"""Compute inverse of rfft2."""
def rfftn(x, s=None, axes=None, norm=None, overwrite_x=False):
"""Compute real N-D discrete Fourier Transform."""
def irfftn(x, s=None, axes=None, norm=None, overwrite_x=False):
"""Compute inverse of rfftn."""
def hfft(x, n=None, axis=-1, norm=None, overwrite_x=False):
"""Compute FFT of Hermitian sequence."""
def ihfft(x, n=None, axis=-1, norm=None, overwrite_x=False):
"""Compute inverse of hfft."""
def fftfreq(n, d=1.0):
"""Return discrete Fourier Transform sample frequencies."""
def rfftfreq(n, d=1.0):
"""Return sample frequencies for rfft."""
def fftshift(x, axes=None):
"""Shift zero-frequency component to center."""
def ifftshift(x, axes=None):
"""Inverse of fftshift."""Signal processing functions for filtering, spectral analysis, and signal manipulation.
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: str, convolution mode ('full', 'valid', 'same')
- method: str, computation method
Returns:
cupy.ndarray: Convolution of in1 and in2
"""
def correlate(in1, in2, mode='full', method='auto'):
"""Cross-correlate two N-dimensional arrays."""
def convolve2d(in1, in2, mode='full', boundary='fill', fillvalue=0):
"""Convolve two 2-dimensional arrays."""
def correlate2d(in1, in2, mode='full', boundary='fill', fillvalue=0):
"""Cross-correlate two 2-dimensional arrays."""
def sepfir2d(input, hrow, hcol):
"""Convolve with separable 2-D FIR filter."""
def fftconvolve(in1, in2, mode='full', axes=None):
"""Convolve using FFT."""
def oaconvolve(in1, in2, mode='full', axes=None):
"""Convolve using overlap-add method."""
def hilbert(x, N=None, axis=-1):
"""Compute analytic signal using Hilbert transform."""
def hilbert2(x, N=None):
"""Compute 2-D analytic signal using Hilbert transform."""
def decimate(x, q, n=None, ftype='iir', axis=-1, zero_phase=True):
"""Downsample signal after applying anti-aliasing filter."""
def detrend(data, axis=-1, type='linear', bp=0, overwrite_data=False):
"""Remove linear trend from data."""
def resample(x, num, t=None, axis=0, window=None, domain='time'):
"""Resample x to num samples using Fourier method."""
def resample_poly(x, up, down, axis=0, window='kaiser', padtype='constant', cval=None):
"""Resample using polyphase filtering."""Mathematical special functions for advanced scientific computing.
def gamma(z, out=None):
"""Gamma function."""
def gammaln(z, out=None):
"""Logarithm of gamma function."""
def digamma(z, out=None):
"""Digamma function (psi function)."""
def polygamma(n, z, out=None):
"""Polygamma function."""
def beta(a, b, out=None):
"""Beta function."""
def betaln(a, b, out=None):
"""Logarithm of beta function."""
def erf(z, out=None):
"""Error function."""
def erfc(z, out=None):
"""Complementary error function."""
def erfinv(z, out=None):
"""Inverse error function."""
def erfcinv(z, out=None):
"""Inverse complementary error function."""
def j0(z):
"""Bessel function of first kind of order 0."""
def j1(z):
"""Bessel function of first kind of order 1."""
def jv(v, z):
"""Bessel function of first kind of real order."""
def y0(z):
"""Bessel function of second kind of order 0."""
def y1(z):
"""Bessel function of second kind of order 1."""
def yv(v, z):
"""Bessel function of second kind of real order."""
def i0(z):
"""Modified Bessel function of first kind of order 0."""
def i1(z):
"""Modified Bessel function of first kind of order 1."""
def iv(v, z):
"""Modified Bessel function of first kind of real order."""
def k0(z):
"""Modified Bessel function of second kind of order 0."""
def k1(z):
"""Modified Bessel function of second kind of order 1."""
def kv(v, z):
"""Modified Bessel function of second kind of real order."""
def logsumexp(a, axis=None, b=None, keepdims=False, return_sign=False):
"""Compute log of sum of exponentials."""
def softmax(x, axis=None):
"""Compute softmax function."""
def log_softmax(x, axis=None):
"""Compute log-softmax function."""Spatial algorithms and distance computations.
def distance_matrix(x, y, p=2, threshold=1000000):
"""
Compute distance matrix between arrays.
Parameters:
- x: array_like, input array of size m by k
- y: array_like, input array of size n by k
- p: float, p-norm for distance calculation
- threshold: int, use dense computation if product m*n < threshold
Returns:
cupy.ndarray: Distance matrix of shape (m, n)
"""
def pdist(X, metric='euclidean', out=None):
"""Pairwise distances between observations."""
def cdist(XA, XB, metric='euclidean', out=None):
"""Distances between each pair of observations."""
def squareform(X, force='no', checks=True):
"""Convert between condensed and square distance matrices."""import cupy as cp
import cupyx.scipy.linalg as linalg
# Solve linear system
A = cp.random.random((1000, 1000))
b = cp.random.random(1000)
x = linalg.solve(A, b)
# Matrix decompositions
L, P = linalg.lu_factor(A)
solution = linalg.lu_solve((L, P), b)
# Cholesky decomposition for positive definite matrices
symmetric_A = cp.dot(A, A.T) # Make positive definite
chol = linalg.cholesky(symmetric_A, lower=True)
# SVD decomposition
U, s, Vh = linalg.svd(A, full_matrices=False)
# Eigenvalue problems
eigenvals, eigenvecs = linalg.eigh(symmetric_A)import cupyx.scipy.sparse as sparse
# Create sparse matrices
data = cp.array([1, 2, 3, 4, 5])
row = cp.array([0, 0, 1, 2, 2])
col = cp.array([0, 2, 1, 0, 2])
coo = sparse.coo_matrix((data, (row, col)), shape=(3, 3))
# Convert between formats
csr = coo.tocsr()
csc = coo.tocsc()
# Sparse matrix operations
identity = sparse.identity(1000, format='csr')
random_sparse = sparse.random(5000, 5000, density=0.01, format='csr')
# Sparse-dense operations
dense_vector = cp.random.random(5000)
result = random_sparse.dot(dense_vector)
# Solve sparse linear system
b_sparse = cp.random.random(5000)
x_sparse = sparse.linalg.spsolve(random_sparse, b_sparse)import cupyx.scipy.fft as fft
# 1D FFT
signal_1d = cp.sin(2 * cp.pi * cp.arange(1000) * 0.1)
fft_1d = fft.fft(signal_1d)
ifft_1d = fft.ifft(fft_1d)
# 2D FFT for images
image = cp.random.random((512, 512))
fft_2d = fft.fft2(image)
shifted_fft = fft.fftshift(fft_2d)
# Real FFT for real-valued signals
real_signal = cp.random.random(1000)
rfft_result = fft.rfft(real_signal)
irfft_result = fft.irfft(rfft_result)
# Frequency analysis
frequencies = fft.fftfreq(len(signal_1d))
power_spectrum = cp.abs(fft_1d) ** 2import cupyx.scipy.signal as signal
# Convolution and correlation
sig1 = cp.random.random(1000)
sig2 = cp.random.random(100)
convolved = signal.convolve(sig1, sig2, mode='same')
correlated = signal.correlate(sig1, sig2, mode='same')
# 2D image filtering
image = cp.random.random((256, 256))
kernel = cp.array([[1, 2, 1], [2, 4, 2], [1, 2, 1]]) / 16 # Gaussian blur
filtered = signal.convolve2d(image, kernel, mode='same')
# Hilbert transform for analytic signals
analytic_signal = signal.hilbert(sig1)
amplitude = cp.abs(analytic_signal)
phase = cp.angle(analytic_signal)
# Signal resampling
upsampled = signal.resample(sig1, 2000) # Upsample by factor of 2
decimated = signal.decimate(sig1, 2) # Downsample by factor of 2import cupyx.scipy.special as special
# Gamma and related functions
x = cp.linspace(0.1, 5, 1000)
gamma_vals = special.gamma(x)
gammaln_vals = special.gammaln(x)
# Error functions
z = cp.linspace(-3, 3, 1000)
erf_vals = special.erf(z)
erfc_vals = special.erfc(z)
# Bessel functions
bessel_j0 = special.j0(x)
bessel_j1 = special.j1(x)
modified_bessel_i0 = special.i0(x)
# Log-sum-exp for numerical stability
logits = cp.random.normal(0, 1, (100, 10))
logsumexp_result = special.logsumexp(logits, axis=1)
softmax_result = special.softmax(logits, axis=1)import cupyx.scipy.spatial as spatial
# Distance matrix computation
points_a = cp.random.random((1000, 3)) # 1000 points in 3D
points_b = cp.random.random((500, 3)) # 500 points in 3D
# Pairwise distances
distances = spatial.distance_matrix(points_a, points_b)
# All pairwise distances within single set
pairwise_dist = spatial.pdist(points_a)
square_dist = spatial.squareform(pairwise_dist)
# Different distance metrics
manhattan_dist = spatial.cdist(points_a, points_b, metric='cityblock')# Complete signal processing workflow
import cupy as cp
import cupyx.scipy as scipy
# Generate noisy signal
t = cp.linspace(0, 1, 1000)
clean_signal = cp.sin(2 * cp.pi * 10 * t) + 0.5 * cp.sin(2 * cp.pi * 20 * t)
noise = 0.1 * cp.random.normal(size=len(t))
noisy_signal = clean_signal + noise
# FFT-based filtering
fft_signal = scipy.fft.fft(noisy_signal)
frequencies = scipy.fft.fftfreq(len(t), t[1] - t[0])
# Apply low-pass filter
cutoff = 25 # Hz
fft_filtered = fft_signal.copy()
fft_filtered[cp.abs(frequencies) > cutoff] = 0
# Inverse FFT
filtered_signal = scipy.fft.ifft(fft_filtered).real
# Compute power spectral density
power_spectrum = cp.abs(fft_signal) ** 2
# Statistical analysis of results
snr_improvement = 20 * cp.log10(
cp.std(clean_signal) / cp.std(filtered_signal - clean_signal)
)Install with Tessl CLI
npx tessl i tessl/pypi-cupy-cuda110