CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-cupy-cuda11x

CuPy: NumPy & SciPy for GPU - CUDA 11.x optimized distribution providing GPU-accelerated computing with Python

Pending
Overview
Eval results
Files

linear-algebra.mddocs/

Linear Algebra

CuPy provides comprehensive linear algebra operations for GPU-accelerated computation, offering NumPy-compatible matrix operations, decomposition methods, eigenvalue computation, and linear system solving optimized for CUDA and ROCm platforms.

Capabilities

Matrix and Vector Products

Core linear algebra operations for matrix multiplication and vector products optimized for GPU computation.

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 argument for the result
    """

def vdot(a, b):
    """
    Return the dot product of two vectors.
    
    Parameters:
        a: array_like - First input array
        b: array_like - Second input array
    """

def inner(a, b):
    """
    Inner product of two arrays.
    
    Parameters:
        a, b: array_like - Input arrays
    """

def outer(a, b, out=None):
    """
    Compute the outer product of two vectors.
    
    Parameters:
        a: (M,) array_like - First input vector
        b: (N,) array_like - Second input vector
        out: (M, N) ndarray, optional - Output array for the result
    """

def matmul(x1, x2, out=None):
    """
    Matrix product of two arrays.
    
    Parameters:
        x1, x2: array_like - Input arrays, scalars not allowed
        out: ndarray, optional - Output array for the result
    """

def tensordot(a, b, axes=2):
    """
    Compute tensor dot product along specified axes.
    
    Parameters:
        a, b: array_like - Tensors to "dot"
        axes: int or (2,) array_like - If an int N, sum over the last N axes of a and the first N axes of b in order
    """

def einsum(subscripts, *operands, out=None, dtype=None, order='K', casting='safe', optimize=False):
    """
    Evaluates the Einstein summation convention on the operands.
    
    Parameters:
        subscripts: str - Specifies the subscripts for summation as comma separated list of subscript labels
        operands: list of array_like - Arrays for the operation
        out: ndarray, optional - Output array to store the result
        dtype: dtype, optional - Type of the returned array
        order: {'C', 'F', 'A', 'K'}, optional - Controls the memory layout of the output
        casting: {'no', 'equiv', 'safe', 'same_kind', 'unsafe'}, optional - Controls what kind of data casting may occur
        optimize: {False, True, 'greedy', 'optimal'}, optional - Controls if intermediate optimization should occur
    """

def kron(a, b):
    """
    Kronecker product of two arrays.
    
    Parameters:
        a, b: array_like - Input arrays
    """

def cross(a, b, axisa=-1, axisb=-1, axisc=-1, axis=None):
    """
    Return the cross product of two (arrays of) vectors.
    
    Parameters:
        a: array_like - Components of the first vector(s)
        b: array_like - Components of the second vector(s)
        axisa: int, optional - Axis of a that defines the vector(s)
        axisb: int, optional - Axis of b that defines the vector(s)
        axisc: int, optional - Axis of c containing the cross product vector(s)
        axis: int, optional - If defined, the axis of a, b and c that defines the vector(s) and cross product
    """

Norms and Matrix Properties

Functions for computing matrix and vector norms and other numerical properties.

def trace(a, offset=0, axis1=0, axis2=1, dtype=None, out=None):
    """
    Return the sum along diagonals of the array.
    
    Parameters:
        a: array_like - Input array, from which the diagonals are taken
        offset: int, optional - Offset of the diagonal from the main diagonal
        axis1, axis2: int, optional - Axes to be used as the first and second axis of the 2-D sub-arrays
        dtype: dtype, optional - Determines the data-type of the returned array
        out: ndarray, optional - Array into which the output is placed
    """

Advanced Linear Algebra (cupy.linalg)

Advanced linear algebra operations including decompositions, eigenvalues, and system solving.

def matrix_power(a, n):
    """
    Raise a square matrix to the (integer) power n.
    
    Parameters:
        a: (..., M, M) array_like - Matrix to be "powered"
        n: int - The exponent can be any integer or long integer, positive, negative, or zero
    """

def cholesky(a):
    """
    Cholesky decomposition.
    
    Parameters:
        a: (..., M, M) array_like - Hermitian (symmetric if real-valued), positive-definite input matrix
    """

def qr(a, mode='reduced'):
    """
    Compute the qr factorization of a matrix.
    
    Parameters:
        a: array_like, shape (..., M, N) - Matrix to be factored
        mode: {'reduced', 'complete'}, optional - If K = min(M, N), then 'reduced' returns Q, R with dimensions (..., M, K), (..., K, N)
    """

def svd(a, full_matrices=True, compute_uv=True, hermitian=False):
    """
    Singular Value Decomposition.
    
    Parameters:
        a: (..., M, N) array_like - Input array with a.ndim >= 2
        full_matrices: bool, optional - If True (default), u and vh have the shapes (..., M, M) and (..., N, N)
        compute_uv: bool, optional - Whether or not to compute u and vh in addition to s (default is True)
        hermitian: bool, optional - If True, a is assumed to be Hermitian (symmetric if real-valued)
    """

def eigh(a, UPLO='L'):
    """
    Return the eigenvalues and eigenvectors of a complex Hermitian (conjugate symmetric) or a real symmetric matrix.
    
    Parameters:
        a: (..., M, M) array - Hermitian or real symmetric matrices whose eigenvalues and eigenvectors are to be computed
        UPLO: {'L', 'U'}, optional - Whether the calculation is done with the lower triangular part of a ('L', default) or the upper triangular part ('U')
    """

def eigvalsh(a, UPLO='L'):
    """
    Compute the eigenvalues of a complex Hermitian or real symmetric matrix.
    
    Parameters:
        a: (..., M, M) array_like - Hermitian or real symmetric matrices whose eigenvalues are to be computed
        UPLO: {'L', 'U'}, optional - Whether the calculation is done with the lower triangular part of a ('L', default) or the upper triangular part ('U')
    """

def norm(x, ord=None, axis=None, keepdims=False):
    """
    Matrix or vector norm.
    
    Parameters:
        x: array_like - Input array
        ord: {non-zero int, inf, -inf, 'fro', 'nuc'}, optional - Order of the norm
        axis: {None, int, 2-tuple of ints}, optional - If axis is an integer, it specifies the axis of x along which to compute the vector norms
        keepdims: bool, optional - If this is set to True, the axes which are normed over are left in the result as dimensions with size one
    """

def det(a):
    """
    Compute the determinant of an array.
    
    Parameters:
        a: (..., M, M) array_like - Input array to compute determinants for
    """

def matrix_rank(M, tol=None, hermitian=False):
    """
    Return matrix rank of array using SVD method.
    
    Parameters:
        M: {(M,), (..., M, N)} array_like - Input vector or stack of matrices
        tol: (...) array_like, float, optional - Threshold below which SVD values are considered zero
        hermitian: bool, optional - If True, M is assumed to be Hermitian (symmetric if real-valued)
    """

def slogdet(a):
    """
    Compute the sign and (natural) logarithm of the determinant of an array.
    
    Parameters:
        a: (..., M, M) array_like - Input array, has to be a square 2-D array
    """

def solve(a, b):
    """
    Solve a linear matrix equation, or system of linear scalar equations.
    
    Parameters:
        a: (..., M, M) array_like - Coefficient matrix
        b: {(..., M,), (..., M, K)}, array_like - Ordinate or "dependent variable" values
    """

def tensorsolve(a, b, axes=None):
    """
    Solve the tensor equation a x = b for x.
    
    Parameters:
        a: array_like - Coefficient tensor, of shape b.shape + Q
        b: array_like - Right-hand tensor, which can be of any shape
        axes: tuple of ints, optional - Axes in a to reorder to the right, before inversion
    """

def lstsq(a, b, rcond=None):
    """
    Return the least-squares solution to a linear matrix equation.
    
    Parameters:
        a: (M, N) array_like - "Coefficient" matrix
        b: array_like - Ordinate or "dependent variable" values
        rcond: float, optional - Cut-off ratio for small singular values of a
    """

def inv(a):
    """
    Compute the (multiplicative) inverse of a matrix.
    
    Parameters:
        a: (..., M, M) array_like - Matrix to be inverted
    """

def pinv(a, rcond=1e-15, hermitian=False):
    """
    Compute the (Moore-Penrose) pseudo-inverse of a matrix.
    
    Parameters:
        a: (..., M, N) array_like - Matrix or stack of matrices to be pseudo-inverted
        rcond: (...) array_like of float - Cutoff for small singular values
        hermitian: bool, optional - If True, a is assumed to be Hermitian (symmetric if real-valued)
    """

def tensorinv(a, ind=2):
    """
    Compute the 'inverse' of an N-dimensional array.
    
    Parameters:
        a: array_like - Tensor to 'invert'. Its shape must be 'square', i.e., prod(a.shape[:ind]) == prod(a.shape[ind:])
        ind: int, optional - Number of first indices that are involved in the inverse sum
    """

Linear Algebra Exceptions

Exception classes for linear algebra errors.

class LinAlgError(Exception):
    """
    Generic Python-exception-derived object raised by linalg functions.
    
    General purpose exception class, derived from Python's exception.Exception
    class, programmatically raised in linalg functions when a Linear
    Algebra-related condition would prevent further correct execution of the
    function.
    """

Extended Linear Algebra (cupyx.linalg)

Extended linear algebra functions not available in standard NumPy.

def invh(a):
    """
    Compute the inverse of a Hermitian positive-definite matrix.
    
    Parameters:
        a: (..., M, M) array_like - Hermitian positive-definite matrix to be inverted
    """

Usage Examples

import cupy as cp
import cupy.linalg as la

# Matrix operations
A = cp.random.rand(1000, 1000)
B = cp.random.rand(1000, 1000)

# Basic matrix multiplication
C = cp.dot(A, B)
D = cp.matmul(A, B)

# Matrix decompositions
Q, R = la.qr(A)
U, s, Vt = la.svd(A)
L = la.cholesky(A @ A.T + cp.eye(1000))  # Make positive definite

# Eigenvalue decomposition (for symmetric matrices)
symmetric_A = A + A.T
eigenvals, eigenvecs = la.eigh(symmetric_A)

# Solving linear systems
x = la.solve(A, cp.random.rand(1000))
x_lstsq, residuals, rank, s = la.lstsq(A, cp.random.rand(1000))

# Matrix properties
det_A = la.det(A)
norm_A = la.norm(A)
trace_A = cp.trace(A)
rank_A = la.matrix_rank(A)

# Matrix inverse and pseudo-inverse
A_inv = la.inv(A)
A_pinv = la.pinv(A)

# Vector operations
v1 = cp.array([1, 2, 3])
v2 = cp.array([4, 5, 6])
dot_product = cp.dot(v1, v2)
cross_product = cp.cross(v1, v2)
outer_product = cp.outer(v1, v2)

# Einstein summation
# Matrix multiplication using einsum
result = cp.einsum('ij,jk->ik', A[:10, :10], B[:10, :10])

# Batch operations on multiple matrices
batch_A = cp.random.rand(10, 100, 100)
batch_det = la.det(batch_A)
batch_inv = la.inv(batch_A)

Linear algebra operations in CuPy provide high-performance matrix computation capabilities optimized for GPU acceleration, enabling efficient numerical linear algebra with familiar NumPy interfaces while leveraging the parallel processing power of modern GPUs for scientific computing and machine learning applications.

Install with Tessl CLI

npx tessl i tessl/pypi-cupy-cuda11x

docs

array-operations.md

cuda-integration.md

custom-kernels.md

fft.md

index.md

io-operations.md

jit-compilation.md

linear-algebra.md

mathematical-functions.md

performance-profiling.md

polynomial-operations.md

random.md

scipy-extensions.md

tile.json