CuPy: NumPy & SciPy for GPU - A NumPy/SciPy-compatible array library for GPU-accelerated computing with Python, specifically built for CUDA 11.1
—
Comprehensive mathematical function library providing GPU-accelerated implementations of trigonometric, hyperbolic, exponential, logarithmic, arithmetic, and special functions. All functions support universal function (ufunc) behavior with broadcasting, element-wise operations, and advanced indexing.
Standard trigonometric functions and their inverses, optimized for GPU execution.
def sin(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True):
"""
Trigonometric sine, element-wise.
Parameters:
- x: array_like, input array in radians
- out: ndarray, optional, output array
- where: array_like of bool, optional, condition
- casting: {'no', 'equiv', 'safe', 'same_kind', 'unsafe'}, casting rule
- order: {'K', 'A', 'C', 'F'}, memory layout
- dtype: data type, output type
- subok: bool, whether to return subclass
Returns:
- ndarray: Sine of each element
"""
def cos(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True):
"""
Trigonometric cosine, element-wise.
Parameters:
- x: array_like, input array in radians
- out: ndarray, optional, output array
- where: array_like of bool, optional, condition
- casting: casting rule
- order: memory layout
- dtype: data type, output type
- subok: bool, whether to return subclass
Returns:
- ndarray: Cosine of each element
"""
def tan(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True):
"""
Trigonometric tangent, element-wise.
Parameters:
- x: array_like, input array in radians
- out: ndarray, optional, output array
- where: array_like of bool, optional, condition
- casting: casting rule
- order: memory layout
- dtype: data type, output type
- subok: bool, whether to return subclass
Returns:
- ndarray: Tangent of each element
"""
def arcsin(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True):
"""
Inverse sine, element-wise.
Parameters:
- x: array_like, input array in [-1, 1]
- out: ndarray, optional, output array
- where: array_like of bool, optional, condition
- casting: casting rule
- order: memory layout
- dtype: data type, output type
- subok: bool, whether to return subclass
Returns:
- ndarray: Arcsine in radians [-pi/2, pi/2]
"""
def arccos(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True):
"""
Inverse cosine, element-wise.
Parameters:
- x: array_like, input array in [-1, 1]
- out: ndarray, optional, output array
- where: array_like of bool, optional, condition
- casting: casting rule
- order: memory layout
- dtype: data type, output type
- subok: bool, whether to return subclass
Returns:
- ndarray: Arccosine in radians [0, pi]
"""
def arctan(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True):
"""
Inverse tangent, element-wise.
Parameters:
- x: array_like, input array
- out: ndarray, optional, output array
- where: array_like of bool, optional, condition
- casting: casting rule
- order: memory layout
- dtype: data type, output type
- subok: bool, whether to return subclass
Returns:
- ndarray: Arctangent in radians [-pi/2, pi/2]
"""
def arctan2(y, x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True):
"""
Element-wise arc tangent of y/x choosing quadrant correctly.
Parameters:
- y: array_like, Y-coordinates
- x: array_like, X-coordinates
- out: ndarray, optional, output array
- where: array_like of bool, optional, condition
- casting: casting rule
- order: memory layout
- dtype: data type, output type
- subok: bool, whether to return subclass
Returns:
- ndarray: Angle in radians [-pi, pi]
"""Hyperbolic functions and their inverses for mathematical analysis.
def sinh(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True):
"""
Hyperbolic sine, element-wise.
Parameters:
- x: array_like, input array
- out: ndarray, optional, output array
- where: array_like of bool, optional, condition
- casting: casting rule
- order: memory layout
- dtype: data type, output type
- subok: bool, whether to return subclass
Returns:
- ndarray: Hyperbolic sine of each element
"""
def cosh(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True):
"""
Hyperbolic cosine, element-wise.
Parameters:
- x: array_like, input array
- out: ndarray, optional, output array
- where: array_like of bool, optional, condition
- casting: casting rule
- order: memory layout
- dtype: data type, output type
- subok: bool, whether to return subclass
Returns:
- ndarray: Hyperbolic cosine of each element
"""
def tanh(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True):
"""
Hyperbolic tangent, element-wise.
Parameters:
- x: array_like, input array
- out: ndarray, optional, output array
- where: array_like of bool, optional, condition
- casting: casting rule
- order: memory layout
- dtype: data type, output type
- subok: bool, whether to return subclass
Returns:
- ndarray: Hyperbolic tangent of each element
"""
def arcsinh(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True):
"""
Inverse hyperbolic sine, element-wise.
Parameters:
- x: array_like, input array
- out: ndarray, optional, output array
- where: array_like of bool, optional, condition
- casting: casting rule
- order: memory layout
- dtype: data type, output type
- subok: bool, whether to return subclass
Returns:
- ndarray: Inverse hyperbolic sine of each element
"""
def arccosh(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True):
"""
Inverse hyperbolic cosine, element-wise.
Parameters:
- x: array_like, input array (x >= 1)
- out: ndarray, optional, output array
- where: array_like of bool, optional, condition
- casting: casting rule
- order: memory layout
- dtype: data type, output type
- subok: bool, whether to return subclass
Returns:
- ndarray: Inverse hyperbolic cosine of each element
"""
def arctanh(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True):
"""
Inverse hyperbolic tangent, element-wise.
Parameters:
- x: array_like, input array (|x| < 1)
- out: ndarray, optional, output array
- where: array_like of bool, optional, condition
- casting: casting rule
- order: memory layout
- dtype: data type, output type
- subok: bool, whether to return subclass
Returns:
- ndarray: Inverse hyperbolic tangent of each element
"""Exponential, logarithmic, and power functions for scientific computing.
def exp(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True):
"""
Calculate exponential of all elements, element-wise.
Parameters:
- x: array_like, input array
- out: ndarray, optional, output array
- where: array_like of bool, optional, condition
- casting: casting rule
- order: memory layout
- dtype: data type, output type
- subok: bool, whether to return subclass
Returns:
- ndarray: Element-wise exponential of x
"""
def exp2(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True):
"""
Calculate 2**x for all x in input array.
Parameters:
- x: array_like, input array
- out: ndarray, optional, output array
- where: array_like of bool, optional, condition
- casting: casting rule
- order: memory layout
- dtype: data type, output type
- subok: bool, whether to return subclass
Returns:
- ndarray: Element-wise 2**x
"""
def expm1(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True):
"""
Calculate exp(x) - 1 for all elements.
Parameters:
- x: array_like, input array
- out: ndarray, optional, output array
- where: array_like of bool, optional, condition
- casting: casting rule
- order: memory layout
- dtype: data type, output type
- subok: bool, whether to return subclass
Returns:
- ndarray: Element-wise exp(x) - 1
"""
def log(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True):
"""
Natural logarithm, element-wise.
Parameters:
- x: array_like, input array
- out: ndarray, optional, output array
- where: array_like of bool, optional, condition
- casting: casting rule
- order: memory layout
- dtype: data type, output type
- subok: bool, whether to return subclass
Returns:
- ndarray: Natural logarithm of x
"""
def log2(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True):
"""
Base-2 logarithm, element-wise.
Parameters:
- x: array_like, input array
- out: ndarray, optional, output array
- where: array_like of bool, optional, condition
- casting: casting rule
- order: memory layout
- dtype: data type, output type
- subok: bool, whether to return subclass
Returns:
- ndarray: Base-2 logarithm of x
"""
def log10(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True):
"""
Base-10 logarithm, element-wise.
Parameters:
- x: array_like, input array
- out: ndarray, optional, output array
- where: array_like of bool, optional, condition
- casting: casting rule
- order: memory layout
- dtype: data type, output type
- subok: bool, whether to return subclass
Returns:
- ndarray: Base-10 logarithm of x
"""
def log1p(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True):
"""
Return natural logarithm of one plus input array.
Parameters:
- x: array_like, input array
- out: ndarray, optional, output array
- where: array_like of bool, optional, condition
- casting: casting rule
- order: memory layout
- dtype: data type, output type
- subok: bool, whether to return subclass
Returns:
- ndarray: Natural logarithm of 1 + x
"""Basic arithmetic operations with broadcasting and advanced features.
def add(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True):
"""
Add arguments element-wise.
Parameters:
- x1, x2: array_like, input arrays
- out: ndarray, optional, output array
- where: array_like of bool, optional, condition
- casting: casting rule
- order: memory layout
- dtype: data type, output type
- subok: bool, whether to return subclass
Returns:
- ndarray: Element-wise sum of x1 and x2
"""
def subtract(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True):
"""
Subtract arguments element-wise.
Parameters:
- x1, x2: array_like, input arrays
- out: ndarray, optional, output array
- where: array_like of bool, optional, condition
- casting: casting rule
- order: memory layout
- dtype: data type, output type
- subok: bool, whether to return subclass
Returns:
- ndarray: Element-wise difference x1 - x2
"""
def multiply(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True):
"""
Multiply arguments element-wise.
Parameters:
- x1, x2: array_like, input arrays
- out: ndarray, optional, output array
- where: array_like of bool, optional, condition
- casting: casting rule
- order: memory layout
- dtype: data type, output type
- subok: bool, whether to return subclass
Returns:
- ndarray: Element-wise product x1 * x2
"""
def divide(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True):
"""
Divide arguments element-wise.
Parameters:
- x1, x2: array_like, input arrays
- out: ndarray, optional, output array
- where: array_like of bool, optional, condition
- casting: casting rule
- order: memory layout
- dtype: data type, output type
- subok: bool, whether to return subclass
Returns:
- ndarray: Element-wise division x1 / x2
"""
def power(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True):
"""
First array elements raised to powers from second array.
Parameters:
- x1: array_like, base array
- x2: array_like, exponent array
- out: ndarray, optional, output array
- where: array_like of bool, optional, condition
- casting: casting rule
- order: memory layout
- dtype: data type, output type
- subok: bool, whether to return subclass
Returns:
- ndarray: Element-wise x1**x2
"""
def sqrt(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True):
"""
Return non-negative square-root of array, element-wise.
Parameters:
- x: array_like, input array
- out: ndarray, optional, output array
- where: array_like of bool, optional, condition
- casting: casting rule
- order: memory layout
- dtype: data type, output type
- subok: bool, whether to return subclass
Returns:
- ndarray: Element-wise square root of x
"""
def square(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True):
"""
Return element-wise square of input.
Parameters:
- x: array_like, input array
- out: ndarray, optional, output array
- where: array_like of bool, optional, condition
- casting: casting rule
- order: memory layout
- dtype: data type, output type
- subok: bool, whether to return subclass
Returns:
- ndarray: Element-wise x**2
"""Functions for rounding, ceiling, and floor operations.
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, optional, output array
Returns:
- ndarray: Rounded array
"""
def round_(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, optional, output array
Returns:
- ndarray: Rounded array
"""
def ceil(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True):
"""
Return ceiling of input, element-wise.
Parameters:
- x: array_like, input array
- out: ndarray, optional, output array
- where: array_like of bool, optional, condition
- casting: casting rule
- order: memory layout
- dtype: data type, output type
- subok: bool, whether to return subclass
Returns:
- ndarray: Ceiling of each element
"""
def floor(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True):
"""
Return floor of input, element-wise.
Parameters:
- x: array_like, input array
- out: ndarray, optional, output array
- where: array_like of bool, optional, condition
- casting: casting rule
- order: memory layout
- dtype: data type, output type
- subok: bool, whether to return subclass
Returns:
- ndarray: Floor of each element
"""
def trunc(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True):
"""
Return truncated value of input, element-wise.
Parameters:
- x: array_like, input array
- out: ndarray, optional, output array
- where: array_like of bool, optional, condition
- casting: casting rule
- order: memory layout
- dtype: data type, output type
- subok: bool, whether to return subclass
Returns:
- ndarray: Truncated value of each element
"""Aggregation functions and cumulative operations.
def sum(a, axis=None, dtype=None, out=None, keepdims=False, initial=0, where=True):
"""
Sum of array elements over given axis.
Parameters:
- a: array_like, input array
- axis: None or int or tuple of ints, axis to sum over
- dtype: data type, type of returned array
- out: ndarray, optional, output array
- keepdims: bool, keep reduced dimensions
- initial: scalar, starting value for sum
- where: array_like of bool, elements to include
Returns:
- ndarray: Sum of array elements
"""
def prod(a, axis=None, dtype=None, out=None, keepdims=False, initial=1, where=True):
"""
Product of array elements over given axis.
Parameters:
- a: array_like, input array
- axis: None or int or tuple of ints, axis to multiply over
- dtype: data type, type of returned array
- out: ndarray, optional, output array
- keepdims: bool, keep reduced dimensions
- initial: scalar, starting value for product
- where: array_like of bool, elements to include
Returns:
- ndarray: Product of array elements
"""
def cumsum(a, axis=None, dtype=None, out=None):
"""
Cumulative sum of elements along axis.
Parameters:
- a: array_like, input array
- axis: int, optional, axis along which to compute
- dtype: data type, type of returned array
- out: ndarray, optional, output array
Returns:
- ndarray: Cumulative sum
"""
def cumprod(a, axis=None, dtype=None, out=None):
"""
Cumulative product of elements along axis.
Parameters:
- a: array_like, input array
- axis: int, optional, axis along which to compute
- dtype: data type, type of returned array
- out: ndarray, optional, output array
Returns:
- ndarray: Cumulative product
"""
def diff(a, n=1, axis=-1, prepend=None, append=None):
"""
Calculate n-th discrete difference along 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:
- ndarray: n-th differences
"""Additional mathematical utilities and special functions.
def absolute(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True):
"""
Calculate absolute value element-wise.
Parameters:
- x: array_like, input array
- out: ndarray, optional, output array
- where: array_like of bool, optional, condition
- casting: casting rule
- order: memory layout
- dtype: data type, output type
- subok: bool, whether to return subclass
Returns:
- ndarray: Absolute value of each element
"""
def sign(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True):
"""
Returns element-wise indication of sign of number.
Parameters:
- x: array_like, input array
- out: ndarray, optional, output array
- where: array_like of bool, optional, condition
- casting: casting rule
- order: memory layout
- dtype: data type, output type
- subok: bool, whether to return subclass
Returns:
- ndarray: Sign of each element (-1, 0, 1)
"""
def maximum(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True):
"""
Element-wise maximum of array elements.
Parameters:
- x1, x2: array_like, input arrays
- out: ndarray, optional, output array
- where: array_like of bool, optional, condition
- casting: casting rule
- order: memory layout
- dtype: data type, output type
- subok: bool, whether to return subclass
Returns:
- ndarray: Element-wise maximum
"""
def minimum(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True):
"""
Element-wise minimum of array elements.
Parameters:
- x1, x2: array_like, input arrays
- out: ndarray, optional, output array
- where: array_like of bool, optional, condition
- casting: casting rule
- order: memory layout
- dtype: data type, output type
- subok: bool, whether to return subclass
Returns:
- ndarray: Element-wise minimum
"""
def clip(a, a_min, a_max, out=None, **kwargs):
"""
Clip values in array to specified range.
Parameters:
- a: array_like, input array
- a_min: scalar or array_like, minimum value
- a_max: scalar or array_like, maximum value
- out: ndarray, optional, output array
Returns:
- ndarray: Array with values clipped to [a_min, a_max]
"""import cupy as cp
import numpy as np
# Create test data
x = cp.linspace(0, 2*cp.pi, 100)
y = cp.array([1, 4, 9, 16, 25])
# Trigonometric functions
sin_x = cp.sin(x)
cos_x = cp.cos(x)
tan_x = cp.tan(x)
# Exponential and logarithm
exp_y = cp.exp(y)
log_y = cp.log(y)
sqrt_y = cp.sqrt(y)
# Element-wise arithmetic
a = cp.array([1, 2, 3, 4])
b = cp.array([5, 6, 7, 8])
sum_ab = cp.add(a, b)
prod_ab = cp.multiply(a, b)
power_ab = cp.power(a, b)import cupy as cp
# Create complex operations
x = cp.linspace(-5, 5, 1000)
# Hyperbolic functions
sinh_x = cp.sinh(x)
cosh_x = cp.cosh(x)
tanh_x = cp.tanh(x)
# Special combinations
y = cp.exp(-x**2/2) / cp.sqrt(2*cp.pi) # Gaussian
z = cp.sin(x) * cp.exp(-x/10) # Damped sine
# Aggregation operations
data = cp.random.random((1000, 1000))
total_sum = cp.sum(data)
column_means = cp.mean(data, axis=0)
row_products = cp.prod(data, axis=1)import cupy as cp
# Broadcasting with mathematical functions
a = cp.array([[1, 2, 3], [4, 5, 6]]) # Shape: (2, 3)
b = cp.array([10, 20, 30]) # Shape: (3,)
# Broadcasting in arithmetic operations
result = cp.add(a, b) # Broadcasting: (2,3) + (3,) -> (2,3)
power_result = cp.power(a, 2) # Element-wise squaring
# Universal function features
x = cp.array([1, 2, 3, 4, 5])
where_condition = x > 2
# Conditional operations with where parameter
conditional_sqrt = cp.sqrt(x, where=where_condition)Install with Tessl CLI
npx tessl i tessl/pypi-cupy-cuda111