NumPy & SciPy-compatible array library for GPU-accelerated computing with Python
—
Element-wise mathematical functions including trigonometric, logarithmic, arithmetic, and comparison operations. All functions support broadcasting and operate on GPU arrays with the same semantics as NumPy ufuncs.
Basic arithmetic operations supporting broadcasting between arrays and scalars.
def add(x1, x2, /, out=None):
"""
Add elements element-wise.
Parameters:
- x1, x2: array-like, input arrays
- out: cupy.ndarray, output array
Returns:
cupy.ndarray: Element-wise sum
"""
def subtract(x1, x2, /, out=None):
"""Subtract elements element-wise."""
def multiply(x1, x2, /, out=None):
"""Multiply elements element-wise."""
def divide(x1, x2, /, out=None):
"""Divide elements element-wise."""
def true_divide(x1, x2, /, out=None):
"""True division element-wise."""
def floor_divide(x1, x2, /, out=None):
"""Floor division element-wise."""
def power(x1, x2, /, out=None):
"""
Raise x1 to power x2 element-wise.
Parameters:
- x1: array-like, base values
- x2: array-like, exponent values
- out: cupy.ndarray, output array
Returns:
cupy.ndarray: x1**x2 element-wise
"""
def float_power(x1, x2, /, out=None):
"""x1**x2 element-wise with float result."""
def remainder(x1, x2, /, out=None):
"""Remainder of division element-wise."""
def mod(x1, x2, /, out=None):
"""Remainder of division element-wise (alias for remainder)."""
def divmod(x1, x2, /, out1=None, out2=None):
"""
Element-wise divmod operation.
Parameters:
- x1, x2: array-like, input arrays
- out1, out2: cupy.ndarray, output arrays for quotient and remainder
Returns:
tuple: (quotient, remainder)
"""Operations on single arrays including negation, absolute value, and sign.
def negative(x, /, out=None):
"""
Numerical negative element-wise.
Parameters:
- x: array-like, input array
- out: cupy.ndarray, output array
Returns:
cupy.ndarray: -x element-wise
"""
def positive(x, /, out=None):
"""Numerical positive element-wise."""
def absolute(x, /, out=None):
"""
Absolute value element-wise.
Parameters:
- x: array-like, input array
- out: cupy.ndarray, output array
Returns:
cupy.ndarray: |x| element-wise
"""
def abs(x, /, out=None):
"""Absolute value element-wise (alias for absolute)."""
def fabs(x, /, out=None):
"""Absolute value element-wise (for real inputs)."""
def sign(x, /, out=None):
"""
Sign of elements element-wise.
Parameters:
- x: array-like, input array
- out: cupy.ndarray, output array
Returns:
cupy.ndarray: Sign of x (-1, 0, or 1)
"""
def reciprocal(x, /, out=None):
"""Reciprocal (1/x) element-wise."""
def square(x, /, out=None):
"""Square element-wise."""
def sqrt(x, /, out=None):
"""
Square root element-wise.
Parameters:
- x: array-like, input array
- out: cupy.ndarray, output array
Returns:
cupy.ndarray: sqrt(x) element-wise
"""
def cbrt(x, /, out=None):
"""Cube root element-wise."""Trigonometric operations in radians.
def sin(x, /, out=None):
"""
Trigonometric sine element-wise.
Parameters:
- x: array-like, input array in radians
- out: cupy.ndarray, output array
Returns:
cupy.ndarray: sin(x) element-wise
"""
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."""
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.
Parameters:
- x1: array-like, y-coordinates
- x2: array-like, x-coordinates
- out: cupy.ndarray, output array
Returns:
cupy.ndarray: atan2(x1, x2) in range [-pi, pi]
"""
def hypot(x1, x2, /, out=None):
"""
Euclidean distance sqrt(x1²+x2²) element-wise.
Parameters:
- x1, x2: array-like, input arrays
- out: cupy.ndarray, output array
Returns:
cupy.ndarray: sqrt(x1**2 + x2**2)
"""Hyperbolic trigonometric functions.
def sinh(x, /, out=None):
"""
Hyperbolic sine element-wise.
Parameters:
- x: array-like, input array
- out: cupy.ndarray, output array
Returns:
cupy.ndarray: sinh(x) 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):
"""
Exponential function element-wise.
Parameters:
- x: array-like, input array
- out: cupy.ndarray, output array
Returns:
cupy.ndarray: e**x element-wise
"""
def exp2(x, /, out=None):
"""2**x element-wise."""
def expm1(x, /, out=None):
"""exp(x) - 1 element-wise (more accurate for small x)."""
def log(x, /, out=None):
"""
Natural logarithm element-wise.
Parameters:
- x: array-like, input array
- out: cupy.ndarray, output array
Returns:
cupy.ndarray: ln(x) element-wise
"""
def log2(x, /, out=None):
"""Base-2 logarithm element-wise."""
def log10(x, /, out=None):
"""Base-10 logarithm element-wise."""
def log1p(x, /, out=None):
"""log(1 + x) element-wise (more accurate for small x)."""
def logaddexp(x1, x2, /, out=None):
"""
log(exp(x1) + exp(x2)) element-wise.
Parameters:
- x1, x2: array-like, input arrays
- out: cupy.ndarray, output array
Returns:
cupy.ndarray: Numerically stable log(exp(x1) + exp(x2))
"""
def logaddexp2(x1, x2, /, out=None):
"""log2(2**x1 + 2**x2) element-wise."""Functions for rounding and truncating values.
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: cupy.ndarray, output array
Returns:
cupy.ndarray: Rounded values
"""
def round(a, decimals=0, out=None):
"""Round to given number of decimals (alias for around)."""
def rint(x, /, out=None):
"""Round to nearest integer."""
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):
"""Truncate to integers element-wise."""
def fix(x, out=None):
"""Round to nearest integer towards zero."""Element-wise comparison functions returning boolean arrays.
def equal(x1, x2, /, out=None):
"""
Element-wise equality comparison.
Parameters:
- x1, x2: array-like, input arrays
- out: cupy.ndarray, output boolean array
Returns:
cupy.ndarray: Boolean array of x1 == x2
"""
def not_equal(x1, x2, /, out=None):
"""Element-wise inequality comparison."""
def less(x1, x2, /, out=None):
"""Element-wise less-than comparison."""
def less_equal(x1, x2, /, out=None):
"""Element-wise less-than-or-equal comparison."""
def greater(x1, x2, /, out=None):
"""Element-wise greater-than comparison."""
def greater_equal(x1, x2, /, out=None):
"""Element-wise greater-than-or-equal comparison."""Operations for complex numbers.
def real(val):
"""
Real part of complex values.
Parameters:
- val: array-like, input array
Returns:
cupy.ndarray: Real components
"""
def imag(val):
"""Imaginary part of complex values."""
def conjugate(x, /, out=None):
"""
Complex conjugate element-wise.
Parameters:
- x: array-like, input array
- out: cupy.ndarray, output array
Returns:
cupy.ndarray: Complex conjugate of x
"""
def conj(x, /, out=None):
"""Complex conjugate element-wise (alias for conjugate)."""
def angle(z, deg=False):
"""
Argument of complex values.
Parameters:
- z: array-like, complex input array
- deg: bool, return angle in degrees if True
Returns:
cupy.ndarray: Angles in radians (or degrees)
"""Element-wise minimum and maximum operations.
def maximum(x1, x2, /, out=None):
"""
Element-wise maximum.
Parameters:
- x1, x2: array-like, input arrays
- out: cupy.ndarray, output array
Returns:
cupy.ndarray: Element-wise maximum values
"""
def minimum(x1, x2, /, out=None):
"""Element-wise minimum."""
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 values to range [a_min, a_max].
Parameters:
- a: array-like, input array
- a_min: scalar or array, minimum value
- a_max: scalar or array, maximum value
- out: cupy.ndarray, output array
Returns:
cupy.ndarray: Clipped values
"""Operations that reduce arrays along axes.
def sum(a, axis=None, dtype=None, out=None, keepdims=False, initial=None, where=None):
"""
Sum of array elements over given axes.
Parameters:
- a: array-like, input array
- axis: None or int or tuple of ints, axes to sum over
- dtype: data type, type of output
- out: cupy.ndarray, output array
- keepdims: bool, keep reduced dimensions as size 1
- initial: scalar, starting value for sum
- where: array of bool, elements to include
Returns:
cupy.ndarray: Sum along specified axes
"""
def prod(a, axis=None, dtype=None, out=None, keepdims=False, initial=None, where=None):
"""Product of array elements over given axes."""
def mean(a, axis=None, dtype=None, out=None, keepdims=False):
"""Mean of array elements over given axes."""
def cumsum(a, axis=None, dtype=None, out=None):
"""
Cumulative sum along axis.
Parameters:
- a: array-like, input array
- axis: int, axis along which to compute cumsum
- dtype: data type, type of output
- out: cupy.ndarray, output array
Returns:
cupy.ndarray: Cumulative sum
"""
def cumprod(a, axis=None, dtype=None, out=None):
"""Cumulative product along axis."""
def diff(a, n=1, axis=-1, prepend=None, append=None):
"""
Discrete difference along given axis.
Parameters:
- a: array-like, input array
- n: int, number of times to difference
- axis: int, axis along which to difference
- prepend: array-like, values to prepend
- append: array-like, values to append
Returns:
cupy.ndarray: Differences
"""Specialized mathematical functions.
def sinc(x):
"""
Sinc function: sin(πx)/(πx).
Parameters:
- x: array-like, input array
Returns:
cupy.ndarray: sinc(x) element-wise
"""
def heaviside(x1, x2, /, out=None):
"""
Heaviside step function.
Parameters:
- x1: array-like, input values
- x2: array-like, value at x1 == 0
- out: cupy.ndarray, output array
Returns:
cupy.ndarray: Heaviside function values
"""
def nan_to_num(x, copy=True, nan=0.0, posinf=None, neginf=None):
"""
Replace NaN/Inf with finite numbers.
Parameters:
- x: array-like, input array
- copy: bool, whether to create copy
- nan: scalar, value to replace NaN with
- posinf: scalar, value to replace +inf with
- neginf: scalar, value to replace -inf with
Returns:
cupy.ndarray: Array with finite values
"""
def real_if_close(a, tol=100):
"""Return real part if imaginary part is close to zero."""
def interp(x, xp, fp, left=None, right=None, period=None):
"""
One-dimensional linear interpolation.
Parameters:
- x: array-like, x-coordinates to interpolate at
- xp: array-like, x-coordinates of data points
- fp: array-like, y-coordinates of data points
- left: scalar, value for x < xp[0]
- right: scalar, value for x > xp[-1]
- period: scalar, period for periodic interpolation
Returns:
cupy.ndarray: Interpolated values
"""import cupy as cp
# Element-wise operations
a = cp.array([1, 2, 3, 4])
b = cp.array([10, 20, 30, 40])
sum_result = cp.add(a, b) # [11, 22, 33, 44]
prod_result = a * b # Broadcasting works same as NumPy
# Scalar operations
scaled = a * 2.5
power_result = cp.power(a, 2) # [1, 4, 9, 16]# Trigonometric operations on ranges
x = cp.linspace(0, 2*cp.pi, 1000)
y = cp.sin(x)
phase = cp.arctan2(cp.sin(x), cp.cos(x))
# Logarithmic operations
data = cp.random.lognormal(0, 1, (1000,))
log_data = cp.log(data)
stable_sum = cp.logaddexp.reduce(log_data)# Statistical reductions
matrix = cp.random.random((1000, 1000))
row_sums = cp.sum(matrix, axis=1)
col_means = cp.mean(matrix, axis=0)
total_sum = cp.sum(matrix) # Sum all elements
# Cumulative operations
cumulative = cp.cumsum(matrix, axis=0)
running_product = cp.cumprod(matrix, axis=1)Install with Tessl CLI
npx tessl i tessl/pypi-cupy