NumPy - 15 Universal Functions

Universal Functions (Ufuncs)

A universal function (ufunc) is a function that performs element-wise operations on data in ndarrays. Ufuncs act as fast, vectorized wrappers for simple functions that take one or more scalar values and produce one or more scalar results.

Ufuncs can be classified into two types:

  • Unary ufuncs: These take a single input array and perform operations on each element (e.g., numpy.sqrt or numpy.exp).
  • Binary ufuncs: These take two input arrays and perform operations element-wise between them (e.g., numpy.add or numpy.maximum).

Key Features of Ufuncs:

  • Out Argument: Ufuncs accept an optional out argument that allows them to assign their results into an existing array rather than creating a new one.
  • Continuous Updates: New ufuncs are regularly added to NumPy, so it’s best to consult the official documentation for a comprehensive list and stay up-to-date.

Common Unary Ufuncs

FunctionDescription
abs, fabsCompute the absolute value element-wise for integer, floating-point, or complex values.
sqrtCompute the square root of each element (equivalent to arr ** 0.5).
squareCompute the square of each element (equivalent to arr ** 2).
expCompute the exponent e^x of each element.
log, log10, log2, log1pCompute the natural logarithm (base e), log base 10, log base 2, and log(1 + x) respectively.
signCompute the sign of each element: 1 (positive), 0 (zero), or –1 (negative).
ceilCompute the ceiling of each element (i.e., the smallest integer greater than or equal to the number).
floorCompute the floor of each element (i.e., the largest integer less than or equal to the number).
rintRound elements to the nearest integer, preserving the dtype.
modfReturn fractional and integral parts of an array as separate arrays.
isnanReturn a Boolean array indicating whether each value is NaN (Not a Number).
isfinite, isinfReturn a Boolean array indicating whether each element is finite (non-inf, non-NaN) or infinite, respectively.
cos, cosh, sin, sinh, tan, tanhRegular and hyperbolic trigonometric functions.
arccos, arccosh, arcsin, arcsinh, arctan, arctanhInverse trigonometric functions.
logical_notCompute the truth value of not x element-wise (equivalent to ~arr).

Common Binary Ufuncs

FunctionDescription
addAdd corresponding elements in two arrays.
subtractSubtract elements in the second array from the first array.
multiplyMultiply array elements element-wise.
divide, floor_divideDivide or floor divide (truncating the remainder) element-wise.
powerRaise elements in the first array to powers indicated in the second array.
maximum, fmaxElement-wise maximum; fmax ignores NaN values.
minimum, fminElement-wise minimum; fmin ignores NaN values.
modElement-wise modulus (remainder of division).
copysignCopy the sign of values in the second argument to values in the first argument.
greater, greater_equal, less, less_equal, equal, not_equalPerform element-wise comparison, yielding a Boolean array (equivalent to infix operators: >, >=, <, <=, ==, !=).
logical_andCompute the element-wise truth value of the AND (&) logical operation.
logical_orCompute the element-wise truth value of the OR (`
logical_xorCompute the element-wise truth value of the XOR (^) logical operation.

These ufuncs provide powerful, efficient tools for performing element-wise operations on NumPy arrays.