Python NumPy ufunc: Simple Arithmetic Operations

Last updated 3 months, 3 weeks ago | 239 views 75     5

Tags:- Python NumPy

When working with numerical data in Python, NumPy is the go-to library for high-performance operations. One of the key features that powers NumPy’s speed and simplicity is the universal function (ufunc) framework.

This article focuses on using NumPy ufuncs for basic arithmetic operations like addition, subtraction, multiplication, division, and more.


What is a ufunc in NumPy?

A universal function (ufunc) is a function that operates element-wise on arrays. NumPy’s built-in ufuncs are:

  • Fast – implemented in C

  • Broadcastable – support operations on arrays of different shapes

  • Vectorized – avoid explicit Python loops

NumPy includes ufuncs for arithmetic, trigonometry, statistics, and more.


➕ Basic Arithmetic ufuncs

NumPy provides several built-in ufuncs for performing arithmetic operations. Below are the most commonly used ones:

Operation ufunc Equivalent Operator
Addition np.add() +
Subtraction np.subtract() -
Multiplication np.multiply() *
Division np.divide() /
Floor Division np.floor_divide() //
Modulus np.mod() %
Power np.power() **

Step-by-Step Examples

Let’s explore how to use each of these with example arrays.

Setup

import numpy as np

a = np.array([10, 20, 30])
b = np.array([1, 2, 3])

➕ 1. Addition

result = np.add(a, b)
print("Addition:", result)
# Output: [11 22 33]

➖ 2. Subtraction

result = np.subtract(a, b)
print("Subtraction:", result)
# Output: [ 9 18 27]

✖️ 3. Multiplication

result = np.multiply(a, b)
print("Multiplication:", result)
# Output: [10 40 90]

➗ 4. Division

result = np.divide(a, b)
print("Division:", result)
# Output: [10. 10. 10.]

⛳ 5. Floor Division

result = np.floor_divide(a, b)
print("Floor Division:", result)
# Output: [10 10 10]

6. Modulus

result = np.mod(a, b)
print("Modulus:", result)
# Output: [0 0 0]

⚡ 7. Power

result = np.power(a, b)
print("Power:", result)
# Output: [  10  400 27000]

✅ Full Working Example

import numpy as np

a = np.array([4, 9, 16])
b = np.array([2, 3, 4])

print("a + b =", np.add(a, b))
print("a - b =", np.subtract(a, b))
print("a * b =", np.multiply(a, b))
print("a / b =", np.divide(a, b))
print("a // b =", np.floor_divide(a, b))
print("a % b =", np.mod(a, b))
print("a ** b =", np.power(a, b))

Output:

a + b = [ 6 12 20]
a - b = [ 2  6 12]
a * b = [ 8 27 64]
a / b = [2. 3. 4.]
a // b = [2 3 4]
a % b = [0 0 0]
a ** b = [  16  729 65536]

Tips

  • Broadcasting: You can perform operations between arrays of different shapes if they follow broadcasting rules.

    a = np.array([1, 2, 3])
    b = 2
    print(np.add(a, b))  # Output: [3 4 5]
    
  • Chaining ufuncs: You can chain operations for concise and fast code.

    result = np.multiply(np.add(a, b), 2)
    
  • Use inplace operations: To reduce memory overhead, use out parameter.

    np.add(a, b, out=a)
    

Common Pitfalls

Pitfall Solution
Mismatched shapes Understand broadcasting
Integer division losing precision Use np.true_divide() if needed
Unexpected object dtype Avoid using mixed data types

Summary

NumPy's ufuncs for simple arithmetic provide:

  • Clean syntax

  • Fast computation

  • Element-wise operations

  • Compatibility with broadcasting

By leveraging built-in ufuncs, you can write cleaner, faster, and more efficient data-processing code.


What's Next?