Numpy Ufunc Simple Arithmetic
The purpose of this article is to provide you with some guidance on how to execute simple arithmetic operations utilizing NumPy.
Simple Arithmetic
In Numpy Ufunc simple arithmetic, you can use the arithmetic operators + – * /
This section explains how to extend NumPy’s array features by providing functions that can take any array-like object, such as lists, tuples, etc. and execute conditional arithmetic on it.
Arithmetic Conditionally: In other words, we can specify the conditions under which an arithmetic calculation should be performed.
There is a where parameter in each arithmetic function that we examined, which lets us indicate the condition to be satisfied.
Addition
According to Numpy Ufunc simple arithmetic, the add() function adds up the data of two arrays, and displays the outcome of the calculation in another array.
The numbers in even_arr array will be added to the numbers in odd_arr array as follows:
Example: 
As a result of the example above, we will receive [1 5 9 13 17 21], which is the total of 0+1, 2+3, 4+5, 6+7, 8+9, and 10+11.
The reverse_arr1 numbers are added to the reverse_arr2 numbers in the following way:
Example: 
In the example above, we get [15 13 11 9 7], or in other words, a sum of 10+5, 9+4, 8+3, 7+2, and 6+1.
Subtraction
In Numpy Ufunc simple arithmetic, subtract() function removes the elements from one array from the elements from another array.
Calculate the difference between factorial_arr and rational_arr by subtracting them:
Example: 
As a result of 1-0.4, 2-4.6, 6-2.7, 24-11.5 and 120-94.2, the example above provides [ 0.6 -2.6 3.3 12.5 25.8].
Subtract prime_arr from fibonacci_arr to compute the difference:
Example: 
In the example above, [2 2 4 5 8 8 9] will be produced from 2-0, 3-1, 5-1, 7-2, 11-3, 13-5, and 17-8.
Multiplication
When we examine Numpy Ufunc simple arithmetic, the multiply() function multiplies the elements from one array by the elements from a second array, and displays the output.
In the following example multiply factorial_arr array to revsquare_arr array:
Example: 
Utilizing the example above, you will get [25 32 54 96 120] which is the product of 1*25, 2*16, 6*9, 24*4 and 120*1.
Find the product of prime_arr and fibonacci_arr by multiplying them:
Example: 
In the example above, [0 3 5 14 33 65 136] represents the output of multiplying 2*0, 3*1, 5*1, 7*2, 11*3, 13*5 and 17*8.
Division
With Numpy Ufunc simple arithmetic, the divide() function divides the elements from one array by the elements from a second array, and stores the outcome in a new array.
Take the values from gap_arr1 and gap_arr2 and divide them:
Example: 
Utilizing the example above, you will get [1. 2.5 3. 3.25 3.4 ] which is the division of 5/5, 25/10, 45/15, 65/20 and 85/25.
You can divide the reverse_list1 and reverse_list2 numbers as follows:
Example: 
In the example above, [2. 2.25 2.66666667 3.5 6. ] represents the output of divisions 10/5, 9/4, 8/3, 7/2 and 6/1.
Power
In Numpy Ufunc simple arithmetic, power() increases the numbers from the initial array to the power of the numbers from the second array.
Increase the numbers in num_arr to the power of the numbers in power_arr:
Example: 
As a result of the example above, we will receive [ 1 4 27 256 3125], which is the total of 1*1, 2*2, 3*3*3, 4*4*4*4 and 5*5*5*5*5.
Implement the above example by changing the order of the power array:
Example: 
In the example above, we get [ 1 16 27 16 5], or in other words, a increment of 1*1*1*1*1, 2*2*2*2, 3*3*3, 4*4, and 5*1.
Remainder
The mod() and remainder() functions compute the remainder of the elements in the first array in relation to the elements in the secondary array.
The remainders below two arrays should be displayed as follows:
Example: 
Using the example above, you will get [ 0 2 4 6 8 10 ] which represents the remainder of dividing 0 with 1 (0%1), 2 with 3 (2%3), 4 with 5 (4%5), 6 with 7 (6%7), 8 with 9 (8%9), and 10 with 11 (10%11).
If there are any remainders in the below example, return them as follows:
Example: 
According to the example above, [1 2 6 0 0] is the remainder when you divide 1 by 3 (1%3), 2 by 6 (2%6), 6 by 9 (6%9), 24 by 12 (24%12), and 120 by 15 (120%15).
When you utilize the remainder() function, you will receive the identical outcome as when you utilize the mod() function:
Utilize the remainder() function to generate remainders:
Example: 
Show the remainder from factorial_arr and gap_arr:
Example: 
Quotient and Mod
It is important to note that the divmod() function provides both the quotient and the mod. The output value is composed of two arrays, the first array holding the quotient, and the second array holding the mod of the quotient.
Calculate the quotient and mod of two random arrays as follows:
Example: 
As a result of the example above:
(array([15, 19, 28, 62, 47]),
array([5, 9, 4, 12, 23]))
In the first array, you have the quotients, that is, when you divide 15 with 5, 19 with 9, 28 with 4, etc., the output is an integer.
In the second array, the remainders of the previous divisions are displayed.
Display the quoteint and mod with even_arr and cube_arr arrays:
Example: 
The example above provides the following results:
The first array shows the quotients, which is equivalent to the integer value of the outcome when you divide 22 by 1, 38 by 8, 44 by 27, etc.
In the second array, the remainder of similar divisions is presented.
Absolute Values
There is no difference between the abs() and absolute() functions in the sense that both behave in the same absolute way element-wise, but we should utilize absolute() to reduce conflict with Python’s inbuilt math.abs() function.
Convert all the negative values of num_arr to positive with the absolute() function:
Example: 
Firstly convert the two arrays negative value to positive then utilize the add() function:
Example: 
Example Explanation
First, the absolute() function is applied to num_arr1 and num_arr2 to get the absolute values of each element in the arrays.
This means that any negative values are converted to positive values.
Then, the add() function is used to add the resulting arrays mrx_arr and ample_arr element-wise.
This results in a new array sum_arr containing the sum of the corresponding absolute values in num_arr1 and num_arr2.