CuPy: NumPy & SciPy for GPU - A NumPy/SciPy-compatible array library for GPU-accelerated computing with Python, specifically built for AMD ROCm 4.3 platform
—
Comprehensive mathematical operations providing GPU-accelerated versions of standard mathematical functions. All functions maintain NumPy compatibility while leveraging GPU parallelism for significant performance improvements.
Standard trigonometric functions for angle calculations and periodic operations.
def sin(x):
"""
Trigonometric sine, element-wise.
Parameters:
- x: array-like, input array in radians
Returns:
cupy.ndarray: Sine of each element
"""
def cos(x):
"""Trigonometric cosine, element-wise."""
def tan(x):
"""Trigonometric tangent, element-wise."""
def arcsin(x):
"""
Inverse sine, element-wise.
Parameters:
- x: array-like, input array with values in [-1, 1]
Returns:
cupy.ndarray: Arcsine in radians, range [-pi/2, pi/2]
"""
def arccos(x):
"""Inverse cosine, element-wise."""
def arctan(x):
"""Inverse tangent, element-wise."""
def arctan2(y, x):
"""
Element-wise arc tangent of y/x choosing quadrant correctly.
Parameters:
- y: array-like, y-coordinates
- x: array-like, x-coordinates
Returns:
cupy.ndarray: Angle in radians, range [-pi, pi]
"""
def hypot(x1, x2):
"""
Return sqrt(x1**2 + x2**2), element-wise.
Parameters:
- x1, x2: array-like, leg lengths of right triangle
Returns:
cupy.ndarray: Hypotenuse length
"""Hyperbolic trigonometric functions for exponential relationships.
def sinh(x):
"""
Hyperbolic sine, element-wise.
Parameters:
- x: array-like, input array
Returns:
cupy.ndarray: Hyperbolic sine of each element
"""
def cosh(x):
"""Hyperbolic cosine, element-wise."""
def tanh(x):
"""Hyperbolic tangent, element-wise."""
def arcsinh(x):
"""Inverse hyperbolic sine, element-wise."""
def arccosh(x):
"""Inverse hyperbolic cosine, element-wise."""
def arctanh(x):
"""Inverse hyperbolic tangent, element-wise."""Exponential and logarithmic operations for growth and scaling calculations.
def exp(x):
"""
Calculate exponential of all elements, e^x.
Parameters:
- x: array-like, input array
Returns:
cupy.ndarray: Exponential of each element
"""
def exp2(x):
"""Calculate 2^x for all elements."""
def expm1(x):
"""
Calculate exp(x) - 1 for all elements.
More accurate for x close to zero.
"""
def log(x):
"""
Natural logarithm, element-wise.
Parameters:
- x: array-like, input array with positive values
Returns:
cupy.ndarray: Natural logarithm of each element
"""
def log10(x):
"""Base-10 logarithm, element-wise."""
def log2(x):
"""Base-2 logarithm, element-wise."""
def log1p(x):
"""
Return ln(1 + x), element-wise.
More accurate for x close to zero.
"""
def logaddexp(x1, x2):
"""
Logarithm of sum of exponentials of inputs.
Computes log(exp(x1) + exp(x2)) in numerically stable way.
"""
def logaddexp2(x1, x2):
"""Logarithm of sum of exponentials of inputs in base 2."""Basic arithmetic operations supporting broadcasting and element-wise computation.
def add(x1, x2):
"""
Add arguments element-wise.
Parameters:
- x1, x2: array-like, input arrays
Returns:
cupy.ndarray: Element-wise sum
"""
def subtract(x1, x2):
"""Subtract arguments element-wise."""
def multiply(x1, x2):
"""Multiply arguments element-wise."""
def divide(x1, x2):
"""Divide arguments element-wise."""
def true_divide(x1, x2):
"""True division, returns floating point result."""
def floor_divide(x1, x2):
"""Floor division, largest integer smaller than or equal to division."""
def power(x1, x2):
"""
First array elements raised to powers from second array, element-wise.
Parameters:
- x1: array-like, base values
- x2: array-like, exponent values
Returns:
cupy.ndarray: x1**x2 element-wise
"""
def float_power(x1, x2):
"""Power function that promotes integers to float."""
def remainder(x1, x2):
"""
Return element-wise remainder of division.
Same as x1 % x2.
"""
def mod(x1, x2):
"""Return element-wise remainder of division (alias for remainder)."""
def fmod(x1, x2):
"""Return element-wise remainder of division with same sign as dividend."""
def divmod(x1, x2):
"""
Return element-wise quotient and remainder simultaneously.
Returns:
tuple: (floor_divide(x1, x2), remainder(x1, x2))
"""Functions for rounding and truncating floating-point values.
def around(a, decimals=0):
"""
Round array to given number of decimals.
Parameters:
- a: array-like, input array
- decimals: int, number of decimal places to round to
Returns:
cupy.ndarray: Rounded array
"""
def round(a, decimals=0):
"""Round array to given number of decimals (alias for around)."""
def rint(x):
"""
Round elements to nearest integer.
Returns:
cupy.ndarray: Rounded to nearest integer, dtype unchanged
"""
def ceil(x):
"""
Return ceiling of input, element-wise.
Returns:
cupy.ndarray: Smallest integer >= x
"""
def floor(x):
"""
Return floor of input, element-wise.
Returns:
cupy.ndarray: Largest integer <= x
"""
def trunc(x):
"""Return truncated value, element-wise."""
def fix(x):
"""Round to nearest integer towards zero."""Functions for handling signs and absolute values.
def absolute(x):
"""
Calculate absolute value element-wise.
Parameters:
- x: array-like, input array
Returns:
cupy.ndarray: Absolute values
"""
def abs(x):
"""Calculate absolute value element-wise (alias for absolute)."""
def fabs(x):
"""Absolute value, floating-point version."""
def sign(x):
"""
Return element-wise indication of sign.
Returns:
cupy.ndarray: -1 for negative, 0 for zero, +1 for positive
"""
def copysign(x1, x2):
"""
Change sign of x1 to match sign of x2, element-wise.
Parameters:
- x1: array-like, values to change sign of
- x2: array-like, values whose signs are used
Returns:
cupy.ndarray: x1 with signs of x2
"""
def signbit(x):
"""
Return element-wise True where signbit is set (less than zero).
Returns:
cupy.ndarray: Boolean array indicating sign bit
"""Element-wise comparison and extremum functions.
def maximum(x1, x2):
"""
Element-wise maximum of array elements.
Parameters:
- x1, x2: array-like, input arrays
Returns:
cupy.ndarray: Maximum values element-wise
"""
def minimum(x1, x2):
"""Element-wise minimum of array elements."""
def fmax(x1, x2):
"""Element-wise maximum, ignoring NaNs."""
def fmin(x1, x2):
"""Element-wise minimum, ignoring NaNs."""
def clip(a, a_min, a_max, out=None):
"""
Clip values in array to specified range.
Parameters:
- a: array-like, input array
- a_min: scalar or array, minimum value(s)
- a_max: scalar or array, maximum value(s)
- out: cupy.ndarray, output array
Returns:
cupy.ndarray: Clipped array
"""Functions for calculating roots and powers.
def sqrt(x):
"""
Return non-negative square root, element-wise.
Parameters:
- x: array-like, input array with non-negative values
Returns:
cupy.ndarray: Square root of each element
"""
def cbrt(x):
"""Return cube root, element-wise."""
def square(x):
"""Return element-wise square of input."""
def reciprocal(x):
"""
Return reciprocal of argument, element-wise.
Returns:
cupy.ndarray: 1/x element-wise
"""Specialized mathematical functions for scientific computing.
def heaviside(x1, x2):
"""
Compute Heaviside step function.
Parameters:
- x1: array-like, input values
- x2: array-like, value for x1 == 0
Returns:
cupy.ndarray: 0 where x1 < 0, x2 where x1 == 0, 1 where x1 > 0
"""
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: scalar, value to replace NaN with
- posinf: scalar, value to replace positive infinity with
- neginf: scalar, value to replace negative infinity with
Returns:
cupy.ndarray: Array with finite values
"""
def real_if_close(a, tol=100):
"""
Convert complex array to real if imaginary parts are close to zero.
Parameters:
- a: array-like, input array
- tol: float, tolerance for considering imaginary part as zero
Returns:
cupy.ndarray: Real array if possible, otherwise unchanged
"""
def interp(x, xp, fp, left=None, right=None, period=None):
"""
One-dimensional linear interpolation.
Parameters:
- x: array-like, x-coordinates at which to evaluate interpolated values
- xp: array-like, x-coordinates of data points (must be increasing)
- 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
"""Functions for working with complex numbers.
def real(val):
"""
Return real part of complex argument.
Parameters:
- val: array-like, input array
Returns:
cupy.ndarray: Real component
"""
def imag(val):
"""Return imaginary part of complex argument."""
def conjugate(x):
"""
Return complex conjugate, element-wise.
Returns:
cupy.ndarray: Complex conjugate of each element
"""
def conj(x):
"""Return complex conjugate (alias for conjugate)."""
def angle(z, deg=False):
"""
Return angle of complex argument.
Parameters:
- z: array-like, complex input
- deg: bool, return angle in degrees if True, radians if False
Returns:
cupy.ndarray: Angle of each complex number
"""Functions for manipulating floating-point representations.
def frexp(x):
"""
Decompose elements to mantissa and two's exponent.
Parameters:
- x: array-like, input array
Returns:
tuple: (mantissa, exponent) where x = mantissa * 2**exponent
"""
def ldexp(x1, x2):
"""
Return x1 * 2**x2, element-wise.
Parameters:
- x1: array-like, mantissa
- x2: array-like, exponent
Returns:
cupy.ndarray: x1 * 2**x2
"""
def nextafter(x1, x2):
"""
Return next floating-point value after x1 towards x2.
Returns:
cupy.ndarray: Next representable value
"""
def modf(x):
"""
Return fractional and integer parts of array.
Returns:
tuple: (fractional_part, integer_part)
"""import cupy as cp
import numpy as np
# Create sample data
x = cp.linspace(0, 2*cp.pi, 1000)
y = cp.random.rand(1000)
# Trigonometric functions
sine_wave = cp.sin(x)
cosine_wave = cp.cos(x)
tangent_values = cp.tan(x)
# Exponential and logarithmic
exp_values = cp.exp(y)
log_values = cp.log(y + 1) # Add 1 to avoid log(0)
# Arithmetic operations
a = cp.array([1, 2, 3, 4, 5])
b = cp.array([2, 4, 6, 8, 10])
sum_result = cp.add(a, b)
product = cp.multiply(a, b)
power_result = cp.power(a, 2)
# Complex operations
complex_array = cp.array([1+2j, 3+4j, 5+6j])
real_parts = cp.real(complex_array)
imag_parts = cp.imag(complex_array)
conjugates = cp.conjugate(complex_array)
# Binary operations on integer arrays
int_array1 = cp.array([5, 3, 8, 12], dtype=cp.int32) # Binary: 0101, 0011, 1000, 1100
int_array2 = cp.array([3, 5, 4, 15], dtype=cp.int32) # Binary: 0011, 0101, 0100, 1111
# Bitwise operations
and_result = cp.bitwise_and(int_array1, int_array2) # 1, 1, 0, 12
or_result = cp.bitwise_or(int_array1, int_array2) # 7, 7, 12, 15
xor_result = cp.bitwise_xor(int_array1, int_array2) # 6, 6, 12, 3
not_result = cp.bitwise_not(int_array1) # Bitwise inversion
# Bit shifting
left_shifted = cp.left_shift(int_array1, 2) # Multiply by 4: 20, 12, 32, 48
right_shifted = cp.right_shift(int_array1, 1) # Divide by 2: 2, 1, 4, 6
# Binary packing/unpacking
binary_array = cp.array([0, 1, 1, 0, 1, 0, 1, 1], dtype=cp.uint8)
packed = cp.packbits(binary_array) # Pack into uint8
unpacked = cp.unpackbits(packed) # Unpack back to binaryInstall with Tessl CLI
npx tessl i tessl/pypi-cupy-rocm-4-3