NumPy & SciPy compatible GPU-accelerated array library for CUDA computing
—
High-performance linear algebra operations leveraging cuBLAS and cuSOLVER libraries for matrix operations, decompositions, and solving linear systems. CuPy provides comprehensive GPU-accelerated linear algebra functionality with NVIDIA's optimized libraries.
Core matrix multiplication and transformation operations.
def dot(a, b, out=None):
"""Dot product of two arrays.
Args:
a: First input array
b: Second input array
out: Output array
Returns:
cupy.ndarray: Dot product result
"""
def matmul(x1, x2, out=None, **kwargs):
"""Matrix product of two arrays.
Args:
x1, x2: Input arrays
out: Output array
**kwargs: Additional arguments
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."""
def kron(a, b):
"""Kronecker product of two arrays."""Calculate matrix properties and various norms.
def norm(x, ord=None, axis=None, keepdims=False):
"""Matrix or vector norm.
Args:
x: Input array
ord: Order of norm (None, 'fro', 'nuc', inf, -inf, 1, -1, 2, -2)
axis: Axis for norm calculation
keepdims: Keep reduced dimensions
Returns:
cupy.ndarray or float: Norm value(s)
"""
def trace(a, offset=0, axis1=0, axis2=1, dtype=None, out=None):
"""Return sum along diagonals of array.
Args:
a: Input array
offset: Diagonal offset
axis1, axis2: Axes to take diagonal from
dtype: Output data type
out: Output array
Returns:
cupy.ndarray: Sum of diagonal elements
"""
def det(a):
"""Compute determinant of array.
Args:
a: Input array
Returns:
cupy.ndarray: Determinant
"""
def slogdet(a):
"""Compute sign and logarithm of determinant.
Args:
a: Input array
Returns:
tuple: (sign, logdet) arrays
"""
def matrix_rank(M, tol=None, hermitian=False):
"""Return matrix rank of array using SVD."""Solve linear systems and matrix equations.
def solve(a, b):
"""Solve linear matrix equation ax = b.
Args:
a: Coefficient matrix
b: Ordinate values
Returns:
cupy.ndarray: Solution to system
"""
def lstsq(a, b, rcond=None):
"""Return least-squares solution to linear matrix equation.
Args:
a: Coefficient matrix
b: Ordinate values
rcond: Cutoff for small singular values
Returns:
tuple: (solution, residuals, rank, singular_values)
"""
def solve_triangular(a, b, lower=False, unit_diagonal=False, overwrite_b=False, debug=None):
"""Solve triangular system of equations."""
def tensorsolve(a, b, axes=None):
"""Solve tensor equation a x = b for x."""Compute matrix inverses and pseudo-inverses.
def inv(a):
"""Compute multiplicative inverse of matrix.
Args:
a: Matrix to invert
Returns:
cupy.ndarray: Inverse matrix
"""
def pinv(a, rcond=1e-15, hermitian=False):
"""Compute Moore-Penrose pseudo-inverse.
Args:
a: Matrix to pseudo-invert
rcond: Cutoff for small singular values
hermitian: Whether matrix is Hermitian
Returns:
cupy.ndarray: Pseudo-inverse matrix
"""
def tensorinv(a, ind=2):
"""Compute 'inverse' of N-dimensional array."""Compute eigenvalues and eigenvectors of matrices.
def eigh(a, UPLO='L'):
"""Return eigenvalues and eigenvectors of Hermitian matrix.
Args:
a: Hermitian matrix
UPLO: Whether to use upper ('U') or lower ('L') triangle
Returns:
tuple: (eigenvalues, eigenvectors)
"""
def eigvalsh(a, UPLO='L'):
"""Compute eigenvalues of Hermitian matrix."""Various matrix factorizations and decompositions.
def svd(a, full_matrices=True, compute_uv=True):
"""Singular Value Decomposition.
Args:
a: Input matrix
full_matrices: Whether to compute full or reduced SVD
compute_uv: Whether to compute U and Vh matrices
Returns:
tuple: (U, s, Vh) or just s if compute_uv=False
"""
def qr(a, mode='reduced'):
"""Compute QR decomposition of matrix.
Args:
a: Input matrix
mode: Decomposition mode ('reduced', 'complete', 'r', 'raw')
Returns:
tuple: (Q, R) matrices or modified based on mode
"""
def cholesky(a):
"""Cholesky decomposition of positive-definite matrix.
Args:
a: Positive-definite matrix
Returns:
cupy.ndarray: Lower triangular Cholesky factor
"""Specialized operations for specific matrix types.
def matrix_power(a, n):
"""Raise square matrix to integer power.
Args:
a: Square matrix
n: Integer power
Returns:
cupy.ndarray: Matrix power a^n
"""Operations specific to vectors and 1D arrays.
def cross(a, b, axisa=-1, axisb=-1, axisc=-1, axis=None):
"""Return cross product of two vectors.
Args:
a, b: Input vectors
axisa, axisb, axisc: Axis specifications
axis: Axis of vectors
Returns:
cupy.ndarray: Cross product
"""
def vdot(a, b):
"""Return dot product of two vectors."""import cupy as cp
# Create matrices
A = cp.random.random((3, 3))
B = cp.random.random((3, 3))
x = cp.random.random(3)
# Matrix multiplication
C = cp.dot(A, B) # Matrix-matrix multiplication
y = cp.dot(A, x) # Matrix-vector multiplication
C_alt = cp.matmul(A, B) # Alternative matrix multiplication
# Matrix properties
trace_A = cp.trace(A) # Sum of diagonal elements
det_A = cp.det(A) # Determinant
norm_A = cp.norm(A) # Frobenius norm
norm_2 = cp.norm(A, ord=2) # 2-norm (spectral norm)# Solve Ax = b
A = cp.array([[3, 1], [1, 2]], dtype=cp.float32)
b = cp.array([9, 8], dtype=cp.float32)
# Direct solution
x = cp.solve(A, b) # x = [2, 3]
# Least squares solution for overdetermined systems
A_over = cp.random.random((10, 3)) # 10 equations, 3 unknowns
b_over = cp.random.random(10)
x_ls, residuals, rank, s = cp.lstsq(A_over, b_over)
# Matrix inversion
A_inv = cp.inv(A)
x_inv = cp.dot(A_inv, b) # Alternative solution method# Symmetric matrix eigenvalues
symmetric_matrix = cp.array([[4, -2], [-2, 1]], dtype=cp.float32)
eigenvals, eigenvecs = cp.eigh(symmetric_matrix)
# Note: CuPy only supports eigenvalue computation for Hermitian matrices
# For general matrices, use NumPy on CPU or specialized libraries# SVD decomposition
matrix = cp.random.random((4, 3))
U, s, Vh = cp.svd(matrix, full_matrices=False)
reconstructed = cp.dot(U * s, Vh) # Reconstruction
# QR decomposition
tall_matrix = cp.random.random((5, 3))
Q, R = cp.qr(tall_matrix)
# Cholesky decomposition for positive definite matrix
pos_def = cp.dot(matrix.T, matrix) # Ensure positive definite
L = cp.cholesky(pos_def)# Matrix functions
matrix = cp.array([[1, 2], [3, 4]], dtype=cp.float32)
# Matrix powers
matrix_squared = cp.matrix_power(matrix, 2)
# Vector operations
v1 = cp.array([1, 2, 3])
v2 = cp.array([4, 5, 6])
cross_product = cp.cross(v1, v2) # Cross product
dot_product = cp.vdot(v1, v2) # Dot product
# Multi-matrix dot product
matrices = [cp.random.random((3, 4)),
cp.random.random((4, 5)),
cp.random.random((5, 2))]
# Chain multiplication using matmul
result = cp.matmul(cp.matmul(matrices[0], matrices[1]), matrices[2])# Use appropriate data types for better performance
A_f32 = cp.array(A, dtype=cp.float32) # Single precision
A_f64 = cp.array(A, dtype=cp.float64) # Double precision
# Prefer matmul over dot for matrix multiplication
result = cp.matmul(A, B) # Preferred for matrix operations
# Use in-place operations when possible
cp.dot(A, B, out=C) # Write result directly to C
# For large matrices, consider blocked algorithms
# CuPy automatically uses optimized BLAS routines# Solving multiple systems with same coefficient matrix
A = cp.random.random((100, 100))
multiple_b = cp.random.random((100, 50)) # 50 different RHS vectors
# Efficient solution for multiple RHS
solutions = cp.solve(A, multiple_b)
# Rank analysis
rank = cp.matrix_rank(A)
if rank < A.shape[0]:
print("Matrix is rank deficient")Install with Tessl CLI
npx tessl i tessl/pypi-cupy-cuda114