NumPy ufuncs

🔢 NumPy Universal Functions (ufuncs)

Universal functions (ufuncs) in NumPy are vectorized functions that operate element-wise on arrays.
They are highly optimized, fast, and avoid explicit Python loops.


✅ 1. Types of ufuncs

  1. Arithmetic ufuncs

  2. Comparison ufuncs

  3. Logical ufuncs

  4. Trigonometric ufuncs

  5. Exponential and logarithmic ufuncs

  6. Statistical ufuncs (like sum, mean, etc.)


✅ 2. Arithmetic ufuncs

Function Description
np.add(x, y) Element-wise addition
np.subtract(x, y) Element-wise subtraction
np.multiply(x, y) Element-wise multiplication
np.divide(x, y) Element-wise division
np.power(x, y) Element-wise power
np.mod(x, y) Element-wise modulus
import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])

print(np.add(a, b)) # [5 7 9]
print(np.subtract(a, b)) # [-3 -3 -3]
print(np.multiply(a, b)) # [4 10 18]
print(np.divide(b, a)) # [4. 2.5 2.]
print(np.power(a, 2)) # [1 4 9]


✅ 3. Comparison ufuncs

Function Description
np.equal(x, y) Element-wise equality
np.not_equal(x, y) Element-wise inequality
np.greater(x, y) Element-wise greater than
np.less(x, y) Element-wise less than
np.greater_equal(x, y) Element-wise ≥
np.less_equal(x, y) Element-wise ≤
print(np.equal(a, b)) # [False False False]
print(np.greater(a, b)) # [False False False]
print(np.less(a, b)) # [ True True True]

✅ 4. Logical ufuncs

Function Description
np.logical_and(x, y) Element-wise AND
np.logical_or(x, y) Element-wise OR
np.logical_not(x) Element-wise NOT
np.logical_xor(x, y) Element-wise XOR
x = np.array([True, False, True])
y = np.array([False, False, True])

print(np.logical_and(x, y)) # [False False True]
print(np.logical_or(x, y)) # [ True False True]
print(np.logical_not(x)) # [False True False]


✅ 5. Trigonometric ufuncs

Function Description
np.sin(x) Sine
np.cos(x) Cosine
np.tan(x) Tangent
np.arcsin(x) Inverse sine
np.arccos(x) Inverse cosine
np.arctan(x) Inverse tangent
angles = np.array([0, np.pi/2, np.pi])
print(np.sin(angles)) # [0. 1. 0.]
print(np.cos(angles)) # [ 1. 0. -1.]

✅ 6. Exponential & Logarithmic ufuncs

Function Description
np.exp(x) e^x
np.exp2(x) 2^x
np.log(x) Natural logarithm
np.log2(x) Base-2 logarithm
np.log10(x) Base-10 logarithm
np.sqrt(x) Square root
np.square(x) Element-wise square
x = np.array([1, 2, 3])
print(np.exp(x)) # [ 2.718 7.389 20.086]
print(np.log(x)) # [0. 0.693 1.098]
print(np.sqrt(x)) # [1. 1.414 1.732]
print(np.square(x)) # [1 4 9]

✅ 7. Statistical ufuncs

Function Description
np.sum(x) Sum of elements
np.mean(x) Mean
np.std(x) Standard deviation
np.var(x) Variance
np.min(x) Minimum
np.max(x) Maximum
x = np.array([1, 2, 3, 4])
print(np.sum(x)) # 10
print(np.mean(x)) # 2.5
print(np.std(x)) # 1.118
print(np.min(x)) # 1
print(np.max(x)) # 4

✅ 8. Using ufunc with Scalars

print(np.add(5, 3)) # 8
print(np.sin(np.pi / 2)) # 1.0
  • ufuncs work with arrays or scalars seamlessly


🧠 Summary

  • ufuncs = element-wise, fast operations

  • Covers arithmetic, comparison, logical, trigonometric, exponential, statistical functions

  • Avoids Python loops → vectorized computations


🎯 Practice Exercises

  1. Create an array [1,2,3,4,5] and compute square, square root, exp, and log using ufuncs.

  2. Compare two arrays element-wise for greater, equal, and less.

  3. Use logical ufuncs to find elements >2 AND <5 in an array.

CodeCapsule

Sanjit Sinha — Web Developer | PHP • Laravel • CodeIgniter • MySQL • Bootstrap Founder, CodeCapsule — Student projects & practical coding guides. Email: info@codecapsule.in • Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *