CuPy: NumPy & SciPy-compatible array library for GPU-accelerated computing with Python that provides a drop-in replacement for NumPy/SciPy on NVIDIA CUDA platforms.
—
GPU-accelerated linear algebra operations including matrix products, decompositions, eigenvalue computations, and system solving through cuBLAS and cuSOLVER integration. These functions provide high-performance linear algebra capabilities optimized for GPU execution.
Basic matrix multiplication and products between arrays.
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: ndarray, output array
Returns:
cupy.ndarray: dot product result
"""
def matmul(x1, x2, out=None):
"""Matrix product of two arrays following broadcasting rules.
Parameters:
- x1: array-like, first input array
- x2: array-like, second input array
- out: ndarray, output array
Returns:
cupy.ndarray: matrix product
"""
def inner(a, b):
"""Inner product of two arrays."""
def outer(a, b, out=None):
"""Compute outer product of two vectors."""
def tensordot(a, b, axes=2):
"""Compute tensor dot product along specified axes.
Parameters:
- a: array-like, first input array
- b: array-like, second input array
- axes: int or (2,) array-like, axes to sum over
Returns:
cupy.ndarray: tensor dot product
"""
def vdot(a, b):
"""Return dot product of two vectors (flattened if multi-dimensional)."""
def cross(a, b, axisa=-1, axisb=-1, axisc=-1, axis=None):
"""Return cross product of two arrays."""
def kron(a, b):
"""Kronecker product of two arrays."""Einstein summation and advanced tensor operations.
def einsum(subscripts, *operands, out=None, dtype=None, order='K', casting='safe', optimize=False):
"""Evaluate Einstein summation convention on operands.
Parameters:
- subscripts: str, Einstein summation specification
- operands: list of array-like, arrays to operate on
- out: ndarray, output array
- dtype: data-type, type of output array
- order: memory layout order
- casting: casting rule for operations
- optimize: bool or str, optimization strategy
Returns:
cupy.ndarray: result of Einstein summation
"""Matrix norms, traces, and determinants available at the top level.
def trace(a, offset=0, axis1=0, axis2=1, dtype=None, out=None):
"""Return sum along diagonals of array.
Parameters:
- a: array-like, input array
- offset: int, diagonal offset
- axis1: int, first axis for 2-D sub-arrays
- axis2: int, second axis for 2-D sub-arrays
- dtype: data-type, type of output
- out: ndarray, output array
Returns:
cupy.ndarray: sum along diagonal
"""Advanced linear algebra operations in the dedicated linalg module.
def norm(x, ord=None, axis=None, keepdims=False):
"""Matrix or vector norm.
Parameters:
- x: array-like, input array
- ord: int, float, inf, -inf, 'fro', 'nuc', norm order
- axis: int, 2-tuple of ints, axis along which to compute norm
- keepdims: bool, keep dimensions of result same as input
Returns:
cupy.ndarray: norm of the matrix or vector
"""
def det(a):
"""Compute determinant of array.
Parameters:
- a: array-like, input square matrix
Returns:
cupy.ndarray: determinant of matrix
"""
def slogdet(a):
"""Compute sign and logarithm of determinant.
Parameters:
- a: array-like, input square matrix
Returns:
tuple: (sign, logdet) where sign is ±1 and logdet is log(|det(a)|)
"""
def matrix_rank(M, tol=None, hermitian=False):
"""Return matrix rank using SVD method.
Parameters:
- M: array-like, input matrix
- tol: float, threshold for singular values
- hermitian: bool, whether M is Hermitian
Returns:
int: rank of matrix
"""Factorization methods for matrices.
def cholesky(a):
"""Cholesky decomposition of positive-definite matrix.
Parameters:
- a: array-like, Hermitian positive-definite matrix
Returns:
cupy.ndarray: lower triangular Cholesky factor
"""
def qr(a, mode='reduced'):
"""QR decomposition of matrix.
Parameters:
- a: array-like, input matrix
- mode: str, decomposition mode ('reduced', 'complete', 'r', 'raw')
Returns:
tuple: (Q, R) orthogonal and upper triangular matrices
"""
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 matrices
- compute_uv: bool, compute U and Vh matrices
- hermitian: bool, whether a is Hermitian
Returns:
tuple: (U, s, Vh) where a = U @ diag(s) @ Vh
"""Eigenvalue and eigenvector computations for symmetric/Hermitian matrices.
def eigh(a, UPLO='L'):
"""Eigenvalues and eigenvectors of Hermitian/real symmetric matrix.
Parameters:
- a: array-like, Hermitian or real symmetric matrix
- UPLO: str, whether to use upper ('U') or lower ('L') triangle
Returns:
tuple: (eigenvalues, eigenvectors) sorted in ascending order
"""
def eigvalsh(a, UPLO='L'):
"""Eigenvalues of Hermitian/real symmetric matrix.
Parameters:
- a: array-like, Hermitian or real symmetric matrix
- UPLO: str, whether to use upper ('U') or lower ('L') triangle
Returns:
cupy.ndarray: eigenvalues in ascending order
"""Methods for solving linear equations and matrix inversion.
def solve(a, b):
"""Solve linear matrix equation ax = b.
Parameters:
- a: array-like, coefficient matrix
- b: array-like, ordinate values
Returns:
cupy.ndarray: solution to linear system
"""
def lstsq(a, b, rcond=None):
"""Return least-squares solution to linear matrix equation.
Parameters:
- a: array-like, coefficient matrix
- b: array-like, ordinate values
- rcond: float, cutoff for small singular values
Returns:
tuple: (solution, residuals, rank, singular_values)
"""
def inv(a):
"""Compute multiplicative inverse of matrix.
Parameters:
- a: array-like, square matrix to invert
Returns:
cupy.ndarray: multiplicative inverse of a
"""
def pinv(a, rcond=1e-15, hermitian=False):
"""Compute Moore-Penrose pseudoinverse of matrix.
Parameters:
- a: array-like, matrix to invert
- rcond: float, cutoff for small singular values
- hermitian: bool, whether a is Hermitian
Returns:
cupy.ndarray: pseudoinverse of a
"""
def tensorsolve(a, b, axes=None):
"""Solve tensor equation a x = b for x.
Parameters:
- a: array-like, coefficient tensor
- b: array-like, target tensor
- axes: list of ints, axes in a to reorder to right
Returns:
cupy.ndarray: solution tensor
"""
def tensorinv(a, ind=2):
"""Compute inverse of N-dimensional array.
Parameters:
- a: array-like, tensor to invert
- ind: int, number of first indices forming square matrix
Returns:
cupy.ndarray: inverse of a
"""def matrix_power(a, n):
"""Raise square matrix to integer power.
Parameters:
- a: array-like, square matrix
- n: int, exponent (can be negative)
Returns:
cupy.ndarray: a raised to power n
"""import cupy as cp
# Create matrices
A = cp.array([[1, 2], [3, 4]])
B = cp.array([[5, 6], [7, 8]])
v = cp.array([1, 2])
# Matrix multiplication
C = cp.dot(A, B) # Matrix product
Cv = cp.dot(A, v) # Matrix-vector product
# Using matmul (recommended for matrix multiplication)
C_matmul = A @ B # Equivalent to cp.matmul(A, B)import cupy as cp
# Create symmetric positive definite matrix
A = cp.array([[4, 2], [2, 3]])
b = cp.array([1, 2])
# Solve linear system
x = cp.linalg.solve(A, b)
# Matrix decompositions
L = cp.linalg.cholesky(A) # Cholesky decomposition
Q, R = cp.linalg.qr(A) # QR decomposition
U, s, Vh = cp.linalg.svd(A) # SVD decomposition
# Eigenvalue decomposition (for symmetric matrices)
eigenvals, eigenvecs = cp.linalg.eigh(A)import cupy as cp
# Create test matrix
A = cp.random.random((5, 5))
# Matrix properties
det_A = cp.linalg.det(A) # Determinant
rank_A = cp.linalg.matrix_rank(A) # Matrix rank
trace_A = cp.trace(A) # Trace (sum of diagonal)
# Matrix norms
frobenius_norm = cp.linalg.norm(A, 'fro') # Frobenius norm
spectral_norm = cp.linalg.norm(A, 2) # Spectral norm
nuclear_norm = cp.linalg.norm(A, 'nuc') # Nuclear normimport cupy as cp
# Create large system
n = 10000
A = cp.random.random((n, n)) + n * cp.eye(n) # Well-conditioned matrix
b = cp.random.random(n)
# Solve efficiently using GPU
x = cp.linalg.solve(A, b)
# Verify solution
residual = cp.linalg.norm(A @ x - b)
print(f"Residual norm: {residual}")
# For overdetermined systems, use least squares
m = 15000
A_tall = cp.random.random((m, n))
b_tall = cp.random.random(m)
x_ls, residuals, rank, s = cp.linalg.lstsq(A_tall, b_tall)import cupy as cp
# Create arrays
A = cp.random.random((3, 4))
B = cp.random.random((4, 5))
C = cp.random.random((3, 4, 5))
# Matrix multiplication using einsum
result1 = cp.einsum('ij,jk->ik', A, B) # Equivalent to A @ B
# Trace using einsum
result2 = cp.einsum('ii->', A[:3, :3]) # Equivalent to cp.trace(A[:3, :3])
# Batch matrix multiplication
batch_A = cp.random.random((10, 3, 4))
batch_B = cp.random.random((10, 4, 5))
batch_result = cp.einsum('bij,bjk->bik', batch_A, batch_B)
# Element-wise product and sum
result3 = cp.einsum('ijk,ijk->', C, C) # Sum of squares of all elementsInstall with Tessl CLI
npx tessl i tessl/pypi-cupy-cuda113