Skip to main content

Creating Ufunc


In this tutorial you will learn:

  • How to create own ufunc?
  • Checking types of funcs?
  • Python Syntax

Creating a ufunc

NumPy provides programmers a flexibility to create a ufunc by letting the programmers define a ufunc and adding that function in NumPy ufunc library using the inbuilt method “frompyfunc”. “frompyfunc” function takes in 3 mandatory parameters

  • The name of function, but before passing the function as argument the function has to be defined.
  • Number of inputs of the defined function
  • Number of output from the defined function

Lets take an example, we will be defining a function of subtraction and will add it into ufunc library. Here in the first line of code we are importing the NumPy lib, and defining the function in fourth line of the code.

  1. #importing the NumPy library
  2. import numpy as np
  3. #defining the function
  4. def subs(x, y):
  5. return x-y
  6. #Here we are adding the subtraction func "subs" to the ufunc lib,we are giving in the parameters with two input values and resultantly one output value
  7. subs = np.frompyfunc(subs, 2, 1)
  8. #Here we are declaring two arrays
  9. array_a=[10,20,30,40]
  10. array_b=[1,2,3,4]
  11. #Using the arrays as parameters for input into the subs() function
  12. array_c=subs(array_a,array_b)
  13. #here we are printing the result
  14. print ("Result after subtraction of 2 arrays")
  15. print(array_c)

Type of functions

We can know the type of each function whether it’s a built-in function or a universal function using the type() function. This function takes in a function name and returns its type.
In this example we will be finding the type of a function which is a ufunc and is used to carry out subtraction operation

  1. import numpy as np
  2. #printing type of the function “subtract”
  3. print ("Type of the function 'Subtract()'")
  4. print(type(np.subtract))

In this example we will find the type of a function which is not a universal function.
  1. import numpy as np
  2. #printing type of the function “hsplit”
  3. print ("Type of the function 'hsplit()'")
  4. print(type(np.hsplit))

We can also use the if and else statements to check if the function is built in, a universal function or none of them.
  1. import numpy as np
  2. #name of function is given here
  3. funcName = np.subtract
  4. #here we get the type of function
  5. tfunc = type(funcName)
  6. #here we are converting the type object to a string
  7. tfunc = str(tfunc)
  8. #applying if and else statements
  9. if tfunc == "<class 'numpy.ufunc'>":
  10. print('The function is a universal function')
  11. elif tfunc == "<class 'function'>":
  12. print('The function is a built-in function')
  13. else:
  14. print('It is not a built-in nor a universal function')

Tags

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.