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#

ufunc(name, nin, nout, _Ops ops[, preamble, ...])

Universal function.

Available ufuncs#

Math operations#

add

Adds two arrays elementwise.

subtract

Subtracts arguments elementwise.

multiply

Multiplies two arrays elementwise.

matmul

matmul(x1, x2, /, out=None, **kwargs)

divide

Elementwise true division (i.e.

logaddexp

Computes log(exp(x1) + exp(x2)) elementwise.

logaddexp2

Computes log2(exp2(x1) + exp2(x2)) elementwise.

true_divide

Elementwise true division (i.e.

floor_divide

Elementwise floor division (i.e.

negative

Takes numerical negative elementwise.

positive

Takes numerical positive elementwise.

power

Computes x1 ** x2 elementwise.

float_power

First array elements raised to powers from second array, element-wise.

remainder

Computes the remainder of Python division elementwise.

mod

Computes the remainder of Python division elementwise.

fmod

Computes the remainder of C division elementwise.

divmod

absolute

Elementwise absolute value function.

fabs

Calculates absolute values element-wise.

rint

Rounds each element of an array to the nearest integer.

sign

Elementwise sign function.

conj

Returns the complex conjugate, element-wise.

conjugate

Returns the complex conjugate, element-wise.

exp

Elementwise exponential function.

exp2

Elementwise exponentiation with base 2.

log

Elementwise natural logarithm function.

log2

Elementwise binary logarithm function.

log10

Elementwise common logarithm function.

expm1

Computes exp(x) - 1 elementwise.

log1p

Computes log(1 + x) elementwise.

sqrt

Elementwise square root function.

square

Elementwise square function.

cbrt

Elementwise cube root function.

reciprocal

Computes 1 / x elementwise.

gcd

Computes gcd of x1 and x2 elementwise.

lcm

Computes lcm of x1 and x2 elementwise.

Trigonometric functions#

sin

Elementwise sine function.

cos

Elementwise cosine function.

tan

Elementwise tangent function.

arcsin

Elementwise inverse-sine function (a.k.a.

arccos

Elementwise inverse-cosine function (a.k.a.

arctan

Elementwise inverse-tangent function (a.k.a.

arctan2

Elementwise inverse-tangent of the ratio of two arrays.

hypot

Computes the hypoteneous of orthogonal vectors of given length.

sinh

Elementwise hyperbolic sine function.

cosh

Elementwise hyperbolic cosine function.

tanh

Elementwise hyperbolic tangent function.

arcsinh

Elementwise inverse of hyperbolic sine function.

arccosh

Elementwise inverse of hyperbolic cosine function.

arctanh

Elementwise inverse of hyperbolic tangent function.

degrees

Converts angles from radians to degrees elementwise.

radians

Converts angles from degrees to radians elementwise.

deg2rad

Converts angles from degrees to radians elementwise.

rad2deg

Converts angles from radians to degrees elementwise.

Bit-twiddling functions#

bitwise_and

Computes the bitwise AND of two arrays elementwise.

bitwise_or

Computes the bitwise OR of two arrays elementwise.

bitwise_xor

Computes the bitwise XOR of two arrays elementwise.

invert

Computes the bitwise NOT of an array elementwise.

left_shift

Shifts the bits of each integer element to the left.

right_shift

Shifts the bits of each integer element to the right.

Comparison functions#

greater

Tests elementwise if x1 > x2.

greater_equal

Tests elementwise if x1 >= x2.

less

Tests elementwise if x1 < x2.

less_equal

Tests elementwise if x1 <= x2.

not_equal

Tests elementwise if x1 != x2.

equal

Tests elementwise if x1 == x2.

logical_and

Computes the logical AND of two arrays.

logical_or

Computes the logical OR of two arrays.

logical_xor

Computes the logical XOR of two arrays.

logical_not

Computes the logical NOT of an array.

maximum

Takes the maximum of two arrays elementwise.

minimum

Takes the minimum of two arrays elementwise.

fmax

Takes the maximum of two arrays elementwise.

fmin

Takes the minimum of two arrays elementwise.

Floating functions#

isfinite

Tests finiteness elementwise.

isinf

Tests if each element is the positive or negative infinity.

isnan

Tests if each element is a NaN.

fabs

Calculates absolute values element-wise.

signbit

Tests elementwise if the sign bit is set (i.e.

copysign

Returns the first argument with the sign bit of the second elementwise.

nextafter

Computes the nearest neighbor float values towards the second argument.

modf

Extracts the fractional and integral parts of an array elementwise.

ldexp

Computes x1 * 2 ** x2 elementwise.

frexp

Decomposes each element to mantissa and two's exponent.

fmod

Computes the remainder of C division elementwise.

floor

Rounds each element of an array to its floor integer.

ceil

Rounds each element of an array to its ceiling integer.

trunc

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.

GeneralizedUFunc(func, signature, **kwargs)

Creates a Generalized Universal Function by wrapping a user provided function with the signature.