لَآ إِلَـٰهَ إِلَّا هُوَ
LA ILAHA ILLA HU
Allah, Your Lord There Is No Deity Except Him.

# Python Data Science NumPy ufuncs Simple Arithmetic

One can use arithmetic operators + - * / directly between NumPy arrays, but here we will discuss an extension of the same where we have functions that can take any array-like objects e.g. lists, tuples etc. and perform arithmetic conditionally.

What Does Arithmetic Conditionally Means?

Arithmetic Conditionally simply means we can define conditions where the arithmetic operation should happen.

All of the above discussed arithmetic functions take a where parameter in which we can specify that condition.

Addition The add() function sums the content of two arrays, and return the results in a new array.

Example 1: Add the values in arr1 to the values in arr2.

Code

import numpy as np

arr1 = np.array([1, 2, 3, 4, 4, 6])
arr2 = np.array([7, 8, 9, 10, 11, 12])

print(newarr)

the output will be

[ 8 10 12 14 15 18]

The example above will return [ 8 10 12 14 15 18] which is the sums of 1+7, 2+8, 3+9 and so on and so forth.

Subtraction The subtract() function subtracts the values from one array with the values from another array, and return the results in a new array.

Example 2: Subtract the values in arr2 from the values in arr1.

Code

import numpy as np

arr1 = np.array([1, 2, 3, 4, 4, 6])
arr2 = np.array([7, 8, 9, 10, 11, 12])

newarr = np.subtract(arr1, arr2)

print(newarr)

the output will be

[-6 -6 -6 -6 -7 -6]

The example above will return [-6 -6 -6 -6 -7 -6] which is the result of 1-7, 2-8, 3-9 and so on and so forth..

Multiplication The multiply() function multiplies the values from one array with the values from another array, and return the results in a new array.

Example 3: Multiply the values with arr2 from the values in arr1.

Code

import numpy as np

arr1 = np.array([1, 2, 3, 4, 4, 6])
arr2 = np.array([7, 8, 9, 10, 11, 12])

newarr = np.multiply(arr1, arr2)

print(newarr)

the output will be

[[ 7 16 27 40 44 72]

The example above will return [ 7 16 27 40 44 72] which is the result of 1*7, 2*8, 3*9 and so on and so forth..

Division The divide() function divides the values from one array with the values from another array, and return the results in a new array.

Example 4: Divide the values in arr1 with the values in arr2.

Code

import numpy as np

arr1 = np.array([10, 20, 30, 40, 50, 60])
arr2 = np.array([20, 21, 22, 23, 24, 25])

newarr = np.divide(arr1, arr2)

print(newarr)

the output will be

[0.5 0.95238095 1.36363636 1.73913043 2.08333333 2.4 ]

The example above will return

[0.5 0.95238095 1.36363636 1.73913043 2.08333333 2.4 ]

which is the result of 10/20, 20/21, 30/22 and so on and so forth.

Power The power() function rises the values from the first array to the power of the values of the second array, and return the results in a new array.

Example 5: Divide the values in arr1 with the values in arr2.

Code

import numpy as np

arr1 = np.array([10, 20, 30, 40, 50, 60])
arr2 = np.array([2, 4, 5, 8, 2, 20])

newarr = np.power(arr1, arr2)

print(newarr)

the output will be

[[ 100 160000 24300000

6553600000000 2500 -6450068360557232128]

The example above will return

[ 100 160000 24300000

6553600000000 2500 -6450068360557232128]

which is the result of 10*2, 20*4, 30*5 and so on and so forth.

Remainder

Both the mod() and the remainder() functions return the remainder of the values in the first array corresponding to the values in the second array, and return the results in a new array.

Example 6: Return the remainders.

Code

import numpy as np

arr1 = np.array([10, 20, 30, 40, 50, 60])
arr2 = np.array([2, 8, 7, 9, 15, 36])

newarr = np.mod(arr1, arr2)

print(newarr)

the output will be

[ 0 4 2 4 5 24]

The example above will return [ 0 4 2 4 5 24] which is the remainders when you divide 10/2, 20/8, 30/7 and so on and so forth..

You get the same result when using the remainder() function

Example 7: Return the remainders.

Code

import numpy as np

arr1 = np.array([10, 20, 30, 40, 50, 60])
arr2 = np.array([2, 8, 7, 9, 15, 36])

newarr = np.remainder(arr1, arr2)

print(newarr)

the output will be

[ 0 4 2 4 5 24]

Quotient and Mod The divmod() function return both the quotient and the the mod. The return value is two arrays, the first array contains the quotient and second array contains the mod.

Example 8: Return the remainders.

Code

import numpy as np

arr1 = np.array([10, 20, 30, 40, 50, 60])
arr2 = np.array([2, 8, 7, 9, 15, 36])

newarr = np.divmod(arr1, arr2)

print(newarr)

the output will be

(array([5, 2, 4, 4, 3, 1]), array([ 0, 4, 2, 4, 5, 24]))

The example above will return:

(array([5, 2, 4, 4, 3, 1]), array([ 0, 4, 2, 4, 5, 24]))

The first array represents the quotients, (the integer value when you divide 10 with 2, 20 with 8, 30 with 7 etc.

The second array represents the remainders of the same divisions.

Absolute Values Both the absolute() and the abs() functions functions do the same absolute operation element-wise but we should use absolute() to avoid confusion with python's inbuilt math.abs().

Example 9: Absolute Values.

Code

import numpy as np

arr = np.array([-1, 2, 1, -2, 3, -4])

newarr = np.absolute(arr)

print(newarr)

the output will be

[1 2 1 2 3 4]

The example above will return [1 2 1 2 3 4].
Note: NumPy ufunc Universal function or ufuncs is a function which operates on ndarrays in an element by element fashion and supports array broadcasting, type casting, and many other standard features.