CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-cupy-cuda12x

CuPy is a NumPy/SciPy-compatible array library for GPU-accelerated computing with Python

Pending
Overview
Eval results
Files

linear-algebra.mddocs/

Linear Algebra

GPU-accelerated linear algebra operations using cuBLAS and cuSOLVER libraries. These functions provide high-performance matrix operations, decompositions, and solving capabilities while maintaining NumPy compatibility.

Capabilities

Matrix and Vector Products

def dot(a, b, out=None):
    """
    Dot product of two arrays.

    Parameters:
    - a, b: input arrays
    - out: output array

    Returns:
    cupy.ndarray: dot product result
    """

def inner(a, b):
    """Inner product of two arrays."""

def outer(a, b, out=None):
    """Compute outer product of two vectors."""

def matmul(x1, x2, out=None, casting='same_kind', order='K', dtype=None, subok=True):
    """
    Matrix product of two arrays.

    Parameters:
    - x1, x2: input arrays
    - out: output array
    - casting: casting rule
    - order: memory layout
    - dtype: data type
    - subok: allow subclasses

    Returns:
    cupy.ndarray: matrix product
    """

def vdot(a, b):
    """Return dot product of two vectors."""

def tensordot(a, b, axes=2):
    """
    Compute tensor dot product along specified axes.

    Parameters:
    - a, b: input arrays
    - axes: integer or sequence of integers

    Returns:
    cupy.ndarray: tensor dot product
    """

def kron(a, b):
    """Kronecker product of two arrays."""

def cross(a, b, axisa=-1, axisb=-1, axisc=-1, axis=None):
    """Return cross product of two (arrays of) vectors."""

def einsum(subscripts, *operands, out=None, dtype=None, order='K', casting='safe', optimize=False):
    """
    Evaluate Einstein summation convention.

    Parameters:
    - subscripts: string specifying subscripts for summation
    - operands: input arrays
    - out: output array
    - dtype: data type
    - order: memory layout
    - casting: casting rule
    - optimize: optimization level

    Returns:
    cupy.ndarray: Einstein summation result
    """

Decompositions

def cholesky(a):
    """
    Cholesky decomposition.

    Parameters:
    - a: Hermitian positive-definite matrix

    Returns:
    cupy.ndarray: lower triangular Cholesky factor
    """

def qr(a, mode='reduced'):
    """
    QR decomposition.

    Parameters:
    - a: input matrix
    - mode: 'reduced', 'complete', 'r', 'raw'

    Returns:
    tuple: (Q, R) matrices or R matrix depending on mode
    """

def svd(a, full_matrices=True, compute_uv=True, hermitian=False):
    """
    Singular Value Decomposition.

    Parameters:
    - a: input matrix
    - full_matrices: compute full-sized U and Vh
    - compute_uv: compute U and Vh in addition to s
    - hermitian: assume a is Hermitian

    Returns:
    tuple: (U, s, Vh) where s are singular values
    """

Matrix Eigenvalues

def eigh(a, UPLO='L'):
    """
    Return eigenvalues and eigenvectors of Hermitian matrix.

    Parameters:
    - a: Hermitian matrix
    - UPLO: 'L' for lower triangle, 'U' for upper triangle

    Returns:
    tuple: (eigenvalues, eigenvectors)
    """

def eigvalsh(a, UPLO='L'):
    """
    Return eigenvalues of Hermitian matrix.

    Parameters:
    - a: Hermitian matrix
    - UPLO: 'L' for lower triangle, 'U' for upper triangle

    Returns:
    cupy.ndarray: eigenvalues in ascending order
    """

Norms and Other Numbers

def norm(x, ord=None, axis=None, keepdims=False):
    """
    Matrix or vector norm.

    Parameters:
    - x: input array
    - ord: order of norm
    - axis: axis along which to compute norm
    - keepdims: keep dimensions

    Returns:
    cupy.ndarray or float: norm of the matrix or vector
    """

def det(a):
    """
    Compute determinant of array.

    Parameters:
    - a: input array

    Returns:
    cupy.ndarray: determinant of a
    """

def matrix_rank(M, tol=None, hermitian=False):
    """
    Return matrix rank using SVD method.

    Parameters:
    - M: input matrix
    - tol: threshold for small singular values
    - hermitian: assume M is Hermitian

    Returns:
    int: rank of matrix
    """

def slogdet(a):
    """
    Compute sign and log of determinant.

    Parameters:
    - a: input array

    Returns:
    tuple: (sign, logdet) where det = sign * exp(logdet)
    """

def trace(a, offset=0, axis1=0, axis2=1, dtype=None, out=None):
    """
    Return sum along diagonals of array.

    Parameters:
    - a: input array
    - offset: diagonal offset
    - axis1, axis2: axes to be used as 2-D sub-arrays
    - dtype: data type
    - out: output array

    Returns:
    cupy.ndarray: sum of diagonal elements
    """

Solving Equations and Inverting Matrices

def solve(a, b):
    """
    Solve linear system ax = b.

    Parameters:
    - a: coefficient matrix
    - b: dependent variable values

    Returns:
    cupy.ndarray: solution to the system
    """

def tensorsolve(a, b, axes=None):
    """
    Solve tensor equation ax = b for x.

    Parameters:
    - a: coefficient tensor
    - b: dependent variable tensor
    - axes: axes to be used

    Returns:
    cupy.ndarray: solution tensor
    """

