Simple Arithmetic – NumPy uFuncs (Python Tutorial)

Simple Arithmetic NumPy UFuncs (Python Tutorial)

This is a detailed tutorial of the NumPy Simple Arithmetic Universal Functions. Learn the usage of these functions with the help of examples.

Simple Arithmetic

As we all know, we have various arithmetic operators that are helpful in data modification of NumPy arrays. So in this section, we will discuss some functions which will take in the array-like objects e.g. lists and tuples and will use the arithmetic functions which will work under some conditions. Conditions will work in such a way that we define that on what condition an arithmetic operation will work.

In order to apply these conditions, we use a parameter known as where which helps in specifying the condition. This condition will act as the basis on which the arithmetic function will work upon. Let us discuss some of the arithmetic function:

Addition:

This function helps in the addition of the NumPy arrays. This will return us the result in the new array. The add()  function will help us in performing this operation.

So let us go through an example to understand it better:

#now we will import the numpy package and then make an alias as np
import numpy as np
# here we will take the two set of values which we are going to add up
a=np.array([10, 30, 50, 70, 90])
b=np.array([20, 40, 60, 80, 100])
#we will use the add() function here
c=np.add(a, b)
#now we will print the added list
print(c)

Output.

[ 30 70 110 150 190]

Now we see in this example that we are adding up two NumPy arrays which as a result gives us the following outcome.

Subtraction

In this function, we will perform the functionality of subtraction where will subtract one NumPy array from another. The function used for this is subtract(). This function will help in subtracting and the result will come in a new array.

Let us go through an example to understand it better:

#now we will import the numpy package and then make an alias as np
import numpy as np
# here we will take the two set of values
a=np.array([10, 30, 50, 70, 90])
b=np.array([20, 40, 60, 80, 100])
#we will use the subtract() function here
c=np.subtract(a, b)
#now we will print the list
print(c)

Output.

[-10 -10 -10 -10 -10]

Here we are subtracting the two arrays from each other and getting the following results. And we are getting minus sign in front of every result because the numbers in the second array are bigger than the first one.

Multiplication

With the help of this function, we will multiply the values between the two NumPy arrays. This result will be given in a new array. The function with help of which we can perform this multiplication is multiply().

Let us go through an example to understand it better:

#now we will import the numpy package and then make an alias as np
import numpy as np
# here we will take the two set of values which we are going to multiply
a=np.array([10, 30, 50, 70, 90])
b=np.array([20, 40, 60, 80, 100])
#we will use the multiply() function here
c=np.multiply(a, b)
#now we will print the list
print(c)

Output.

[ 200 1200 3000 5600 9000]

now we are trying here to multiply every first value with the second one.

Division

In order to divide the values of two NumPy arrays from each other, we use this function and the resultant array will be a completely new array. So the function we use to perform this function is known as divide().

Let us go through an example to understand it in a better way:

#now we will import the numpy package and then make an alias as np
import numpy as np
# here we will take the two set of values which we are going to divide
a=np.array([10, 30, 50, 70, 90])
b=np.array([20, 40, 60, 80, 100])
#we will use the divide() function here
c=np.divide(a, b)
#now we will print the list
print(c)

Output.

[ 0.5 0.75 0.83333333 0.875 0.9 ]

Here in this example, we are trying to divide two arrays and as a result, we get another array with new divided values.

Power

In this operation, we have two arrays in which the values in the second array will be the power of the values in the first array. In this also the results will be returned in the new array. And the function with help of which we will perform this operation is power() .

Let us go through an example:

#now we will import the numpy package and then make an alias as np
import numpy as np
# here we will take the two set of values which we are going to find power for 
a=np.array([2, 3, 5, 7, 9])
b=np.array([3, 4, 6, 8, 7])
#we will use the power() function here
c=np.power(a, b)
#now we will print the list
print(c)

Output.

[ 8 81 15625 5764801 4782969]

Here in this example, we see that  2 has the power of 3 which means that 2 will go through multiplication three times. so we know that 2*2*2=8.

Remainder

This function will help us in getting the remainder of the values in the first array corresponding to the values in the second array. The result will be given in the form of a new array. In this, we can use two functions which are namely mod() and remainder().

Let us go through an example to understand it better:

#now we will import the numpy package and then make an alias as np
import numpy as np
# here we will take the two set of value
a=np.array([10, 30, 50, 70, 90])
b=np.array([20, 40, 60, 80, 100])
#we will use the mod() function here
c=np.mod(a, b)
#now we will print the list
print(c)

Output.

[10 30 50 70 90]

And also we will take another example where we will use the remainder function.

#now we will import the numpy package and then make an alias as np
import numpy as np
# here we will take the two set of values which we are going to find remainder
a=np.array([10, 30, 50, 70, 90])
b=np.array([20, 40, 60, 80, 100])
#we will use the remainder() function here
c=np.remainder(a, b)
#now we will print the list
print(c)

Output.

[10 30 50 70 90]

So we notice that both functions return the same value. As a result, we can use any one of these functions.

Quotient and Mod

This function helps us in returning both the quotient and mod of the two given NumPy arrays. As a result, we will get two arrays where the first array will have the quotient and the second will return mod. The function with help of which we can go through this is divmod() .

Let us go through an example to understand it better:

#now we will import the numpy package and then make an alias as np
import numpy as np
# here we will take the two set of values 
a=np.array([4, 8, 9, 12, 14])
b=np.array([3, 4, 6, 8, 7])
#we will use the divmod() function here
c=np.divmod(a, b)
#now we will print the list
print(c)

Output.


So here in this example, we are getting two arrays where the first array is giving us the quotient and the second array is giving us the remainder.

Absolute Values

In this, we will be getting the absolute values for a given NumPy array. In this, we are using two functions which are absolute() and abs() but we should prefer to use absolute() to avoid any sort of confusion. 

Let us take an example to understand it better:

#now we will import the numpy package and then make an alias as np
import numpy as np
# here we will take the two set of values which we are going to find absolute values for
a=np.array([4, 8, -9, 12, 14, -1, -2])
#we will use the absolute() function here
c=np.absolute(a)
#now we will print th list
print(c)

Output.

[ 4 8 9 12 14 1 2]

Here we get the absolute values for the array we gave as input.

I hope you found this guide useful. If so, do share it with others who are willing to learn Numpy and Python. If you have any questions related to this article, feel free to ask us in the comments section.

And do not forget to subscribe to WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

Your email address will not be published. Required fields are marked *