Skip to main content

Ufunc Arithmetic


In this tutorial you will learn:

  • What are Arithmetic functions?
  • How to implement simple arithmetic function?
  • Python Syntax

Arithmetic function

Arithmetic operations are the simple operations of addition, subtraction, multiplication and division. The simple arithmetic universal functions in python can take in an array as well as lists and tuples to perform the index wise operations of addition, subtraction, multiplication and division. The simple arithmetic functions can be performed unconditionally and conditionally as well. The ufunc arithmetic functions takes a parameter “where” that ensures that the results are produced conditionally.

Implement simple arithmetic function

Inorder to comprehend the implementation of arithmetic functions, lets take an example, here we will be adding up two tuples. Here in the first line of code we are importing the NumPy lib, and declaring each tuple in subsequent lines. Here you can observe that the arithmetic functions can perform operations on data structures other than arrays as well.

  1. #importing the numpy lib
  2. import numpy as np
  3. #Declaring the first tuple
  4. tup1 = (1,3,5,7,9,11)
  5. #Declaring the second tuple
  6. tup2 = (2,4,6,8,10,12)
  7. #adding up two tuples using add() arithmetic ufunc
  8. res = np.add(tup1,tup2)
  9. print('Result after adding up two tuples:')
  10. print(res)

Now we will apply the add() arithmetic to two 1D arrays the values we will be using will be the same as used for the tuple.
  1. #importing the numpy lib
  2. import numpy as np
  3. #Declaring the first array
  4. array_1 = np.array([1,3,5,7,9,11])
  5. #Declaring the second array
  6. array_2 = np.array([2,4,6,8,10,12])
  7. #adding up two arrays using add() arithmetic ufunc
  8. res_arr = np.add(array_1,array_2)
  9. print('Result after adding up two arrays:')
  10. print(res_arr)

In this example we be carrying out the subtraction op on two tuples. The same operation can be applied on the arrays and list as well with minor changes while declaring each data structure.
  1. #importing the numpy lib
  2. import numpy as np
  3. #Declaring the first array
  4. tup1 = (11,13,15,17,19,21)
  5. #Declaring the second array
  6. tup2 = (1,2,3,4,5,6)
  7. #subtracting two arrays using subtract() arithmetic ufunc
  8. tup_res = np.subtract(tup1, tup2)
  9. print('Result after subtracting two tuples:')
  10. print(tup_res)

This example demonstrates the multiplication operation of two tuples.The same function can be applied for lists and arrays as well.
  1. #importing the numpy lib
  2. import numpy as np
  3. #Declaring the first tuple
  4. tup1 = (1,2,3,4,5,6)
  5. #Declaring the second tuple
  6. tup2 = (20,30,40,50,60,70)
  7. #multiplying two arrays using multiply() arithmetic ufunc
  8. tup_res = np.multiply(tup1, tup2)
  9. print('Result after multiplying two tuples:')
  10. print(tup_res)

In this example we will use the divide one tuple by another tuple and index wise result will be produced.
  1. #importing the numpy lib
  2. import numpy as np
  3. #Declaring the first tuple
  4. tup1 = (10,20,30,40,50,60)
  5. #Declaring the second tuple
  6. tup2 = (1,2,3,4,5,6)
  7. #divide two arrays using divide() arithmetic ufunc
  8. tup_res = np.divide(tup1, tup2)
  9. print('Result after dividing two tuples:')
  10. print(tup_res)

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.