CuPy: NumPy & SciPy for GPU (CUDA 10.1 version)
—
GPU-accelerated linear algebra operations powered by cuBLAS and cuSOLVER libraries. These functions provide high-performance matrix operations, decompositions, eigenvalue problems, and linear system solving with NumPy-compatible interfaces.
Core linear algebra operations for matrix multiplication, dot products, and tensor operations.
def dot(a, b, out=None):
"""
Dot product of two arrays.
Parameters:
- a: array-like, first input array
- b: array-like, second input array
- out: array, output array, optional
Returns:
cupy.ndarray: Dot product on GPU
"""
def vdot(a, b):
"""
Vector dot product.
Parameters:
- a: array-like, first input vector
- b: array-like, second input vector
Returns:
scalar: Dot product of flattened arrays on GPU
"""
def inner(a, b):
"""
Inner product of vectors.
Parameters:
- a: array-like, first input array
- b: array-like, second input array
Returns:
cupy.ndarray: Inner product on GPU
"""
def outer(a, b, out=None):
"""
Outer product of vectors.
Parameters:
- a: array-like, first input vector
- b: array-like, second input vector
- out: array, output array, optional
Returns:
cupy.ndarray: Outer product matrix on GPU
"""
def matmul(x1, x2, out=None, **kwargs):
"""
Matrix multiplication.
Parameters:
- x1: array-like, first input array
- x2: array-like, second input array
- out: array, output array, optional
Returns:
cupy.ndarray: Matrix product on GPU
"""
def tensordot(a, b, axes=2):
"""
Tensor dot product along specified axes.
Parameters:
- a: array-like, first input array
- b: array-like, second input array
- axes: int or tuple, axes to sum over
Returns:
cupy.ndarray: Tensor dot product on GPU
"""
def kron(a, b):
"""
Kronecker product of two arrays.
Parameters:
- a: array-like, first input array
- b: array-like, second input array
Returns:
cupy.ndarray: Kronecker product on GPU
"""
def cross(a, b, axisa=-1, axisb=-1, axisc=-1, axis=None):
"""
Cross product of vectors.
Parameters:
- a: array-like, first input array
- b: array-like, second input array
- axisa: int, axis of first array
- axisb: int, axis of second array
- axisc: int, axis of output
- axis: int, axis for cross product
Returns:
cupy.ndarray: Cross product on GPU
"""
def einsum(subscripts, *operands, out=None, dtype=None, order='K', casting='safe', optimize=False):
"""
Einstein summation convention.
Parameters:
- subscripts: str, Einstein summation subscripts
- operands: arrays, input arrays
- out: array, output array, optional
- dtype: data type, optional
- order: memory layout, optional
- casting: casting rule, optional
- optimize: optimization strategy, optional
Returns:
cupy.ndarray: Einstein sum result on GPU
"""Advanced matrix decomposition methods for numerical analysis and solving linear systems.
def cholesky(a):
"""
Cholesky decomposition of positive definite matrix.
Parameters:
- a: array-like, positive definite matrix
Returns:
cupy.ndarray: Lower triangular Cholesky factor on GPU
"""
def qr(a, mode='reduced'):
"""
QR factorization.
Parameters:
- a: array-like, input matrix
- mode: str, decomposition mode ('reduced', 'complete', 'r', 'raw')
Returns:
tuple: Q and R matrices on GPU
"""
def svd(a, full_matrices=True, compute_uv=True, hermitian=False):
"""
Singular Value Decomposition.
Parameters:
- a: array-like, input matrix
- full_matrices: bool, compute full-sized U and Vh
- compute_uv: bool, compute U and Vh matrices
- hermitian: bool, assume Hermitian matrix
Returns:
tuple: U, s, Vh matrices and singular values on GPU
"""Compute eigenvalues and eigenvectors for symmetric and Hermitian matrices.
def eigh(a, UPLO='L'):
"""
Eigenvalues and eigenvectors of Hermitian matrix.
Parameters:
- a: array-like, Hermitian matrix
- UPLO: str, upper ('U') or lower ('L') triangle
Returns:
tuple: eigenvalues and eigenvectors on GPU
"""
def eigvalsh(a, UPLO='L'):
"""
Eigenvalues of Hermitian matrix.
Parameters:
- a: array-like, Hermitian matrix
- UPLO: str, upper ('U') or lower ('L') triangle
Returns:
cupy.ndarray: eigenvalues on GPU
"""Functions for computing matrix norms, determinants, and other matrix properties.
def norm(x, ord=None, axis=None, keepdims=False):
"""
Matrix or vector norm.
Parameters:
- x: array-like, input array
- ord: norm order, optional
- axis: int or tuple, axes for norm computation
- keepdims: bool, keep dimensions
Returns:
cupy.ndarray: norm value on GPU
"""
def det(a):
"""
Determinant of matrix.
Parameters:
- a: array-like, square matrix
Returns:
cupy.ndarray: determinant on GPU
"""
def slogdet(a):
"""
Sign and logarithm of determinant.
Parameters:
- a: array-like, square matrix
Returns:
tuple: sign and log determinant on GPU
"""
def matrix_rank(M, tol=None, hermitian=False):
"""
Matrix rank using SVD.
Parameters:
- M: array-like, input matrix
- tol: float, tolerance for rank determination
- hermitian: bool, assume Hermitian matrix
Returns:
int: matrix rank
"""
def trace(a, offset=0, axis1=0, axis2=1, dtype=None, out=None):
"""
Sum of diagonal elements.
Parameters:
- a: array-like, input array
- offset: int, diagonal offset
- axis1: int, first axis
- axis2: int, second axis
- dtype: data type, optional
- out: array, output array, optional
Returns:
cupy.ndarray: trace on GPU
"""Functions for solving linear equations and matrix inversion.
def solve(a, b):
"""
Solve linear system ax = b.
Parameters:
- a: array-like, coefficient matrix
- b: array-like, dependent variable values
Returns:
cupy.ndarray: solution x on GPU
"""
def tensorsolve(a, b, axes=None):
"""
Solve tensor equation ax = b for x.
Parameters:
- a: array-like, coefficient tensor
- b: array-like, dependent variable tensor
- axes: tuple, axes to sum over
Returns:
cupy.ndarray: solution tensor on GPU
"""
def lstsq(a, b, rcond=None):
"""
Least squares solution to linear system.
Parameters:
- a: array-like, coefficient matrix
- b: array-like, dependent variable values
- rcond: float, cutoff for small singular values
Returns:
tuple: solution, residuals, rank, singular values on GPU
"""
def inv(a):
"""
Matrix inverse.
Parameters:
- a: array-like, square matrix
Returns:
cupy.ndarray: matrix inverse on GPU
"""
def pinv(a, rcond=1e-15, hermitian=False):
"""
Moore-Penrose pseudoinverse.
Parameters:
- a: array-like, input matrix
- rcond: float, cutoff for small singular values
- hermitian: bool, assume Hermitian matrix
Returns:
cupy.ndarray: pseudoinverse on GPU
"""
def tensorinv(a, ind=2):
"""
Tensor inverse.
Parameters:
- a: array-like, input tensor
- ind: int, number of indices
Returns:
cupy.ndarray: tensor inverse on GPU
"""Specialized linear algebra functions for advanced mathematical computations.
def matrix_power(a, n):
"""
Raise matrix to integer power.
Parameters:
- a: array-like, square matrix
- n: int, power exponent
Returns:
cupy.ndarray: matrix power on GPU
"""import cupy as cp
# Create matrices
A = cp.random.random((3, 3)).astype(cp.float32)
B = cp.random.random((3, 3)).astype(cp.float32)
x = cp.random.random(3).astype(cp.float32)
# Matrix multiplication
C = cp.dot(A, B)
C_matmul = A @ B # Equivalent using @ operator
# Vector operations
dot_product = cp.dot(x, x)
outer_product = cp.outer(x, x)
# Matrix-vector multiplication
y = cp.dot(A, x)# Solve linear system Ax = b
A = cp.array([[3, 2, -1], [2, -2, 4], [-1, 0.5, -1]], dtype=cp.float32)
b = cp.array([1, -2, 0], dtype=cp.float32)
# Direct solution
x = cp.linalg.solve(A, b)
# Least squares solution for overdetermined system
A_over = cp.random.random((5, 3)).astype(cp.float32)
b_over = cp.random.random(5).astype(cp.float32)
x_lstsq, residuals, rank, s = cp.linalg.lstsq(A_over, b_over)
# Matrix inversion
A_inv = cp.linalg.inv(A)
identity_check = cp.dot(A, A_inv) # Should be close to identity# Create symmetric positive definite matrix
A_spd = cp.random.random((4, 4)).astype(cp.float32)
A_spd = cp.dot(A_spd, A_spd.T) + cp.eye(4) * 0.1
# Cholesky decomposition
L = cp.linalg.cholesky(A_spd)
# QR decomposition
A_rect = cp.random.random((5, 3)).astype(cp.float32)
Q, R = cp.linalg.qr(A_rect)
# SVD decomposition
U, s, Vh = cp.linalg.svd(A_rect)
# Eigenvalue decomposition for symmetric matrix
eigenvals, eigenvecs = cp.linalg.eigh(A_spd)# Einstein summation for complex tensor operations
A = cp.random.random((3, 4, 5))
B = cp.random.random((4, 5, 6))
# Matrix multiplication using einsum
C = cp.einsum('ijk,jkl->il', A, B)
# Batch matrix multiplication
batch_A = cp.random.random((10, 3, 3))
batch_B = cp.random.random((10, 3, 3))
batch_C = cp.einsum('bij,bjk->bik', batch_A, batch_B)
# Matrix properties
det_A = cp.linalg.det(A_spd)
norm_A = cp.linalg.norm(A_spd, ord='fro') # Frobenius norm
trace_A = cp.trace(A_spd)Install with Tessl CLI
npx tessl i tessl/pypi-cupy-cuda101