NumPy & SciPy compatible GPU-accelerated array library for CUDA computing
—
Complete set of mathematical operations including trigonometric, hyperbolic, exponential, logarithmic, arithmetic, and special functions, all optimized for GPU execution. CuPy provides GPU-accelerated implementations of all NumPy mathematical functions with identical APIs.
Standard trigonometric functions with GPU acceleration.
def sin(x, out=None, **kwargs):
"""Trigonometric sine, element-wise.
Args:
x: Input array in radians
out: Output array
**kwargs: Additional ufunc arguments
Returns:
cupy.ndarray: Sine values
"""
def cos(x, out=None, **kwargs):
"""Trigonometric cosine, element-wise."""
def tan(x, out=None, **kwargs):
"""Trigonometric tangent, element-wise."""
def arcsin(x, out=None, **kwargs):
"""Inverse sine, element-wise."""
def arccos(x, out=None, **kwargs):
"""Inverse cosine, element-wise."""
def arctan(x, out=None, **kwargs):
"""Inverse tangent, element-wise."""
def arctan2(y, x, out=None, **kwargs):
"""Element-wise arc tangent of y/x choosing quadrant correctly."""
def degrees(x, out=None, **kwargs):
"""Convert radians to degrees."""
def radians(x, out=None, **kwargs):
"""Convert degrees to radians."""
def deg2rad(x, out=None, **kwargs):
"""Convert degrees to radians (alias for radians)."""
def rad2deg(x, out=None, **kwargs):
"""Convert radians to degrees (alias for degrees)."""
def hypot(x1, x2, out=None, **kwargs):
"""Given the legs of a right triangle, return its hypotenuse.
Equivalent to sqrt(x1**2 + x2**2), element-wise, with proper handling
of complex numbers and infinities.
"""
def unwrap(p, discont=3.141592653589793, axis=-1):
"""Unwrap by changing deltas between values to 2*pi complement.
Args:
p: Input array
discont: Maximum discontinuity between values
axis: Axis along which unwrap will operate
Returns:
cupy.ndarray: Output array with unwrapped values
"""Hyperbolic trigonometric functions for exponential relationships.
def sinh(x, out=None, **kwargs):
"""Hyperbolic sine, element-wise."""
def cosh(x, out=None, **kwargs):
"""Hyperbolic cosine, element-wise."""
def tanh(x, out=None, **kwargs):
"""Hyperbolic tangent, element-wise."""
def arcsinh(x, out=None, **kwargs):
"""Inverse hyperbolic sine, element-wise."""
def arccosh(x, out=None, **kwargs):
"""Inverse hyperbolic cosine, element-wise."""
def arctanh(x, out=None, **kwargs):
"""Inverse hyperbolic tangent, element-wise."""Exponential and logarithmic operations with various bases.
def exp(x, out=None, **kwargs):
"""Calculate the exponential of all elements in the input array.
Args:
x: Input array
out: Output array
**kwargs: Additional ufunc arguments
Returns:
cupy.ndarray: Exponential values
"""
def exp2(x, out=None, **kwargs):
"""Calculate 2**x for all elements in array."""
def expm1(x, out=None, **kwargs):
"""Calculate exp(x) - 1 for all elements."""
def log(x, out=None, **kwargs):
"""Natural logarithm, element-wise."""
def log2(x, out=None, **kwargs):
"""Base-2 logarithm, element-wise."""
def log10(x, out=None, **kwargs):
"""Base-10 logarithm, element-wise."""
def log1p(x, out=None, **kwargs):
"""Return the natural logarithm of one plus the input array."""
def logaddexp(x1, x2, out=None, **kwargs):
"""Logarithm of the sum of exponentials of inputs."""
def logaddexp2(x1, x2, out=None, **kwargs):
"""Logarithm of the sum of exponentials of inputs in base 2."""Power operations and root calculations.
def power(x1, x2, out=None, **kwargs):
"""First array elements raised to powers from second array.
Args:
x1: Base values
x2: Exponent values
out: Output array
**kwargs: Additional ufunc arguments
Returns:
cupy.ndarray: Power values
"""
def sqrt(x, out=None, **kwargs):
"""Return the non-negative square-root of an array."""
def square(x, out=None, **kwargs):
"""Return the element-wise square of the input."""
def cbrt(x, out=None, **kwargs):
"""Return the cube-root of an array."""
def reciprocal(x, out=None, **kwargs):
"""Return the reciprocal of the argument."""Basic and advanced arithmetic operations.
def add(x1, x2, out=None, **kwargs):
"""Add arguments element-wise.
Args:
x1, x2: Input arrays or scalars
out: Output array
**kwargs: Additional ufunc arguments
Returns:
cupy.ndarray: Sum of inputs
"""
def subtract(x1, x2, out=None, **kwargs):
"""Subtract arguments element-wise."""
def multiply(x1, x2, out=None, **kwargs):
"""Multiply arguments element-wise."""
def divide(x1, x2, out=None, **kwargs):
"""Divide arguments element-wise."""
def true_divide(x1, x2, out=None, **kwargs):
"""Returns true division element-wise."""
def floor_divide(x1, x2, out=None, **kwargs):
"""Return largest integers smaller or equal to division."""
def mod(x1, x2, out=None, **kwargs):
"""Return element-wise remainder of division."""
def remainder(x1, x2, out=None, **kwargs):
"""Return element-wise remainder of division."""
def divmod(x1, x2, out1=None, out2=None):
"""Return element-wise quotient and remainder simultaneously."""
def negative(x, out=None, **kwargs):
"""Numerical negative, element-wise."""
def positive(x, out=None, **kwargs):
"""Numerical positive, element-wise."""
def absolute(x, out=None, **kwargs):
"""Calculate absolute value element-wise."""
def fabs(x, out=None, **kwargs):
"""Compute absolute values element-wise."""
def sign(x, out=None, **kwargs):
"""Returns element-wise indication of sign of number."""Functions for rounding and discretizing values.
def around(a, decimals=0, out=None):
"""Round to given number of decimals.
Args:
a: Input array
decimals: Number of decimals to round to
out: Output array
Returns:
cupy.ndarray: Rounded values
"""
def round(a, decimals=0, out=None):
"""Round to given number of decimals."""
def rint(x, out=None, **kwargs):
"""Round elements to nearest integers."""
def floor(x, out=None, **kwargs):
"""Return floor of input, element-wise."""
def ceil(x, out=None, **kwargs):
"""Return ceiling of input, element-wise."""
def trunc(x, out=None, **kwargs):
"""Return truncated value of input, element-wise."""
def fix(x, out=None):
"""Round to nearest integer towards zero."""Operations specific to complex numbers.
def real(val):
"""Return real part of complex argument."""
def imag(val):
"""Return imaginary part of complex argument."""
def conj(x, out=None, **kwargs):
"""Return complex conjugate, element-wise."""
def conjugate(x, out=None, **kwargs):
"""Return complex conjugate, element-wise."""
def angle(z, deg=False):
"""Return angle of complex argument."""
def real_if_close(a, tol=100):
"""If complex parts are close to zero, return real parts."""Aggregate operations across array dimensions.
def sum(a, axis=None, dtype=None, out=None, keepdims=False, initial=None, where=None):
"""Sum of array elements over given axis.
Args:
a: Input array
axis: Axis to sum over
dtype: Output data type
out: Output array
keepdims: Keep reduced dimensions
initial: Initial value
where: Boolean mask
Returns:
cupy.ndarray or scalar: Sum result
"""
def prod(a, axis=None, dtype=None, out=None, keepdims=False, initial=None, where=None):
"""Product of array elements over given axis."""
def mean(a, axis=None, dtype=None, out=None, keepdims=False, where=None):
"""Compute arithmetic mean along specified axis."""
def cumsum(a, axis=None, dtype=None, out=None):
"""Return cumulative sum of elements along given axis."""
def cumprod(a, axis=None, dtype=None, out=None):
"""Return cumulative product of elements along given axis."""
def diff(a, n=1, axis=-1, prepend=None, append=None):
"""Calculate n-th discrete difference along given axis."""
def gradient(f, *varargs, axis=None, edge_order=1):
"""Return gradient of an N-dimensional array."""Mathematical statistics and data analysis functions.
def std(a, axis=None, dtype=None, out=None, ddof=0, keepdims=False, where=None):
"""Compute standard deviation along specified axis."""
def var(a, axis=None, dtype=None, out=None, ddof=0, keepdims=False, where=None):
"""Compute variance along specified axis."""
def min(a, axis=None, out=None, keepdims=False, initial=None, where=None):
"""Return minimum along axis."""
def max(a, axis=None, out=None, keepdims=False, initial=None, where=None):
"""Return maximum along axis."""
def ptp(a, axis=None, out=None, keepdims=False):
"""Range of values (maximum - minimum) along axis."""Specialized mathematical functions.
def clip(a, a_min, a_max, out=None, **kwargs):
"""Clip values in array to given range.
Args:
a: Input array
a_min: Minimum value
a_max: Maximum value
out: Output array
Returns:
cupy.ndarray: Clipped array
"""
def fmod(x1, x2, out=None, **kwargs):
"""Return remainder of division (C library fmod)."""
def modf(x, out1=None, out2=None):
"""Return fractional and integral parts of numbers."""
def frexp(x, out1=None, out2=None):
"""Decompose numbers into mantissa and exponent."""
def ldexp(x1, x2, out=None, **kwargs):
"""Compute x1 * 2**x2, element-wise."""
def copysign(x1, x2, out=None, **kwargs):
"""Change sign of x1 to that of x2, element-wise."""
def nextafter(x1, x2, out=None, **kwargs):
"""Return next floating-point value after x1 towards x2."""
def spacing(x, out=None, **kwargs):
"""Return distance between x and nearest adjacent number."""Window functions for signal processing and spectral analysis.
def bartlett(M):
"""Return the Bartlett window.
Args:
M: Number of points in the output window
Returns:
cupy.ndarray: The window, with the maximum value normalized to 1
"""
def blackman(M):
"""Return the Blackman window.
Args:
M: Number of points in the output window
Returns:
cupy.ndarray: The window, with the maximum value normalized to 1
"""
def hamming(M):
"""Return the Hamming window.
Args:
M: Number of points in the output window
Returns:
cupy.ndarray: The window, with the maximum value normalized to 1
"""
def hanning(M):
"""Return the Hanning window.
Args:
M: Number of points in the output window
Returns:
cupy.ndarray: The window, with the maximum value normalized to 1
"""
def kaiser(M, beta):
"""Return the Kaiser window.
Args:
M: Number of points in the output window
beta: Shape parameter for window
Returns:
cupy.ndarray: The window, with the maximum value normalized to 1
"""import cupy as cp
# Create sample data
x = cp.linspace(0, 2*cp.pi, 100)
y = cp.random.random((10, 10))
# Trigonometric functions
sin_vals = cp.sin(x)
cos_vals = cp.cos(x)
combined = cp.sin(x) * cp.cos(x)
# Exponential and logarithmic
exp_vals = cp.exp(y)
log_vals = cp.log(y + 1) # Add 1 to avoid log(0)
# Power operations
squared = cp.square(y)
sqrt_vals = cp.sqrt(cp.abs(y))
power_vals = cp.power(y, 2.5)# Arrays for operations
a = cp.array([1, 2, 3, 4, 5])
b = cp.array([2, 3, 4, 5, 6])
# Basic arithmetic
sum_ab = cp.add(a, b) # [3, 5, 7, 9, 11]
diff_ab = cp.subtract(a, b) # [-1, -1, -1, -1, -1]
prod_ab = cp.multiply(a, b) # [2, 6, 12, 20, 30]
div_ab = cp.divide(b, a) # [2.0, 1.5, 1.33..., 1.25, 1.2]
# Advanced operations
remainder = cp.mod(b, a) # [0, 1, 1, 1, 1]
absolute = cp.abs(diff_ab) # [1, 1, 1, 1, 1]# 2D array for reductions
matrix = cp.random.random((5, 4))
# Different reduction operations
total_sum = cp.sum(matrix) # Sum all elements
row_sums = cp.sum(matrix, axis=1) # Sum each row
col_sums = cp.sum(matrix, axis=0) # Sum each column
mean_val = cp.mean(matrix) # Overall mean
row_means = cp.mean(matrix, axis=1) # Mean of each row
# Cumulative operations
cumulative_sum = cp.cumsum(matrix, axis=0) # Cumulative sum along columns
cumulative_prod = cp.cumprod(matrix, axis=1) # Cumulative product along rows# Complex number operations
z1 = cp.array([1+2j, 3+4j, 5+6j])
z2 = cp.array([2+1j, 4+3j, 6+5j])
# Complex arithmetic
z_sum = cp.add(z1, z2)
z_prod = cp.multiply(z1, z2)
# Complex-specific functions
real_parts = cp.real(z1) # [1, 3, 5]
imag_parts = cp.imag(z1) # [2, 4, 6]
conjugates = cp.conj(z1) # [1-2j, 3-4j, 5-6j]
magnitudes = cp.abs(z1) # Magnitude of complex numbers
phases = cp.angle(z1) # Phase angles# Clipping values
data = cp.array([-5, -1, 0, 1, 5, 10])
clipped = cp.clip(data, 0, 5) # [0, 0, 0, 1, 5, 5]
# Rounding operations
floats = cp.array([1.2, 2.7, -0.8, 3.14159])
rounded = cp.round(floats, 2) # [1.2, 2.7, -0.8, 3.14]
floors = cp.floor(floats) # [1.0, 2.0, -1.0, 3.0]
ceilings = cp.ceil(floats) # [2.0, 3.0, 0.0, 4.0]
# Sign operations
signs = cp.sign(floats) # [1.0, 1.0, -1.0, 1.0]
absolute_vals = cp.abs(floats) # [1.2, 2.7, 0.8, 3.14159]Install with Tessl CLI
npx tessl i tessl/pypi-cupy-cuda114