CuPy: NumPy & SciPy for GPU - CUDA 11.x optimized distribution providing GPU-accelerated computing with Python
—
CuPy provides comprehensive mathematical functions for GPU-accelerated computation, offering NumPy-compatible trigonometric, hyperbolic, exponential, logarithmic, arithmetic, and statistical operations optimized for CUDA and ROCm platforms.
GPU-accelerated trigonometric functions supporting both scalar and array inputs with element-wise operations.
def sin(x, out=None):
"""
Trigonometric sine, element-wise.
Parameters:
x: array_like - Input array in radians
out: ndarray, optional - Output array of the same shape as x
"""
def cos(x, out=None):
"""
Cosine element-wise.
Parameters:
x: array_like - Input array in radians
out: ndarray, optional - Output array of the same shape as x
"""
def tan(x, out=None):
"""
Compute tangent element-wise.
Parameters:
x: array_like - Input array in radians
out: ndarray, optional - Output array of the same shape as x
"""
def arcsin(x, out=None):
"""
Inverse sine, element-wise.
Parameters:
x: array_like - y-coordinate on the unit circle
out: ndarray, optional - Output array of the same shape as x
"""
def arccos(x, out=None):
"""
Trigonometric inverse cosine, element-wise.
Parameters:
x: array_like - x-coordinate on the unit circle
out: ndarray, optional - Output array of the same shape as x
"""
def arctan(x, out=None):
"""
Trigonometric inverse tangent, element-wise.
Parameters:
x: array_like - Input values
out: ndarray, optional - Output array of the same shape as x
"""
def arctan2(x1, x2, out=None):
"""
Element-wise arc tangent of x1/x2 choosing the quadrant correctly.
Parameters:
x1: array_like, real-valued - y-coordinates
x2: array_like, real-valued - x-coordinates
out: ndarray, optional - Output array of same shape as x1 and x2
"""
def hypot(x1, x2, out=None):
"""
Given the "legs" of a right triangle, return its hypotenuse.
Parameters:
x1, x2: array_like - Leg of the triangle(s)
out: ndarray, optional - Output array of same shape as x1 and x2
"""
def degrees(x, out=None):
"""
Convert angles from radians to degrees.
Parameters:
x: array_like - Input array in radians
out: ndarray, optional - Output array of same shape as x
"""
def radians(x, out=None):
"""
Convert angles from degrees to radians.
Parameters:
x: array_like - Input array in degrees
out: ndarray, optional - Output array of same shape as x
"""
def deg2rad(x, out=None):
"""
Convert angles from degrees to radians.
Parameters:
x: array_like - Angles in degrees
out: ndarray, optional - Output array of same shape as x
"""
def rad2deg(x, out=None):
"""
Convert angles from radians to degrees.
Parameters:
x: array_like - Angles in radians
out: ndarray, optional - Output array of same shape as x
"""
def unwrap(p, discont=3.141592653589793, axis=-1):
"""
Unwrap by changing deltas between values to 2*pi complement.
Parameters:
p: array_like - Input array
discont: float, optional - Maximum discontinuity between values (default is pi)
axis: int, optional - Axis along which unwrap will operate (default is -1)
"""GPU-accelerated hyperbolic trigonometric functions for mathematical computation.
def sinh(x, out=None):
"""
Hyperbolic sine, element-wise.
Parameters:
x: array_like - Input array
out: ndarray, optional - Output array of the same shape as x
"""
def cosh(x, out=None):
"""
Hyperbolic cosine, element-wise.
Parameters:
x: array_like - Input array
out: ndarray, optional - Output array of the same shape as x
"""
def tanh(x, out=None):
"""
Compute hyperbolic tangent element-wise.
Parameters:
x: array_like - Input array
out: ndarray, optional - Output array of the same shape as x
"""
def arcsinh(x, out=None):
"""
Inverse hyperbolic sine element-wise.
Parameters:
x: array_like - Input array
out: ndarray, optional - Output array of the same shape as x
"""
def arccosh(x, out=None):
"""
Inverse hyperbolic cosine, element-wise.
Parameters:
x: array_like - Input array
out: ndarray, optional - Output array of the same shape as x
"""
def arctanh(x, out=None):
"""
Inverse hyperbolic tangent element-wise.
Parameters:
x: array_like - Input array
out: ndarray, optional - Output array of the same shape as x
"""Functions for exponential and logarithmic calculations optimized for GPU computation.
def exp(x, out=None):
"""
Calculate the exponential of all elements in the input array.
Parameters:
x: array_like - Input values
out: ndarray, optional - Output array of same shape as x
"""
def exp2(x, out=None):
"""
Calculate 2**p for all p in the input array.
Parameters:
x: array_like - Input values
out: ndarray, optional - Output array of same shape as x
"""
def expm1(x, out=None):
"""
Calculate exp(x) - 1 for all elements in the array.
Parameters:
x: array_like - Input values
out: ndarray, optional - Output array of same shape as x
"""
def log(x, out=None):
"""
Natural logarithm, element-wise.
Parameters:
x: array_like - Input values
out: ndarray, optional - Output array of same shape as x
"""
def log10(x, out=None):
"""
Return the base 10 logarithm of the input array, element-wise.
Parameters:
x: array_like - Input values
out: ndarray, optional - Output array of same shape as x
"""
def log2(x, out=None):
"""
Base-2 logarithm of x.
Parameters:
x: array_like - Input values
out: ndarray, optional - Output array of same shape as x
"""
def log1p(x, out=None):
"""
Return the natural logarithm of one plus the input array, element-wise.
Parameters:
x: array_like - Input values
out: ndarray, optional - Output array of same shape as x
"""
def logaddexp(x1, x2, out=None):
"""
Logarithm of the sum of exponentials of the inputs.
Parameters:
x1, x2: array_like - Input arrays
out: ndarray, optional - Output array of same shape as x1 and x2
"""
def logaddexp2(x1, x2, out=None):
"""
Logarithm of the sum of exponentials of the inputs in base-2.
Parameters:
x1, x2: array_like - Input arrays
out: ndarray, optional - Output array of same shape as x1 and x2
"""Core arithmetic operations supporting element-wise computation on GPU arrays.
def add(x1, x2, out=None):
"""
Add arguments element-wise.
Parameters:
x1, x2: array_like - Input arrays to be added
out: ndarray, optional - Output array of same shape as x1 and x2
"""
def subtract(x1, x2, out=None):
"""
Subtract arguments, element-wise.
Parameters:
x1, x2: array_like - Input arrays to be subtracted
out: ndarray, optional - Output array of same shape as x1 and x2
"""
def multiply(x1, x2, out=None):
"""
Multiply arguments element-wise.
Parameters:
x1, x2: array_like - Input arrays to be multiplied
out: ndarray, optional - Output array of same shape as x1 and x2
"""
def divide(x1, x2, out=None):
"""
Divide arguments element-wise.
Parameters:
x1: array_like - Dividend array
x2: array_like - Divisor array
out: ndarray, optional - Output array of same shape as x1 and x2
"""
def true_divide(x1, x2, out=None):
"""
Returns a true division of the inputs, element-wise.
Parameters:
x1: array_like - Dividend array
x2: array_like - Divisor array
out: ndarray, optional - Output array of same shape as x1 and x2
"""
def floor_divide(x1, x2, out=None):
"""
Return the largest integer smaller or equal to the division of the inputs.
Parameters:
x1: array_like - Numerator
x2: array_like - Denominator
out: ndarray, optional - Output array of same shape as x1 and x2
"""
def power(x1, x2, out=None):
"""
First array elements raised to powers from second array, element-wise.
Parameters:
x1: array_like - Base values
x2: array_like - Exponents
out: ndarray, optional - Output array of same shape as x1 and x2
"""
def float_power(x1, x2, out=None):
"""
First array elements raised to powers from second array, element-wise.
Parameters:
x1: array_like - Base values
x2: array_like - Exponents
out: ndarray, optional - Output array of same shape as x1 and x2
"""
def mod(x1, x2, out=None):
"""
Return element-wise remainder of division.
Parameters:
x1: array_like - Dividend
x2: array_like - Divisor
out: ndarray, optional - Output array of same shape as x1 and x2
"""
def remainder(x1, x2, out=None):
"""
Return element-wise remainder of division.
Parameters:
x1: array_like - Dividend
x2: array_like - Divisor
out: ndarray, optional - Output array of same shape as x1 and x2
"""
def divmod(x1, x2):
"""
Return element-wise quotient and remainder simultaneously.
Parameters:
x1: array_like - Dividend
x2: array_like - Divisor
"""
def fmod(x1, x2, out=None):
"""
Return the element-wise remainder of division.
Parameters:
x1: array_like - Dividend
x2: array_like - Divisor
out: ndarray, optional - Output array of same shape as x1 and x2
"""
def modf(x, out=None):
"""
Return the fractional and integral parts of an array, element-wise.
Parameters:
x: array_like - Input array
out: ndarray, optional - Output array for fractional part, same shape as x
"""
def negative(x, out=None):
"""
Numerical negative, element-wise.
Parameters:
x: array_like - Input array
out: ndarray, optional - Output array of same shape as x
"""
def positive(x, out=None):
"""
Numerical positive, element-wise.
Parameters:
x: array_like - Input array
out: ndarray, optional - Output array of same shape as x
"""
def absolute(x, out=None):
"""
Calculate the absolute value element-wise.
Parameters:
x: array_like - Input array
out: ndarray, optional - Output array of same shape as x
"""
def abs(x, out=None):
"""
Calculate the absolute value element-wise.
Parameters:
x: array_like - Input array
out: ndarray, optional - Output array of same shape as x
"""
def reciprocal(x, out=None):
"""
Return the reciprocal of the argument, element-wise.
Parameters:
x: array_like - Input array
out: ndarray, optional - Output array of same shape as x
"""
def sign(x, out=None):
"""
Returns an element-wise indication of the sign of a number.
Parameters:
x: array_like - Input values
out: ndarray, optional - Output array of same shape as x
"""Functions for rounding floating-point values to integers or specified decimal places.
def around(a, decimals=0, out=None):
"""
Round an array to the given number of decimals.
Parameters:
a: array_like - Input data
decimals: int, optional - Number of decimal places to round to (default is 0)
out: ndarray, optional - Alternative output array
"""
def round(a, decimals=0, out=None):
"""
Round an array to the given number of decimals.
Parameters:
a: array_like - Input data
decimals: int, optional - Number of decimal places to round to (default is 0)
out: ndarray, optional - Alternative output array
"""
def rint(x, out=None):
"""
Round elements of the array to the nearest integer.
Parameters:
x: array_like - Input array
out: ndarray, optional - Output array of same shape as x
"""
def fix(x, out=None):
"""
Round to nearest integer towards zero.
Parameters:
x: array_like - Input values
out: ndarray, optional - Output array of same shape as x
"""
def floor(x, out=None):
"""
Return the floor of the input, element-wise.
Parameters:
x: array_like - Input data
out: ndarray, optional - Output array of same shape as x
"""
def ceil(x, out=None):
"""
Return the ceiling of the input, element-wise.
Parameters:
x: array_like - Input data
out: ndarray, optional - Output array of same shape as x
"""
def trunc(x, out=None):
"""
Return the truncated value of the input, element-wise.
Parameters:
x: array_like - Input data
out: ndarray, optional - Output array of same shape as x
"""Reduction operations and difference calculations for array analysis.
def sum(a, axis=None, dtype=None, out=None, keepdims=False, initial=0, where=True):
"""
Sum of array elements over a given axis.
Parameters:
a: array_like - Elements to sum
axis: None or int or tuple of ints, optional - Axis or axes along which a sum is performed
dtype: dtype, optional - Type of the returned array and of the accumulator
out: ndarray, optional - Alternative output array
keepdims: bool, optional - If this is set to True, the axes which are reduced are left in the result as dimensions with size one
initial: scalar, optional - Starting value for the sum (default is 0)
where: array_like of bool, optional - Elements to include in the sum
"""
def prod(a, axis=None, dtype=None, out=None, keepdims=False, initial=1, where=True):
"""
Return the product of array elements over a given axis.
Parameters:
a: array_like - Input data
axis: None or int or tuple of ints, optional - Axis or axes along which a product is performed
dtype: dtype, optional - Type of the returned array
out: ndarray, optional - Alternative output array
keepdims: bool, optional - If this is set to True, the axes which are reduced are left in the result
initial: scalar, optional - Starting value for the product (default is 1)
where: array_like of bool, optional - Elements to include in the product
"""
def cumsum(a, axis=None, dtype=None, out=None):
"""
Return the cumulative sum of the elements along a given axis.
Parameters:
a: array_like - Input array
axis: int, optional - Axis along which the cumulative sum is computed (default is None)
dtype: dtype, optional - Type of the returned array
out: ndarray, optional - Alternative output array
"""
def cumprod(a, axis=None, dtype=None, out=None):
"""
Return the cumulative product of elements along a given axis.
Parameters:
a: array_like - Input array
axis: int, optional - Axis along which the cumulative product is computed (default is None)
dtype: dtype, optional - Type of the returned array
out: ndarray, optional - Alternative output array
"""
def diff(a, n=1, axis=-1, prepend=None, append=None):
"""
Calculate the n-th discrete difference along the given axis.
Parameters:
a: array_like - Input array
n: int, optional - Number of times values are differenced (default is 1)
axis: int, optional - Axis along which the difference is taken (default is -1)
prepend, append: array_like, optional - Values to prepend or append to a along axis
"""
def ediff1d(ary, to_end=None, to_begin=None):
"""
The differences between consecutive elements of an array.
Parameters:
ary: array_like - Input array, will be flattened before the differences are taken
to_end: array_like, optional - Number(s) to append at the end of the returned differences
to_begin: array_like, optional - Number(s) to prepend at the beginning of the returned differences
"""
def gradient(f, *varargs, axis=None, edge_order=1):
"""
Return the gradient of an N-dimensional array.
Parameters:
f: array_like - Input array
varargs: list of scalar or array, optional - Spacing between f values
axis: None or int or tuple of ints, optional - Gradient is calculated only along the given axis or axes
edge_order: int, optional - Gradient is calculated using N-th order accurate differences at the boundaries
"""
def trapz(y, x=None, dx=1.0, axis=-1):
"""
Integrate along the given axis using the composite trapezoidal rule.
Parameters:
y: array_like - Input array to integrate
x: array_like, optional - Sample points corresponding to the y values
dx: scalar, optional - Spacing between sample points when x is None (default is 1.0)
axis: int, optional - Axis along which to integrate (default is -1)
"""
def nansum(a, axis=None, dtype=None, out=None, keepdims=False):
"""
Return the sum of array elements over a given axis treating Not a Numbers (NaNs) as zero.
Parameters:
a: array_like - Array containing numbers whose sum is desired
axis: None or int or tuple of ints, optional - Axis or axes along which the sum is computed
dtype: dtype, optional - Type of the returned array and of the accumulator
out: ndarray, optional - Alternative output array
keepdims: bool, optional - If this is set to True, the axes which are reduced are left in the result
"""
def nanprod(a, axis=None, dtype=None, out=None, keepdims=False):
"""
Return the product of array elements over a given axis treating Not a Numbers (NaNs) as ones.
Parameters:
a: array_like - Array containing numbers whose product is desired
axis: None or int or tuple of ints, optional - Axis or axes along which the product is computed
dtype: dtype, optional - Type of the returned array and of the accumulator
out: ndarray, optional - Alternative output array
keepdims: bool, optional - If this is set to True, the axes which are reduced are left in the result
"""
def nancumsum(a, axis=None, dtype=None, out=None):
"""
Return the cumulative sum of array elements over a given axis treating Not a Numbers (NaNs) as zero.
Parameters:
a: array_like - Input array
axis: int, optional - Axis along which the cumulative sum is computed
dtype: dtype, optional - Type of the returned array
out: ndarray, optional - Alternative output array
"""
def nancumprod(a, axis=None, dtype=None, out=None):
"""
Return the cumulative product of array elements over a given axis treating Not a Numbers (NaNs) as one.
Parameters:
a: array_like - Input array
axis: int, optional - Axis along which the cumulative product is computed
dtype: dtype, optional - Type of the returned array
out: ndarray, optional - Alternative output array
"""Functions for working with complex numbers and their components.
def angle(z, deg=False):
"""
Return the angle of the complex argument.
Parameters:
z: array_like - Input array
deg: bool, optional - Return angle in degrees if True, radians if False (default)
"""
def real(val):
"""
Return the real part of the complex argument.
Parameters:
val: array_like - Input array
"""
def imag(val):
"""
Return the imaginary part of the complex argument.
Parameters:
val: array_like - Input array
"""
def conj(x, out=None):
"""
Return the complex conjugate, element-wise.
Parameters:
x: array_like - Input values
out: ndarray, optional - Output array of same shape as x
"""
def conjugate(x, out=None):
"""
Return the complex conjugate, element-wise.
Parameters:
x: array_like - Input values
out: ndarray, optional - Output array of same shape as x
"""Additional mathematical utilities and special functions.
def clip(a, a_min, a_max, out=None):
"""
Clip (limit) the values in an array.
Parameters:
a: array_like - Array containing elements to clip
a_min: scalar or array_like or None - Minimum value
a_max: scalar or array_like or None - Maximum value
out: ndarray, optional - Results will be placed in this array
"""
def sqrt(x, out=None):
"""
Return the non-negative square-root of an array, element-wise.
Parameters:
x: array_like - Values whose square-roots are required
out: ndarray, optional - Output array of same shape as x
"""
def cbrt(x, out=None):
"""
Return the cube-root of an array, element-wise.
Parameters:
x: array_like - Input values
out: ndarray, optional - Output array of same shape as x
"""
def square(x, out=None):
"""
Return the element-wise square of the input.
Parameters:
x: array_like - Input data
out: ndarray, optional - Output array of same shape as x
"""
def fabs(x, out=None):
"""
Compute the absolute values element-wise.
Parameters:
x: array_like - Input values
out: ndarray, optional - Output array of same shape as x
"""
def maximum(x1, x2, out=None):
"""
Element-wise maximum of array elements.
Parameters:
x1, x2: array_like - Input arrays holding the elements to be compared
out: ndarray, optional - Output array of same shape as x1 and x2
"""
def minimum(x1, x2, out=None):
"""
Element-wise minimum of array elements.
Parameters:
x1, x2: array_like - Input arrays holding the elements to be compared
out: ndarray, optional - Output array of same shape as x1 and x2
"""
def fmax(x1, x2, out=None):
"""
Element-wise maximum of array elements.
Parameters:
x1, x2: array_like - Input arrays holding the elements to be compared
out: ndarray, optional - Output array of same shape as x1 and x2
"""
def fmin(x1, x2, out=None):
"""
Element-wise minimum of array elements.
Parameters:
x1, x2: array_like - Input arrays holding the elements to be compared
out: ndarray, optional - Output array of same shape as x1 and x2
"""
def heaviside(x1, x2, out=None):
"""
Compute the Heaviside step function.
Parameters:
x1: array_like - Input values
x2: array_like - Value of the function when x1 is 0
out: ndarray, optional - Output array of same shape as x1 and x2
"""
def nan_to_num(x, copy=True, nan=0.0, posinf=None, neginf=None):
"""
Replace NaN with zero and infinity with large finite numbers (default behaviour) or with the numbers defined by the user using the nan, posinf and/or neginf keywords.
Parameters:
x: array_like - Input data
copy: bool, optional - Whether to create a copy of x (default is True)
nan: int, float, optional - Value to be used to fill NaN values (default is 0.0)
posinf: int, float, optional - Value to be used to fill positive infinity values
neginf: int, float, optional - Value to be used to fill negative infinity values
"""
def real_if_close(a, tol=100):
"""
If complex input returns a real array if complex parts are close to zero.
Parameters:
a: array_like - Input array
tol: float - Tolerance in machine epsilons for the complex part of the elements in the array (default is 100)
"""
def interp(x, xp, fp, left=None, right=None, period=None):
"""
One-dimensional linear interpolation for monotonically increasing sample points.
Parameters:
x: array_like - x-coordinates at which to evaluate the interpolated values
xp: 1-D sequence of floats - x-coordinates of the data points, must be increasing
fp: 1-D sequence of float or complex - y-coordinates of the data points, same length as xp
left: optional float or complex corresponding to fp - Value to return for x < xp[0]
right: optional float or complex corresponding to fp - Value to return for x > xp[-1]
period: None or float, optional - A period for the x-coordinates
"""
def convolve(a, v, mode='full'):
"""
Returns the discrete convolution of two one-dimensional arrays.
Parameters:
a: (N,) array_like - First one-dimensional input array
v: (M,) array_like - Second one-dimensional input array
mode: {'full', 'valid', 'same'}, optional - Size of the output
"""Mathematical special functions for advanced computation.
def i0(x):
"""
Modified Bessel function of the first kind, order 0.
Parameters:
x: array_like - Input array
"""
def sinc(x):
"""
Return the sinc function.
Parameters:
x: array_like - Input array
"""Functions for manipulating floating-point number representation and properties.
def signbit(x, out=None):
"""
Returns element-wise True where signbit is set (less than zero).
Parameters:
x: array_like - Input values
out: ndarray, optional - Output array of same shape as x
"""
def copysign(x1, x2, out=None):
"""
Change the sign of x1 to that of x2, element-wise.
Parameters:
x1: array_like - Values to change the sign of
x2: array_like - Values whose sign will be copied to x1
out: ndarray, optional - Output array of same shape as x1 and x2
"""
def frexp(x, out1=None, out2=None):
"""
Decompose the elements of x into mantissa and twos exponent.
Parameters:
x: array_like - Input array
out1: ndarray, optional - Output array for mantissa
out2: ndarray, optional - Output array for exponent
"""
def ldexp(x1, x2, out=None):
"""
Returns x1 * 2**x2, element-wise.
Parameters:
x1: array_like - Array of multipliers
x2: array_like, int - Array of twos exponents
out: ndarray, optional - Output array of same shape as x1 and x2
"""
def nextafter(x1, x2, out=None):
"""
Return the next floating-point value after x1 towards x2, element-wise.
Parameters:
x1: array_like - Values to find the next representable value of
x2: array_like - Direction where to look for the next representable value
out: ndarray, optional - Output array of same shape as x1 and x2
"""Functions for working with rational numbers and number theory.
def gcd(x1, x2, out=None):
"""
Returns the greatest common divisor of |x1| and |x2|.
Parameters:
x1, x2: array_like, int - Arrays of values
out: ndarray, optional - Output array of same shape as x1 and x2
"""
def lcm(x1, x2, out=None):
"""
Returns the lowest common multiple of |x1| and |x2|.
Parameters:
x1, x2: array_like, int - Arrays of values
out: ndarray, optional - Output array of same shape as x1 and x2
"""Functions for creating window functions commonly used in signal processing.
def bartlett(M):
"""
Return the Bartlett window.
Parameters:
M: int - Number of points in the output window
"""
def blackman(M):
"""
Return the Blackman window.
Parameters:
M: int - Number of points in the output window
"""
def hamming(M):
"""
Return the Hamming window.
Parameters:
M: int - Number of points in the output window
"""
def hanning(M):
"""
Return the Hanning window.
Parameters:
M: int - Number of points in the output window
"""
def kaiser(M, beta):
"""
Return the Kaiser window.
Parameters:
M: int - Number of points in the output window
beta: float - Shape parameter for window
"""import cupy as cp
# Basic mathematical operations
x = cp.linspace(0, 2*cp.pi, 1000)
y = cp.sin(x)
z = cp.exp(x)
# Trigonometric functions
angles = cp.array([0, cp.pi/4, cp.pi/2, cp.pi])
sin_vals = cp.sin(angles)
cos_vals = cp.cos(angles)
# Statistical operations
data = cp.random.normal(0, 1, (1000, 1000))
mean = cp.mean(data)
std = cp.std(data)
total = cp.sum(data)
# Element-wise operations
a = cp.array([1, 4, 9, 16])
sqrt_a = cp.sqrt(a)
log_a = cp.log(a)
# Complex number operations
complex_arr = cp.array([1+2j, 3+4j, 5+6j])
magnitude = cp.abs(complex_arr)
phase = cp.angle(complex_arr)Mathematical functions in CuPy provide comprehensive numerical computation capabilities optimized for GPU acceleration, enabling high-performance scientific computing with familiar NumPy interfaces while leveraging the parallel processing power of modern GPUs.
Install with Tessl CLI
npx tessl i tessl/pypi-cupy-cuda11x