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: 

import numpy as npy even_arr = npy.array([0, 2, 4, 6, 8, 10]) odd_arr = npy.array([1, 3, 5, 7, 9, 11]) mrx_arr = npy.add(even_arr, odd_arr) print(mrx_arr)

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: 

import numpy as npy reverse_arr1 = npy.array([10, 9, 8, 7, 6]) reverse_arr2 = npy.array([5, 4, 3, 2, 1]) mrx_arr = npy.add(reverse_arr1, reverse_arr2) print(mrx_arr)

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: 

import numpy as npy factorial_arr = npy.array([1, 2, 6, 24, 120]) rational_arr = npy.array([0.4, 4.6, 2.7, 11.5, 94.2]) mrx_arr = npy.subtract(factorial_arr, rational_arr) print(mrx_arr)

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: 

import numpy as npy prime_arr = npy.array([2, 3, 5, 7, 11, 13, 17]) fibonacci_arr = npy.array([0, 1, 1, 2, 3, 5, 8]) mrx_arr = npy.subtract(prime_arr, fibonacci_arr) print(mrx_arr)[be]

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: 

import numpy as npy factorial_arr = npy.array([1, 2, 6, 24, 120]) revsquare_arr = npy.array([25, 16, 9, 4, 1])mrx_arr = npy.multiply(factorial_arr, revsquare_arr) print(mrx_arr)[be]

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: 

import numpy as npy prime_arr = npy.array([2, 3, 5, 7, 11, 13, 17]) fibonacci_arr = npy.array([0, 1, 1, 2, 3, 5, 8])mrx_arr = npy.multiply(prime_arr, fibonacci_arr) print(mrx_arr)[be]

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: 

import numpy as npy gap_arr1 = npy.array([5, 25, 45, 65, 85]) gap_arr2 = npy.array([5, 10, 15, 20, 25]) mrx_arr = npy.divide(gap_arr1, gap_arr2) print(mrx_arr)[be]

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: 

import numpy as npy reverse_list1 = npy.array([10, 9, 8, 7, 6]) reverse_list2 = npy.array([5, 4, 3, 2, 1])mrx_arr = npy.divide(reverse_list1, reverse_list2) print(mrx_arr)[be]

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: 

import numpy as npy num_arr = npy.array([1, 2, 3, 4, 5]) power_arr = npy.array([1, 2, 3, 4, 5]) mrx_arr = npy.power(num_arr, power_arr) print(mrx_arr)

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: 

import numpy as npy num_arr = npy.array([1, 2, 3, 4, 5]) revpower_arr = npy.array([5, 4, 3, 2, 1]) mrx_arr = npy.power(num_arr, revpower_arr) print(mrx_arr)

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: 

import numpy as npy even_arr = npy.array([0, 2, 4, 6, 8, 10]) odd_arr = npy.array([1, 3, 5, 7, 9, 11])mrx_arr = npy.mod(even_arr, odd_arr) print(mrx_arr)

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: 

import numpy as npy factorial_arr = npy.array([1, 2, 6, 24, 120]) gap_arr = npy.array([3, 6, 9, 12, 15])mrx_arr = npy.mod(factorial_arr, gap_arr) print(mrx_arr)

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: 

import numpy as npy even_arr = npy.array([0, 2, 4, 6, 8, 10]) odd_arr = npy.array([1, 3, 5, 7, 9, 11])mrx_arr = npy.remainder(even_arr, odd_arr) print(mrx_arr)

Show the remainder from factorial_arr and gap_arr:

Example: 

import numpy as npy factorial_arr = npy.array([1, 2, 6, 24, 120]) gap_arr = npy.array([3, 6, 9, 12, 15])mrx_arr = npy.mod(factorial_arr, gap_arr) print(mrx_arr)

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: 

import numpy as npy random_arr1 = npy.array([15, 19, 28, 62, 47]) random_arr2 = npy.array([5, 9, 4, 12, 23]) mrx_arr = npy.divmod(random_arr1, random_arr2) print(mrx_arr)

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: 

import numpy as npy even_arr = npy.array([22, 38, 44, 98, 196]) cube_arr = npy.array([1, 8, 27, 64, 125]) mrx_arr = npy.divmod(even_arr, cube_arr) print(mrx_arr)

The example above provides the following results:

Simple Arithmetic In Numpy Ufunc

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: 

import numpy as npy num_arr = npy.array([-1, -2, -3, -4, -5]) mrx_arr = npy.absolute(num_arr) print(mrx_arr)

Firstly convert the two arrays negative value to positive then utilize the add() function:

Example: 

import numpy as npy num_arr1 = npy.array([-1, 2, -3, -4, 5]) num_arr2 = npy.array([-6, 7, 8, -9, -10]) mrx_arr = npy.absolute(num_arr1) ample_arr = npy.absolute(num_arr2) sum_arr = npy.add(mrx_arr, ample_arr) print(sum_arr)

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.

We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

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