NumPy & SciPy-compatible GPU-accelerated computing library for CUDA 11.2 environments
—
GPU-accelerated linear algebra operations providing high-performance matrix computations, decompositions, eigenvalue analysis, and equation solving using cuBLAS, cuSOLVER, and custom CUDA implementations.
High-performance matrix multiplication 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: ndarray, optional output array
Returns:
cupy.ndarray, dot product result
"""
def matmul(x1, x2, out=None):
"""
Matrix product of two arrays.
Parameters:
- x1: array-like, first input array
- x2: array-like, second input array
- out: ndarray, optional output array
Returns:
cupy.ndarray, matrix product
"""
def inner(a, b):
"""
Inner product of two arrays.
Parameters:
- a: array-like, first input array
- b: array-like, second input array
Returns:
cupy.ndarray, inner product
"""
def outer(a, b, out=None):
"""
Compute outer product of two vectors.
Parameters:
- a: array-like, first input vector
- b: array-like, second input vector
- out: ndarray, optional output array
Returns:
cupy.ndarray, outer product
"""
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
"""
def cross(a, b, axisa=-1, axisb=-1, axisc=-1, axis=None):
"""
Cross product of two arrays.
Parameters:
- a: array-like, first input array
- b: array-like, second input array
- axisa: int, axis of a along which to take cross product
- axisb: int, axis of b along which to take cross product
- axisc: int, axis of output along which cross product is computed
- axis: int, unified axis specification
Returns:
cupy.ndarray, cross product
"""
def tensordot(a, b, axes=2):
"""
Compute tensor dot product along specified axes.
Parameters:
- a: array-like, first tensor
- b: array-like, second tensor
- axes: int or array-like, axes to sum over
Returns:
cupy.ndarray, tensor dot product
"""
def vdot(a, b):
"""
Return dot product of two vectors.
Parameters:
- a: array-like, first input vector
- b: array-like, second input vector
Returns:
scalar, dot product of flattened arrays
"""
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 subscripts
- operands: sequence of arrays
- out: ndarray, optional output array
- dtype: data type, optional
- order: memory layout
- casting: casting mode
- optimize: bool or str, optimization strategy
Returns:
cupy.ndarray, Einstein sum result
"""Advanced matrix factorizations for numerical analysis and solving linear systems.
def linalg.svd(a, full_matrices=True, compute_uv=True, hermitian=False):
"""
Singular Value Decomposition.
Parameters:
- a: array-like, input matrix
- full_matrices: bool, return full-sized U and V matrices
- compute_uv: bool, compute U and V in addition to s
- hermitian: bool, assume input is Hermitian
Returns:
tuple, (U, s, Vh) where a = U @ diag(s) @ Vh
"""
def linalg.qr(a, mode='reduced'):
"""
QR decomposition of matrix.
Parameters:
- a: array-like, input matrix
- mode: str, decomposition mode ('reduced', 'complete', 'economic', 'raw')
Returns:
tuple, (Q, R) where a = Q @ R
"""
def linalg.cholesky(a):
"""
Cholesky decomposition of positive-definite matrix.
Parameters:
- a: array-like, positive-definite matrix
Returns:
cupy.ndarray, lower triangular Cholesky factor L where a = L @ L.T
"""Eigenvalue computations for symmetric and Hermitian matrices.
def linalg.eigh(a, UPLO='L'):
"""
Eigenvalues and eigenvectors of symmetric/Hermitian matrix.
Parameters:
- a: array-like, symmetric or Hermitian matrix
- UPLO: str, use upper ('U') or lower ('L') triangle
Returns:
tuple, (eigenvalues, eigenvectors)
"""
def linalg.eigvalsh(a, UPLO='L'):
"""
Eigenvalues of symmetric/Hermitian matrix.
Parameters:
- a: array-like, symmetric or Hermitian matrix
- UPLO: str, use upper ('U') or lower ('L') triangle
Returns:
cupy.ndarray, eigenvalues in ascending order
"""Functions for computing matrix norms, determinants, and other matrix properties.
def linalg.norm(x, ord=None, axis=None, keepdims=False):
"""
Matrix or vector norm.
Parameters:
- x: array-like, input array
- ord: order of norm (None, int, inf, -inf, 'fro', 'nuc')
- axis: int or tuple of ints, axis along which to compute norm
- keepdims: bool, keep reduced dimensions
Returns:
cupy.ndarray, norm of input
"""
def linalg.det(a):
"""
Compute determinant of array.
Parameters:
- a: array-like, square matrix
Returns:
cupy.ndarray, determinant of input matrix
"""
def linalg.slogdet(a):
"""
Compute sign and logarithm of determinant.
Parameters:
- a: array-like, square matrix
Returns:
tuple, (sign, logdet) where det = sign * exp(logdet)
"""
def linalg.matrix_rank(M, tol=None, hermitian=False):
"""
Return matrix rank using SVD method.
Parameters:
- M: array-like, input matrix
- tol: float, threshold for small singular values
- hermitian: bool, assume M is 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, diagonal offset
- axis1: int, first axis for 2-D sub-arrays
- axis2: int, second axis for 2-D sub-arrays
- dtype: data type of output
- out: ndarray, optional output array
Returns:
cupy.ndarray, sum of diagonal elements
"""Functions for solving linear systems and matrix inversions.
def linalg.solve(a, b):
"""
Solve linear matrix equation ax = b.
Parameters:
- a: array-like, coefficient matrix
- b: array-like, dependent variable values
Returns:
cupy.ndarray, solution to system ax = b
"""
def linalg.lstsq(a, b, rcond=None):
"""
Return least-squares solution to linear matrix equation.
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)
"""
def linalg.inv(a):
"""
Compute multiplicative inverse of matrix.
Parameters:
- a: array-like, square matrix to invert
Returns:
cupy.ndarray, multiplicative inverse of input
"""
def linalg.pinv(a, rcond=1e-15, hermitian=False):
"""
Compute Moore-Penrose pseudoinverse.
Parameters:
- a: array-like, matrix to pseudoinvert
- rcond: float, cutoff for small singular values
- hermitian: bool, assume a is Hermitian
Returns:
cupy.ndarray, pseudoinverse of input
"""
def linalg.tensorsolve(a, b, axes=None):
"""
Solve tensor equation a x = b for x.
Parameters:
- a: array-like, coefficient tensor
- b: array-like, dependent variable tensor
- axes: tuple of ints, axes to reorder in a
Returns:
cupy.ndarray, solution tensor
"""
def linalg.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 input tensor
"""Functions for computing matrix powers and related operations.
def linalg.matrix_power(a, n):
"""
Raise square matrix to integer power.
Parameters:
- a: array-like, square matrix
- n: int, power to raise matrix to
Returns:
cupy.ndarray, a raised to power n
"""import cupy as cp
# Create matrices
A = cp.random.random((1000, 1000))
B = cp.random.random((1000, 1000))
x = cp.random.random(1000)
# Matrix multiplication
C = cp.dot(A, B) # or A @ B
y = cp.dot(A, x) # Matrix-vector product
# Matrix operations
At = A.T # Transpose
trace_A = cp.trace(A)
norm_A = cp.linalg.norm(A)
det_A = cp.linalg.det(A)import cupy as cp
# Solve Ax = b
A = cp.random.random((100, 100))
b = cp.random.random(100)
# Direct solution
x = cp.linalg.solve(A, b)
# Least squares solution for overdetermined system
A_over = cp.random.random((150, 100))
b_over = cp.random.random(150)
x_ls, residuals, rank, s = cp.linalg.lstsq(A_over, b_over, rcond=None)
# Matrix inversion
A_inv = cp.linalg.inv(A)
A_pinv = cp.linalg.pinv(A_over) # Pseudoinverseimport cupy as cp
# Create symmetric positive definite matrix
A = cp.random.random((100, 100))
A = A @ A.T + cp.eye(100) # Ensure positive definite
# Cholesky decomposition
L = cp.linalg.cholesky(A)
# Verify: A ≈ L @ L.T
# SVD decomposition
U, s, Vh = cp.linalg.svd(A, full_matrices=False)
# Verify: A ≈ U @ cp.diag(s) @ Vh
# QR decomposition
Q, R = cp.linalg.qr(A)
# Verify: A ≈ Q @ R
# Eigenvalue decomposition
eigenvals, eigenvecs = cp.linalg.eigh(A)import cupy as cp
# Einstein summation
A = cp.random.random((10, 20))
B = cp.random.random((20, 30))
C = cp.random.random((30, 10))
# Matrix chain multiplication using einsum
result = cp.einsum('ij,jk,kl->il', A, B, C)
# Tensor operations
T1 = cp.random.random((5, 4, 3))
T2 = cp.random.random((3, 2))
# Tensor contraction
contracted = cp.tensordot(T1, T2, axes=([2], [0]))
# Kronecker product
small_A = cp.array([[1, 2], [3, 4]])
small_B = cp.array([[5, 6], [7, 8]])
kron_product = cp.kron(small_A, small_B)import cupy as cp
# Use appropriate data types
A_float32 = cp.random.random((2000, 2000), dtype=cp.float32)
B_float32 = cp.random.random((2000, 2000), dtype=cp.float32)
# Preallocate output arrays
C = cp.empty((2000, 2000), dtype=cp.float32)
cp.dot(A_float32, B_float32, out=C)
# Use in-place operations when possible
A_float32 *= 2.0 # In-place scaling
# Optimize memory layout
A_fortran = cp.asfortranarray(A_float32) # For column-major operationsInstall with Tessl CLI
npx tessl i tessl/pypi-cupy-cuda112