CuPy: NumPy & SciPy for GPU - A NumPy/SciPy-compatible array library for GPU-accelerated computing with Python, specifically built for AMD ROCm 4.3 platform
—
GPU-accelerated linear algebra operations powered by cuBLAS and cuSOLVER libraries. Provides high-performance matrix operations, decompositions, and equation solving with full NumPy compatibility.
Fundamental linear algebra operations for matrix and vector multiplication.
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: cupy.ndarray, output array
Returns:
cupy.ndarray: Dot product result
- 1D x 1D: inner product
- 2D x 2D: matrix multiplication
- 1D x 2D or 2D x 1D: matrix-vector product
"""
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: cupy.ndarray, output array
Returns:
cupy.ndarray: Matrix product with proper broadcasting
"""
def inner(a, b):
"""
Inner product of two arrays.
Parameters:
- a, b: array-like, input arrays
Returns:
cupy.ndarray: Inner product over last axis
"""
def outer(a, b, out=None):
"""
Compute outer product of two vectors.
Parameters:
- a: array-like, first input vector (M,)
- b: array-like, second input vector (N,)
- out: cupy.ndarray, output array
Returns:
cupy.ndarray: Outer product (M, N)
"""
def tensordot(a, b, axes=2):
"""
Compute tensor dot product along specified axes.
Parameters:
- a, b: array-like, input arrays
- axes: int or (2,) array-like, axes to sum over
Returns:
cupy.ndarray: Tensor dot product
"""
def kron(a, b):
"""
Kronecker product of two arrays.
Parameters:
- a, b: array-like, input arrays
Returns:
cupy.ndarray: Kronecker product
"""
def cross(a, b, axisa=-1, axisb=-1, axisc=-1, axis=None):
"""
Cross product of two (arrays of) vectors.
Parameters:
- a, b: array-like, input arrays
- axisa, axisb: int, axis of a and b containing vectors
- axisc: int, axis of output containing cross product
- axis: int, if specified, defines vector axis for all arguments
Returns:
cupy.ndarray: Cross product
"""
def vdot(a, b):
"""
Return dot product of two vectors (flattens arrays first).
Parameters:
- a, b: array-like, input arrays
Returns:
scalar: Dot product of flattened arrays
"""Matrix factorizations for solving linear systems and analyzing matrix properties.
def cholesky(a):
"""
Cholesky decomposition of positive definite matrix.
Parameters:
- a: array-like, positive definite matrix (..., M, M)
Returns:
cupy.ndarray: Lower triangular Cholesky factor L where a = L @ L.T
Raises:
cupy.linalg.LinAlgError: If matrix is not positive definite
"""
def qr(a, mode='reduced'):
"""
Compute QR decomposition of matrix.
Parameters:
- a: array-like, input matrix (..., M, N)
- mode: {'reduced', 'complete'}, decomposition mode
Returns:
tuple: (Q, R) where a = Q @ R
- Q: orthogonal matrix (..., M, K)
- R: upper triangular matrix (..., K, N)
- K = min(M, N) for reduced mode, K = M for complete mode
"""
def svd(a, full_matrices=True, compute_uv=True, hermitian=False):
"""
Singular Value Decomposition.
Parameters:
- a: array-like, input matrix (..., M, N)
- full_matrices: bool, whether to compute full U and V matrices
- compute_uv: bool, whether to compute U and V in addition to s
- hermitian: bool, if True, a is assumed Hermitian
Returns:
tuple or cupy.ndarray:
- If compute_uv=True: (U, s, Vh) where a = U @ diag(s) @ Vh
- If compute_uv=False: s (singular values only)
"""Compute eigenvalues and eigenvectors for symmetric/Hermitian matrices.
def eigh(a, UPLO='L'):
"""
Eigenvalues and eigenvectors of Hermitian/symmetric matrix.
Parameters:
- a: array-like, Hermitian/symmetric matrix (..., M, M)
- UPLO: {'L', 'U'}, use lower or upper triangle
Returns:
tuple: (eigenvalues, eigenvectors)
- eigenvalues: (..., M) real-valued eigenvalues in ascending order
- eigenvectors: (..., M, M) normalized eigenvectors as columns
"""
def eigvalsh(a, UPLO='L'):
"""
Eigenvalues of Hermitian/symmetric matrix.
Parameters:
- a: array-like, Hermitian/symmetric matrix (..., M, M)
- UPLO: {'L', 'U'}, use lower or upper triangle
Returns:
cupy.ndarray: Eigenvalues (..., M) in ascending order
"""Functions to compute matrix norms, determinants, and ranks.
def norm(x, ord=None, axis=None, keepdims=False):
"""
Matrix or vector norm.
Parameters:
- x: array-like, input array
- ord: {None, 'fro', 'nuc', int, float}, norm type
- axis: None or int or 2-tuple of ints, axes for norm computation
- keepdims: bool, whether to keep reduced dimensions
Returns:
cupy.ndarray: Norm of x
"""
def det(a):
"""
Compute determinant of array.
Parameters:
- a: array-like, square matrix (..., M, M)
Returns:
cupy.ndarray: Determinant of a
"""
def slogdet(a):
"""
Compute sign and natural logarithm of determinant.
Parameters:
- a: array-like, square matrix (..., M, M)
Returns:
tuple: (sign, logdet)
- sign: sign of determinant
- logdet: natural log of absolute value of determinant
"""
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, if True, M is assumed Hermitian
Returns:
int: Rank of matrix
"""
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, offset from main diagonal
- axis1, axis2: int, axes to take diagonal from
- dtype: data type, type of returned array
- out: cupy.ndarray, output array
Returns:
cupy.ndarray: Sum along diagonal
"""Solve linear equations and compute matrix inverses.
def solve(a, b):
"""
Solve linear matrix equation ax = b.
Parameters:
- a: array-like, coefficient matrix (..., M, M)
- b: array-like, ordinate values (..., M) or (..., M, K)
Returns:
cupy.ndarray: Solution x to ax = b
Raises:
cupy.linalg.LinAlgError: If a is singular
"""
def lstsq(a, b, rcond=None):
"""
Compute least-squares solution to linear matrix equation.
Parameters:
- a: array-like, coefficient matrix (M, N)
- b: array-like, ordinate values (M,) or (M, K)
- rcond: float, cutoff for singular values
Returns:
tuple: (x, residuals, rank, s)
- x: least-squares solution
- residuals: sum of squared residuals
- rank: rank of a
- s: singular values of a
"""
def inv(a):
"""
Compute multiplicative inverse of matrix.
Parameters:
- a: array-like, square matrix (..., M, M)
Returns:
cupy.ndarray: Multiplicative inverse of a
Raises:
cupy.linalg.LinAlgError: If a is singular
"""
def pinv(a, rcond=1e-15, hermitian=False):
"""
Compute Moore-Penrose pseudoinverse of matrix.
Parameters:
- a: array-like, matrix to invert (..., M, N)
- rcond: float, cutoff for singular values
- hermitian: bool, if True, a is assumed Hermitian
Returns:
cupy.ndarray: Pseudoinverse of a (..., N, M)
"""
def tensorsolve(a, b, axes=None):
"""
Solve tensor equation a x = b for x.
Parameters:
- a: array-like, coefficient tensor
- b: array-like, right-hand side tensor
- axes: list of ints, axes in a to reorder to right
Returns:
cupy.ndarray: Solution tensor x
"""
def tensorinv(a, ind=2):
"""
Compute inverse of N-dimensional array.
Parameters:
- a: array-like, tensor to invert
- ind: int, number of first indices that are involved in inverse
Returns:
cupy.ndarray: Inverse of a
"""Compute integer powers of square matrices.
def matrix_power(a, n):
"""
Raise square matrix to integer power.
Parameters:
- a: array-like, square matrix (..., M, M)
- n: int, exponent (can be negative)
Returns:
cupy.ndarray: a raised to power n
Raises:
cupy.linalg.LinAlgError: If n < 0 and a is singular
"""Advanced tensor operations using Einstein summation notation.
def einsum(subscripts, *operands, out=None, dtype=None, order='K', casting='safe', optimize=False):
"""
Evaluates Einstein summation convention on operands.
Parameters:
- subscripts: str, specifies subscripts for summation
- operands: list of array-like, arrays for operation
- out: cupy.ndarray, output array
- dtype: data type, output data type
- order: {'C', 'F', 'A', 'K'}, memory layout
- casting: {'no', 'equiv', 'safe', 'same_kind', 'unsafe'}, casting rule
- optimize: {False, True, 'greedy', 'optimal'}, path optimization
Returns:
cupy.ndarray: Calculation based on Einstein summation convention
"""import cupy as cp
# Matrix multiplication
A = cp.random.rand(1000, 500)
B = cp.random.rand(500, 800)
C = cp.dot(A, B) # or cp.matmul(A, B)
# Vector operations
x = cp.array([1, 2, 3])
y = cp.array([4, 5, 6])
dot_product = cp.dot(x, y)
cross_product = cp.cross(x, y)
# Matrix decompositions
symmetric_matrix = A @ A.T
eigenvals, eigenvecs = cp.linalg.eigh(symmetric_matrix)
U, s, Vh = cp.linalg.svd(A)
Q, R = cp.linalg.qr(A)
# Solving linear systems
x = cp.linalg.solve(symmetric_matrix, cp.random.rand(1000))
x_lstsq = cp.linalg.lstsq(A, cp.random.rand(1000))[0]
# Matrix properties
det_A = cp.linalg.det(symmetric_matrix)
norm_A = cp.linalg.norm(A)
rank_A = cp.linalg.matrix_rank(A)import cupy as cp
# Einstein summation for complex tensor operations
a = cp.random.rand(3, 4, 5)
b = cp.random.rand(4, 5, 6)
# Matrix multiplication using einsum
result = cp.einsum('ijk,jkl->il', a, b)
# Batch operations
batch_matrices = cp.random.rand(10, 100, 100)
batch_vectors = cp.random.rand(10, 100)
# Solve multiple systems simultaneously
solutions = cp.linalg.solve(batch_matrices, batch_vectors)
# Compute eigenvalues for multiple matrices
eigenvalues = cp.linalg.eigvalsh(batch_matrices)class LinAlgError(Exception):
"""
Exception raised for linear algebra errors.
Raised when:
- Matrix is singular (not invertible)
- Matrix is not positive definite (for Cholesky)
- Convergence fails in iterative algorithms
"""Install with Tessl CLI
npx tessl i tessl/pypi-cupy-rocm-4-3