Python numpy Arithmetic Operations

Python numpy module provides various arithmetic functions such as add, subtract, multiply and divide, which performs Python numpy arithmetic operations on arrays. Apart from them, you can use the standard Arithmetic Operators also. Arrays should be of the same shape, or they have to be bound to array rules to use these functions.

We use the below arrays to demonstrate the Python numpy Arithmetic Operations using arithmetic functions and arithmetic operators.

a = np.array([10, 50, 100, 150, 250])
a 

b = np.array([6, 5, 4, 3, 2])
b
 
c = np.array([[26,  48,  91,  57, 120], [33,  95,  68, 109, 155], [111, 194,   7,  22, 124], [ 82, 119,  18, 156,  81],[ 38,  10, 151,  24,  14]])
c
# You can create an array of random numbers using the below statement
#c = np.random.randint(0, 200, size = (5, 5))
#c
 
d = np.array([[12, 11,  0,  9,  7], [10,  4, 11,  6,  9], [ 9,  2, 10,  9, 11], [ 5, 14,  0, 11,  8], [ 5, 12,  5,  5, 11]])
d
 
#d = np.random.randint(0, 15, size = (5, 5))
#d
 
e = np.array([11, 22, 33, 44, 55])
e
Python NumPy Arithmetic Operations 1

Python numpy Arithmetic Operations Examples

The list of Arithmetic Operators and arithmetic functions that are available to perform NumPy Arithmetic Operations in Python

Python numpy add function

This NumPy add function adds two arrays.

np.add(a, b)
np.add(c, d)

Let me try to add a NumPy array of different sizes

np.add(a, c)

We got an unexpected result. Let me try to add three arrays 

np.add(a, b, c)
NumPy add function 1

I think you understand the limitations of using this Python numpy add function.

Python numpy Arithmetic Operator +

Let me use the Arithmetic Operator + to add arrays

a + b
c + d
a + c
a + b + c
a + b + c + d
Python NumPy Arithmetic Add Operations 2

Python numpy subtract function

This Python numpy subtract function subtracts one array from another array.

np.subtract(a, b)
np.subtract(c, d)
np.subtract(d, c)

We are subtracting an array of different sizes.

np.subtract(a, c)

Let me try to use Python numpy subtract function on three arrays 

np.subtract(a, b, c)
NumPy subtract function 1

Python numpy Arithmetic Operations Operator –

We got an unexpected result!. Let me use the Arithmetic Operator – to subtract arrays

a - b
c - d
a - c
a - b - c
c - a - b - d
Python NumPy Arithmetic Operations subtract 3

Python numpy multiply function

This Python numpy multiply function multiplies two arrays.

np.multiply(a, b)
np.multiply(c, d)
np.multiply(d, c)

Multiply an array of different sizes.

np.multiply(a, c)

Let me multiply three arrays of different sizes

np.multiply(a, b, c)
NumPy multiply function 1

Arithmetic Operator *

This time, we are using the Arithmetic Operator * to multiply arrays

a * b
c * d
a * c
a * b * c
a * b * c * d
Python NumPy multiply Arithmetic Operations 4

Python numpy divide function

The Python numpydivide function divides one array from another.

np.divide(a, b)
np.divide(c, d)
np.divide(d, c)

Divide an array of different sizes.

np.divide(a, c)
NumPy divide function 1

Arithmetic Operator /

Using the Arithmetic Operator / to divide those arrays

a / b
d / c
a / c
a / b / c
c / a / b / d
Python NumPy divide Arithmetic Operations 5

Python numpy mod function

The Python numpy mod function returns the remainder of the division.

np.mod(a, 3)
np.mod(a, 6)
np.mod(c, 4)
np.mod(d, 2)

Now, we used this Python numpy mod function on multiple arrays

np.mod(a, b)
np.mod(a, e)
np.mod(d, c)
NumPy mod function 1

Python numpy remainder function

Similar to mod function, the Python numpy reminder function returns the remainder of the arithmetic division.

np.remainder(a, 4)
np.remainder(a, 7)
np.remainder(c, 5)
np.remainder(d, 9)

Let me use this numpy remainder function with two arguments as arrays

np.remainder(a, b)
np.remainder(a, e)

np.remainder(c, d)
np.remainder(d, b)
NumPy remainder function 1