NumPy Ufunc

In this tutorial you will learn:

  • What is NumPy ufunc?
  • Characteristics of ufunc?
  • Why we use ufuncs?

NumPy ufunc

NumPy ufunc stands for NumPy universal function. In NumPy library mathematical functions are termed as universal functions and multiple universal functions are provided which could cover a variety of operations. NumPy unfunc is a vectorized wrapper which takes a fixed number of inputs and produce fixed number of output(s). Vectorized operations are accomplished by applying an operation on an array which is then applied to each element separately. NumPy ufunc ensure much faster execution by pushing the loop into compiled layer. NumPy ufunc includes

  • Functions for arithmetic operations
  • Handling complex number
  • Statistical functions
  • Standard trigonometric functions

Characteristics of ufunc

The NumPy ufunc decrease the size and complexity of code and allows programmers to achieve desired output in a quick and easy way. Ufuncs have following characteristics

  • They could only be applied on ndarray, ndarray is the array class of NumPy.
  • The execution of element-wise array operations is fast.
  • It also supports features like error handling, type casting, output type determination.
  • Inorder to use the universal functions we have to declare an object of the “numpy.ufunc” class.
  • Using frompyfunc we can create python functions as universal function, this will enable the programmers to produce more compact code needed and required.
  • Ufuncs are called automatically when we use the corresponding arithmetic operator on two array. As an example, if we compile code below you will obtain the result same as that of applying np.add() on the two input arrays. On using the “+” operator, np.add() will be called automatically.
array_a = array_b+array_c

Why we use ufuncs?

Vectorization is much faster way while carrying out the mathematical operations, using ufuncs the vectorization is performed with minimal line of code and optimum efficiency. The ufuncs provides multiple methods which facilitate in computation. The ufuncs can be given additional parameters like “where”, “dtype” and “out”, hence facilitating the programmers to optimize code with minimal time consumption.

Add new comment