Menu
×
×
   ❮   
PYTHON FOR DJANGO DJANGO FOR BEGINNERS DJANGO SPECIFICS PAYMENT INTEGRATION API BASICS NUMPY FOR ML Roadmap
     ❯   

MATHEMATICAL OPERATIONS IN NUMPY

Universal Functions (ufuncs)

×

Share this Topic

Share Via:

Thank you for sharing!


Universal Functions (ufuncs) in NumPy

In NumPy, Universal Functions (ufuncs) are a powerful feature that allows you to apply vectorized operations efficiently on arrays. These functions perform element-wise operations, enabling you to perform mathematical computations without the need for explicit loops, making your code faster and more concise.

What Are ufuncs?

Ufuncs are functions that operate element-wise on data, allowing operations like square roots, trigonometric functions, and logarithms to be applied to entire arrays in a single call. This is achieved through vectorized execution, which is significantly faster than using loops in Python.

Common Examples of ufuncs

  • Square Root: Computes the square root of each element in the array.
  • Exponential (e^x): Computes the exponential of each element in the array.
  • Natural Logarithm (ln): Calculates the natural logarithm of each element in the array.
  • Base-10 Logarithm: Computes the base-10 logarithm of each element in the array.
  • Sine, Cosine, Tangent: Applies trigonometric functions (sin, cos, tan) element-wise on the array.

Advantages of ufuncs:

  • Faster Execution: Ufuncs use highly optimized C implementations, resulting in faster computations compared to standard Python loops.
  • Broadcasting Support: Ufuncs automatically handle broadcasting when performing operations between arrays of different shapes, simplifying the code and improving performance.
  • Parallel Execution: Many ufuncs support parallel execution on multi-core processors, which enhances performance on larger datasets.

Example of Using ufuncs


import numpy as np
arr = np.array([1, 2, 3, 4, 5])

print("Square root:", np.sqrt(arr))             # [1. 1.414 1.732 2. 2.236]
print("Exponential (e^x):", np.exp(arr))        # [ 2.718  7.389 20.085 54.598 148.413]
print("Natural logarithm (ln):", np.log(arr))  # [0. 0.693 1.099 1.386 1.609]

Ufuncs play a vital role in NumPy's ability to handle large datasets efficiently, offering both speed and flexibility in array operations.


Django-tutorial.dev is dedicated to providing beginner-friendly tutorials on Django development. Examples are simplified to enhance readability and ease of learning. Tutorials, references, and examples are continuously reviewed to ensure accuracy, but we cannot guarantee complete correctness of all content. By using Django-tutorial.dev, you agree to have read and accepted our terms of use , cookie policy and privacy policy.

© 2025 Django-tutorial.dev .All Rights Reserved.
Django-tutorial.dev is styled using Bootstrap 5.
And W3.CSS.