NumPy Study Notes: Universal Functions

  1. Universal Functions

Universal functions (ufuncs) enable NumPy arrays to perform element-wise operations efficiently. These functions are implemented in C, providing significant performance benefits over pure Python loops.

7.1 Mathematical Operations

7.1.1 Arithmetic Operations

Universal Function Description
add(x1, x2[, y]) y = x1 + x2
subtract(x1, x2[, y]) y = x1 - x2
multiply(x1, x2[, y]) y = x1 * x2
divide(x1, x2[, y]) y = x1 / x2
floor_divide(x1, x2[, y]) y = x1 // x2
power(x1, x2[, y]) y = x1 ** x2
import numpy as np
arr1 = np.array([5, 10])
arr2 = np.array([3, 7])
# Pre-allocate output array with matching shape
output = np.empty(2, dtype=np.int32)
np.subtract(arr1, arr2, output)
output

array([2, 3])

# Direct assignment is more convenient
result = np.power(arr1, arr2)
result

array([  125, 1000000])

7.1.2 Comparison Oeprations

Universal Function Description
equal(x1, x2[, y]) y = (x1 == x2)
not_equal(x1, x2[, y]) y = (x1 != x2)
less(x1, x2[, y]) y = (x1 < x2)
less_equal(x1, x2[, y]) y = (x1 <= x2)
greater(x1, x2[, y]) y = (x1 > x2)
greater_equal(x1, x2[, y]) y = (x1 >= x2)
values_a = np.array([15, 42])
values_b = np.array([20, 42])
comparison = np.greater(values_a, values_b)
comparison

array([False,  True])

verify = np.equal(values_a, values_b)
verify

array([False,  True])

check_result = np.zeros(2)
# When using third parameter for output, values are represented as 0 (false) and 1 (true)
np.greater_equal(values_a, values_b, check_result)

array([0., 1.])

7.2 Creating Custom Universal Functions

Custom universal functions allow element-wise operations using any Python function. The syntax is:

custom_ufunc = numpy.frompyfunc(func, nin, nout)

  • func: Any Python function (built-in or user-defined)
  • nin: Number of input arrays
  • nout: Number of output arrays

This returns a custom universal function with type numpy.ufunc.

Example 1: Creating a universla function for absolute values using len (returns string length)

len_ufunc = np.frompyfunc(len, 1, 1)

len_ufunc = np.frompyfunc(len, 1, 1)
text_matrix = np.array([['hello', 'world'], ['numpy', 'array']])
# Apply len function to each element
lengths = len_ufunc(text_matrix)
lengths

array([[5, 5], [5, 5]], dtype=object)

Example 2: Handling two inputs and producing two outputs Define multiply_div function that computes product and quotient:

def multiply_div(x, y):
    return x * y, x / y

Create the universal function with 2 inputs and 2 outputs:

multiply_div_ufunc = np.frompyfunc(multiply_div, 2, 2)

def multiply_div(x, y):
    return x * y, x / y

multiply_div_ufunc = np.frompyfunc(multiply_div, 2, 2)

first = np.array([[6, 8], [10, 12]])
second = np.array([[2, 4], [2, 3]])
product, quotient = multiply_div_ufunc(first, second)
print("Products:", product, "\nQuotients:", quotient)

Products: [[12 32]
 [20 36]] 
Quotients: [[3.  2. ]
 [5.  4. ]]

Tags: Numpy ufunc universal-functions numpy-arrays element-wise-operations

Posted on Thu, 09 Jul 2026 16:19:28 +0000 by river001