def lstsq(a, b, rcond=None):
    """
    Return least-squares solution to linear system.

    Parameters:
    - a: coefficient matrix
    - b: dependent variable values
    - rcond: cut-off ratio for small singular values

    Returns:
    tuple: (solution, residuals, rank, singular_values)
    """

def inv(a):
    """
    Compute multiplicative inverse of matrix.

    Parameters:
    - a: input matrix

    Returns:
    cupy.ndarray: multiplicative inverse of a
    """

def pinv(a, rcond=1e-15, hermitian=False):
    """
    Compute Moore-Penrose pseudoinverse.

    Parameters:
    - a: input matrix
    - rcond: cutoff for small singular values
    - hermitian: assume a is Hermitian

    Returns:
    cupy.ndarray: pseudoinverse of a
    """

def tensorinv(a, ind=2):
    """
    Compute inverse of N-dimensional array.

    Parameters:
    - a: tensor to invert
    - ind: number of first indices that define inversion

    Returns:
    cupy.ndarray: inverse of a
    """

Advanced Matrix Operations

def matrix_power(a, n):
    """
    Raise square matrix to integer power.

    Parameters:
    - a: input matrix
    - n: exponent

    Returns:
    cupy.ndarray: a raised to power n
    """

Types

# Exception from NumPy
class LinAlgError(Exception):
    """Generic linear algebra error."""

Usage Examples

Basic Matrix Operations

import cupy as cp

# Create matrices
A = cp.random.random((5, 5))
B = cp.random.random((5, 3))
x = cp.random.random(5)

# Matrix multiplication
C = cp.dot(A, B)
matrix_mult = cp.matmul(A, B)

# Vector operations
dot_product = cp.dot(x, x)
outer_product = cp.outer(x, x)

Solving Linear Systems

import cupy as cp

# Create 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)

# Solve the system
x = cp.linalg.solve(A, b)

# Verify solution
residual = cp.dot(A, x) - b
print(f"Residual norm: {cp.linalg.norm(residual)}")

# For overdetermined systems
A_over = cp.random.random((10, 3))
b_over = cp.random.random(10)
x_lstsq, residuals, rank, s = cp.linalg.lstsq(A_over, b_over)

Matrix Decompositions

import cupy as cp

# Create test matrix
A = cp.random.random((5, 5))
A_symmetric = A + A.T  # Make symmetric

# SVD decomposition
U, s, Vh = cp.linalg.svd(A)
reconstructed = cp.dot(U, cp.dot(cp.diag(s), Vh))

# QR decomposition  
Q, R = cp.linalg.qr(A)
reconstructed_qr = cp.dot(Q, R)

# Cholesky decomposition (for positive definite matrix)
A_pd = cp.dot(A, A.T)  # Make positive definite
L = cp.linalg.cholesky(A_pd)
reconstructed_chol = cp.dot(L, L.T)

Eigenvalue Problems

import cupy as cp

# Create symmetric matrix
A = cp.random.random((5, 5))
A_sym = (A + A.T) / 2

# Compute eigenvalues and eigenvectors
eigenvals, eigenvecs = cp.linalg.eigh(A_sym)

# Just eigenvalues
eigenvals_only = cp.linalg.eigvalsh(A_sym)

# Verify eigenvalue equation: A @ v = λ * v
for i in range(len(eigenvals)):
    lambda_i = eigenvals[i]
    v_i = eigenvecs[:, i]
    left_side = cp.dot(A_sym, v_i)
    right_side = lambda_i * v_i
    error = cp.linalg.norm(left_side - right_side)
    print(f"Eigenvalue {i}: error = {error}")

Matrix Properties

import cupy as cp

# Create test matrix
A = cp.random.random((4, 4))

# Compute various properties
det_A = cp.linalg.det(A)
rank_A = cp.linalg.matrix_rank(A)
trace_A = cp.trace(A)
norm_A = cp.linalg.norm(A)

# Different norms
frobenius_norm = cp.linalg.norm(A, 'fro')
nuclear_norm = cp.linalg.norm(A, 'nuc')
spectral_norm = cp.linalg.norm(A, 2)

print(f"Determinant: {det_A}")
print(f"Rank: {rank_A}")
print(f"Trace: {trace_A}")
print(f"Frobenius norm: {frobenius_norm}")

Matrix Inversion

import cupy as cp

# Create invertible matrix
A = cp.random.random((4, 4))
A = A + 4 * cp.eye(4)  # Make well-conditioned

# Compute inverse
A_inv = cp.linalg.inv(A)

# Verify inversion
identity_check = cp.dot(A, A_inv)
error = cp.linalg.norm(identity_check - cp.eye(4))
print(f"Inversion error: {error}")

# Pseudoinverse for non-square matrices
B = cp.random.random((6, 4))
B_pinv = cp.linalg.pinv(B)

Einstein Summation

import cupy as cp

# Create arrays
A = cp.random.random((3, 4))
B = cp.random.random((4, 5))
C = cp.random.random((3, 5))

# Matrix multiplication using einsum
result1 = cp.einsum('ij,jk->ik', A, B)
result2 = cp.dot(A, B)  # Equivalent

# 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)

# Trace of matrix
trace_einsum = cp.einsum('ii', A[:3, :3])  # First 3x3 part
trace_normal = cp.trace(A[:3, :3])

Install with Tessl CLI

npx tessl i tessl/pypi-cupy-cuda12x

docs

array-operations.md

cuda-interface.md

custom-kernels.md

fft-operations.md

index.md

linear-algebra.md

math-functions.md

random-numbers.md

statistics-sorting.md

tile.json