CuPy: NumPy & SciPy-compatible array library for GPU-accelerated computing with Python that provides a drop-in replacement for NumPy/SciPy on NVIDIA CUDA platforms.
—
Comprehensive mathematical operations including trigonometric, hyperbolic, exponential, logarithmic, arithmetic, and complex number functions. All functions are GPU-accelerated and maintain compatibility with NumPy's mathematical function interface.
Standard trigonometric functions operating element-wise on GPU arrays.
def sin(x, out=None):
"""Trigonometric sine, element-wise.
Parameters:
- x: array-like, input array in radians
- out: ndarray, output array
Returns:
cupy.ndarray: sine of each element
"""
def cos(x, out=None):
"""Trigonometric cosine, element-wise."""
def tan(x, out=None):
"""Trigonometric tangent, element-wise."""
def arcsin(x, out=None):
"""Inverse sine, element-wise.
Returns:
cupy.ndarray: angles in radians, in range [-π/2, π/2]
"""
def arccos(x, out=None):
"""Inverse cosine, element-wise."""
def arctan(x, out=None):
"""Inverse tangent, element-wise."""
def arctan2(x1, x2, out=None):
"""Element-wise arc tangent of x1/x2, choosing quadrant correctly.
Parameters:
- x1: array-like, y-coordinates
- x2: array-like, x-coordinates
- out: ndarray, output array
Returns:
cupy.ndarray: angles in radians, in range [-π, π]
"""
def hypot(x1, x2, out=None):
"""Element-wise hypotenuse calculation: sqrt(x1**2 + x2**2)."""
def degrees(x, out=None):
"""Convert angles from radians to degrees."""
def radians(x, out=None):
"""Convert angles from degrees to radians."""
def deg2rad(x, out=None):
"""Convert angles from degrees to radians."""
def rad2deg(x, out=None):
"""Convert angles from radians to degrees."""Hyperbolic trigonometric functions and their inverses.
def sinh(x, out=None):
"""Hyperbolic sine, element-wise."""
def cosh(x, out=None):
"""Hyperbolic cosine, element-wise."""
def tanh(x, out=None):
"""Hyperbolic tangent, element-wise."""
def arcsinh(x, out=None):
"""Inverse hyperbolic sine, element-wise."""
def arccosh(x, out=None):
"""Inverse hyperbolic cosine, element-wise."""
def arctanh(x, out=None):
"""Inverse hyperbolic tangent, element-wise."""Exponential, logarithmic, and power functions.
def exp(x, out=None):
"""Calculate exponential of all elements in input array.
Parameters:
- x: array-like, input values
- out: ndarray, output array
Returns:
cupy.ndarray: e**x for each element
"""
def exp2(x, out=None):
"""Calculate 2**x for all elements in input array."""
def expm1(x, out=None):
"""Calculate exp(x) - 1 for all elements, accurate for small x."""
def log(x, out=None):
"""Natural logarithm, element-wise."""
def log10(x, out=None):
"""Base-10 logarithm, element-wise."""
def log2(x, out=None):
"""Base-2 logarithm, element-wise."""
def log1p(x, out=None):
"""Calculate log(1 + x) for all elements, accurate for small x."""
def logaddexp(x1, x2, out=None):
"""Logarithm of sum of exponentials: log(exp(x1) + exp(x2))."""
def logaddexp2(x1, x2, out=None):
"""Logarithm of sum of exponentials base 2."""
def power(x1, x2, out=None):
"""First array elements raised to powers from second array, element-wise.
Parameters:
- x1: array-like, bases
- x2: array-like, exponents
- out: ndarray, output array
Returns:
cupy.ndarray: x1**x2, element-wise
"""
def sqrt(x, out=None):
"""Non-negative square root, element-wise."""
def cbrt(x, out=None):
"""Cube root, element-wise."""
def square(x, out=None):
"""Element-wise square: x**2."""Basic arithmetic operations between arrays and scalars.
def add(x1, x2, out=None):
"""Add arguments element-wise.
Parameters:
- x1: array-like, first input array
- x2: array-like, second input array
- out: ndarray, output array
Returns:
cupy.ndarray: sum of x1 and x2, element-wise
"""
def subtract(x1, x2, out=None):
"""Subtract arguments element-wise."""
def multiply(x1, x2, out=None):
"""Multiply arguments element-wise."""
def divide(x1, x2, out=None):
"""Divide arguments element-wise."""
def true_divide(x1, x2, out=None):
"""True division element-wise, always returns float."""
def floor_divide(x1, x2, out=None):
"""Floor division element-wise."""
def negative(x, out=None):
"""Numerical negative, element-wise."""
def positive(x, out=None):
"""Numerical positive, element-wise."""
def reciprocal(x, out=None):
"""Return reciprocal element-wise: 1/x."""
def remainder(x1, x2, out=None):
"""Element-wise remainder of division."""
def mod(x1, x2, out=None):
"""Element-wise remainder of division (alias for remainder)."""
def divmod(x1, x2, out1=None, out2=None):
"""Element-wise quotient and remainder simultaneously."""
def fmod(x1, x2, out=None):
"""Element-wise remainder of division (C-style)."""
def modf(x, out1=None, out2=None):
"""Return fractional and integral parts of elements."""
def absolute(x, out=None):
"""Absolute value element-wise."""
def abs(x, out=None):
"""Absolute value element-wise (alias)."""
def sign(x, out=None):
"""Element-wise indication of the sign of a number."""Functions for rounding array elements to different precisions.
def around(a, decimals=0, out=None):
"""Round to given number of decimals.
Parameters:
- a: array-like, input array
- decimals: int, number of decimals to round to
- out: ndarray, output array
Returns:
cupy.ndarray: rounded array
"""
def round_(a, decimals=0, out=None):
"""Round to given number of decimals."""
def rint(x, out=None):
"""Round elements to nearest integer."""
def fix(x, out=None):
"""Round to nearest integer toward zero."""
def floor(x, out=None):
"""Floor of input, element-wise."""
def ceil(x, out=None):
"""Ceiling of input, element-wise."""
def trunc(x, out=None):
"""Truncated value of input, element-wise."""Functions for working with complex numbers.
def real(val):
"""Return real part of complex argument element-wise.
Parameters:
- val: array-like, input array
Returns:
cupy.ndarray: real component of input
"""
def imag(val):
"""Return imaginary part of complex argument element-wise."""
def conjugate(x, out=None):
"""Return complex conjugate element-wise."""
def conj(x, out=None):
"""Return complex conjugate element-wise (alias)."""
def angle(z, deg=False):
"""Return angle of complex argument.
Parameters:
- z: array-like, complex input
- deg: bool, return angle in degrees if True
Returns:
cupy.ndarray: angles in radians (or degrees)
"""Functions for comparing values and finding extrema.
def maximum(x1, x2, out=None):
"""Element-wise maximum of array elements.
Parameters:
- x1: array-like, first input array
- x2: array-like, second input array
- out: ndarray, output array
Returns:
cupy.ndarray: maximum values element-wise
"""
def minimum(x1, x2, out=None):
"""Element-wise minimum of array elements."""
def fmax(x1, x2, out=None):
"""Element-wise maximum, ignoring NaNs."""
def fmin(x1, x2, out=None):
"""Element-wise minimum, ignoring NaNs."""
def clip(a, a_min, a_max, out=None):
"""Clip (limit) values in array.
Parameters:
- a: array-like, input array
- a_min: scalar or array, minimum value
- a_max: scalar or array, maximum value
- out: ndarray, output array
Returns:
cupy.ndarray: clipped array
"""Functions for working with floating-point representations.
def copysign(x1, x2, out=None):
"""Change sign of x1 to match sign of x2, element-wise."""
def frexp(x, out1=None, out2=None):
"""Decompose numbers into mantissa and exponent."""
def ldexp(x1, x2, out=None):
"""Compute x1 * 2**x2, element-wise."""
def nextafter(x1, x2, out=None):
"""Return next floating-point value after x1 towards x2."""
def signbit(x, out=None):
"""Return element-wise True where signbit is set."""Special functions and numerical utilities.
def nan_to_num(x, copy=True, nan=0.0, posinf=None, neginf=None):
"""Replace NaN with zero and infinity with large finite numbers.
Parameters:
- x: array-like, input array
- copy: bool, whether to create copy
- nan: int, float, value to replace NaN with
- posinf: int, float, value to replace positive infinity with
- neginf: int, float, value to replace negative infinity with
Returns:
cupy.ndarray: array with replaced values
"""
def interp(x, xp, fp, left=None, right=None, period=None):
"""One-dimensional linear interpolation."""
def sinc(x):
"""Return sinc function: sin(π*x)/(π*x)."""
def i0(x):
"""Modified Bessel function of first kind, order 0."""Reduction operations that compute summary statistics.
def sum(a, axis=None, dtype=None, out=None, keepdims=False):
"""Sum of array elements over given axis.
Parameters:
- a: array-like, input array
- axis: int or tuple, axis to sum over
- dtype: data type of output
- out: ndarray, output array
- keepdims: bool, keep reduced dimensions
Returns:
cupy.ndarray: sum along specified axis
"""
def prod(a, axis=None, dtype=None, out=None, keepdims=False):
"""Product of array elements over given axis."""
def cumsum(a, axis=None, dtype=None, out=None):
"""Cumulative sum along axis."""
def cumprod(a, axis=None, dtype=None, out=None):
"""Cumulative product along axis."""
def diff(a, n=1, axis=-1, prepend=None, append=None):
"""Calculate discrete difference along axis."""
def gradient(f, *varargs, axis=None, edge_order=1):
"""Return gradient of N-dimensional array."""import cupy as cp
import numpy as np
# Create sample data
x = cp.linspace(0, 2 * cp.pi, 1000)
y = cp.random.random((100, 100))
# Trigonometric functions
sin_vals = cp.sin(x)
cos_vals = cp.cos(x)
tan_vals = cp.tan(x)
# Exponential and logarithmic
exp_vals = cp.exp(y)
log_vals = cp.log(cp.abs(y) + 1) # Add 1 to avoid log(0)
power_vals = cp.power(y, 2.5)import cupy as cp
# Create arrays
a = cp.array([1, 4, 9, 16, 25])
b = cp.array([1, 2, 3, 4, 5])
# Arithmetic operations
addition = cp.add(a, b) # [2, 6, 12, 20, 30]
multiplication = cp.multiply(a, b) # [1, 8, 27, 64, 125]
square_root = cp.sqrt(a) # [1, 2, 3, 4, 5]
reciprocal = cp.reciprocal(b.astype(float)) # [1.0, 0.5, 0.33, 0.25, 0.2]import cupy as cp
# Create complex arrays
z = cp.array([1+2j, 3+4j, 5+6j])
# Complex operations
real_part = cp.real(z) # [1, 3, 5]
imag_part = cp.imag(z) # [2, 4, 6]
conjugate = cp.conjugate(z) # [1-2j, 3-4j, 5-6j]
magnitude = cp.abs(z) # [2.236, 5.0, 7.81]
phase = cp.angle(z) # [1.107, 0.927, 0.876] radiansimport cupy as cp
# Create 2D array
data = cp.random.random((1000, 500))
# Compute statistics
total_sum = cp.sum(data)
mean_val = cp.mean(data)
row_sums = cp.sum(data, axis=1) # Sum each row
col_means = cp.mean(data, axis=0) # Mean of each column
# Cumulative operations
cumulative_sum = cp.cumsum(data, axis=0)Install with Tessl CLI
npx tessl i tessl/pypi-cupy-cuda113