Open In App

NumPy ufuncs | Universal functions

Last Updated : 10 Dec, 2025
Comments
Improve
Suggest changes
6 Likes
Like
Report

NumPy ufuncs (universal functions) are fast, vectorized functions that perform element-wise operations on NumPy arrays. They are highly optimized and support features like broadcasting and automatic type handling. NumPy includes many ufuncs for arithmetic, trigonometry, statistics, etc, and they execute operations very fast because they are implemented in optimized C code.

Why use ufuncs?

  1. Fast vectorized operations: Ufuncs apply calculations to entire arrays at once, making them much faster than Python loops.
  2. Automatic broadcasting and type handling: They adjust array shapes automatically and handle datatype conversion internally, reducing errors.
  3. Cleaner and more efficient code: They simplify complex numerical tasks into short, readable code that scales well on large datasets.

Basic Universal Functions (ufunc) in NumPy

1. Trigonometric functions

NumPy provides several trigonometric functions that operate element-wise on arrays. Angles should be in radians, so degree values must be converted using np.deg2rad(). These functions include standard, inverse, and hyperbolic trigonometric operations.

Common Trigonometric ufuncs in NumPy

FunctionDescription
np.sin, np.cos, np.tanCompute sine, cosine and tangent of angles
np.arcsin, np.arccos, np.arctanCalculate inverse sine, cosine and tangent
np.sinh, np.cosh, np.tanhCompute hyperbolic sine, cosine and tangent
np.deg2radConvert degrees to radians
np.rad2degConvert radians to degrees
np.hypotCalculate hypotenuse of a right triangle

Example: This example demonstrates sine, inverse sine, hyperbolic sine, and hypotenuse calculation using NumPy arrays.

Python
import numpy as np

angles = np.array([0, 30, 45, 60, 90]) 
rad = np.deg2rad(angles)  # convert degrees to radians

# Sine of angles
sin_vals = np.sin(rad)
print("Sine values:", sin_vals)

# Inverse sine in degrees
inv_sin = np.rad2deg(np.arcsin(sin_vals))
print("Inverse sine (degrees):", inv_sin)

# Hyperbolic sine
sinh_vals = np.sinh(rad)
print("Hyperbolic sine:", sinh_vals)

# Hypotenuse of a right triangle
hyp = np.hypot(3, 4)
print("Hypotenuse:", hyp)

Output

Sine values: [0. 0.5 0.70710678 0.8660254 1. ]
Inverse sine (degrees): [ 0. 30. 45. 60. 90.]
Hyperbolic sine: [0. 0.54785347 0.86867096 1.24936705 2.3012989 ]
Hypotenuse: 5.0

Explanation:

  • np.deg2rad() converts degrees to radians.
  • np.sin() computes sine for each element.
  • np.arcsin() calculates inverse sine; np.rad2deg() converts it back to degrees.
  • np.sinh() applies the hyperbolic sine function element-wise.
  • np.hypot(a, b) computes the hypotenuse of a right triangle with sides a and b.

2. Statistical functions

NumPy provides several statistical functions to calculate properties like mean, median, variance, and range. These functions operate element-wise and along specified axes, making analysis of arrays fast and efficient.

Common Statistical ufuncs in NumPy

FunctionDescription
np.amin, np.amaxMinimum or maximum of an array or along a specific axis
np.ptpRange (max − min) of array values
np.percentile(a, p)p-th percentile of array values
np.meanCompute mean of data
np.medianCompute median of data
np.stdCompute standard deviation
np.varCompute variance
np.averageCompute average value

Example: This example demonstrates common statistical calculations on an array of student weights.

Python
import numpy as np

weights = np.array([50.7, 52.5, 50, 58, 55.63, 73.25, 49.5, 45])

# Min and Max
print("Min and Max:", np.amin(weights), np.amax(weights))

# Range
print("Range:", np.ptp(weights))

# 70th Percentile
print("70th Percentile:", np.percentile(weights, 70))

# Mean
print("Mean:", np.mean(weights))

# Median
print("Median:", np.median(weights))

# Standard Deviation
print("Std Dev:", np.std(weights))

# Variance
print("Variance:", np.var(weights))

# Average
print("Average:", np.average(weights))

Output

Min and Max: 45.0 73.25
Range: 28.25
70th Percentile: 55.317
Mean: 54.3225
Median: 51.6
Std Dev: 8.052773978574091
Variance: 64.84716875
Average: 54.3225

Explanation:

  • np.amin() and np.amax() find the smallest and largest values.
  • np.ptp() calculates the range (max − min).
  • np.percentile(weights, 70) finds the value below which 70% of weights fall.
  • np.mean() and np.average() compute the average of the array.
  • np.median() returns the middle value.
  • np.std() and np.var() measure dispersion via standard deviation and variance.

3. Bitwise Functions

NumPy provides bitwise functions to perform operations on the binary representation of integers. These allow element-wise manipulation of arrays at the bit level.

Common Bitwise ufuncs in NumPy

FunctionDescription
np.bitwise_andElement-wise AND operation
np.bitwise_orElement-wise OR operation
np.bitwise_xorElement-wise XOR operation
np.invertBitwise NOT (invert) of elements
np.left_shiftShift bits of elements to the left
np.right_shiftShift bits of elements to the right

Example: This example demonstrates basic bitwise operations on arrays of integers.

Python
import numpy as np

even = np.array([0, 2, 4, 6, 8, 16, 32])
odd  = np.array([1, 3, 5, 7, 9, 17, 33])

# Bitwise AND, OR, XOR
print("AND:", np.bitwise_and(even, odd))
print("OR :", np.bitwise_or(even, odd))
print("XOR:", np.bitwise_xor(even, odd))

# Bitwise NOT
print("Invert:", np.invert(even))

# Bit shifts
print("Left shift :", np.left_shift(even, 1))
print("Right shift:", np.right_shift(even, 1))

Output
AND: [ 0  2  4  6  8 16 32]
OR : [ 1  3  5  7  9 17 33]
XOR: [1 1 1 1 1 1 1]
Invert: [ -1  -3  -5  -7  -9 -17 -33]
Left shift : [ 0  4  8 12 16 32 64]
Right shift: [ 0  1  2  3  4  8 16]

Explanation:

  • np.bitwise_and(), np.bitwise_or(), np.bitwise_xor() perform element-wise logical operations at the bit level.
  • np.invert() flips all bits of each element.
  • np.left_shift() and np.right_shift() move the bits of each element left or right, effectively multiplying or dividing by powers of 2.

Article Tags :

Explore