Universal functions (cupy.ufunc
)#
CuPy provides universal functions (a.k.a. ufuncs) to support various elementwise operations. CuPy’s ufunc supports following features of NumPy’s one:
Broadcasting
Output type determination
Casting rules
CuPy’s ufunc currently does not provide methods such as reduce
, accumulate
, reduceat
, outer
, and at
.
ufunc#
|
Universal function. |
Available ufuncs#
Math operations#
Adds two arrays elementwise. |
|
Subtracts arguments elementwise. |
|
Multiplies two arrays elementwise. |
|
matmul(x1, x2, /, out=None, **kwargs) |
|
Elementwise true division (i.e. |
|
Computes |
|
Computes |
|
Elementwise true division (i.e. |
|
Elementwise floor division (i.e. |
|
Takes numerical negative elementwise. |
|
Takes numerical positive elementwise. |
|
Computes |
|
First array elements raised to powers from second array, element-wise. |
|
Computes the remainder of Python division elementwise. |
|
Computes the remainder of Python division elementwise. |
|
Computes the remainder of C division elementwise. |
|
Elementwise absolute value function. |
|
Calculates absolute values element-wise. |
|
Rounds each element of an array to the nearest integer. |
|
Elementwise sign function. |
|
Returns the complex conjugate, element-wise. |
|
Returns the complex conjugate, element-wise. |
|
Elementwise exponential function. |
|
Elementwise exponentiation with base 2. |
|
Elementwise natural logarithm function. |
|
Elementwise binary logarithm function. |
|
Elementwise common logarithm function. |
|
Computes |
|
Computes |
|
Elementwise square root function. |
|
Elementwise square function. |
|
Elementwise cube root function. |
|
Computes |
|
Computes gcd of |
|
Computes lcm of |
Trigonometric functions#
Elementwise sine function. |
|
Elementwise cosine function. |
|
Elementwise tangent function. |
|
Elementwise inverse-sine function (a.k.a. |
|
Elementwise inverse-cosine function (a.k.a. |
|
Elementwise inverse-tangent function (a.k.a. |
|
Elementwise inverse-tangent of the ratio of two arrays. |
|
Computes the hypoteneous of orthogonal vectors of given length. |
|
Elementwise hyperbolic sine function. |
|
Elementwise hyperbolic cosine function. |
|
Elementwise hyperbolic tangent function. |
|
Elementwise inverse of hyperbolic sine function. |
|
Elementwise inverse of hyperbolic cosine function. |
|
Elementwise inverse of hyperbolic tangent function. |
|
Converts angles from radians to degrees elementwise. |
|
Converts angles from degrees to radians elementwise. |
|
Converts angles from degrees to radians elementwise. |
|
Converts angles from radians to degrees elementwise. |
Bit-twiddling functions#
Computes the bitwise AND of two arrays elementwise. |
|
Computes the bitwise OR of two arrays elementwise. |
|
Computes the bitwise XOR of two arrays elementwise. |
|
Computes the bitwise NOT of an array elementwise. |
|
Shifts the bits of each integer element to the left. |
|
Shifts the bits of each integer element to the right. |
Comparison functions#
Tests elementwise if |
|
Tests elementwise if |
|
Tests elementwise if |
|
Tests elementwise if |
|
Tests elementwise if |
|
Tests elementwise if |
|
Computes the logical AND of two arrays. |
|
Computes the logical OR of two arrays. |
|
Computes the logical XOR of two arrays. |
|
Computes the logical NOT of an array. |
|
Takes the maximum of two arrays elementwise. |
|
Takes the minimum of two arrays elementwise. |
|
Takes the maximum of two arrays elementwise. |
|
Takes the minimum of two arrays elementwise. |
Floating functions#
Tests finiteness elementwise. |
|
Tests if each element is the positive or negative infinity. |
|
Tests if each element is a NaN. |
|
Calculates absolute values element-wise. |
|
Tests elementwise if the sign bit is set (i.e. |
|
Returns the first argument with the sign bit of the second elementwise. |
|
Computes the nearest neighbor float values towards the second argument. |
|
Extracts the fractional and integral parts of an array elementwise. |
|
Computes |
|
Decomposes each element to mantissa and two's exponent. |
|
Computes the remainder of C division elementwise. |
|
Rounds each element of an array to its floor integer. |
|
Rounds each element of an array to its ceiling integer. |
|
Rounds each element of an array towards zero. |
ufunc.at#
Currently, CuPy does not support at
for ufuncs in general.
However, cupyx.scatter_add()
can substitute add.at
as both behave identically.
Generalized Universal Functions#
In addition to regular ufuncs, CuPy also provides a wrapper class to convert
regular cupy functions into Generalized Universal Functions as in NumPy https://numpy.org/doc/stable/reference/c-api/generalized-ufuncs.html.
This allows to automatically use keyword arguments such as axes
, order
, dtype
without needing to explicitly implement them in the wrapped function.
|
Creates a Generalized Universal Function by wrapping a user provided function with the signature. |