NumPy Tutorial NumPy Statistics NumPy References

NumPy - Arithmetic Functions



The NumPy package contains a number of arithmetic functions which provides all the functionality required for various arithmetic operations. It includes arithmetic functions and functions for handling complex numbers. Below mentioned are the most frequently used arithmetic functions.

FunctionDescription
add() Add arguments element-wise.
subtract() Subtract arguments, element-wise.
multiply() Multiply arguments element-wise.
divide() Returns a true division of the inputs, element-wise.
mod() Return element-wise remainder of division.
reciprocal() Return the reciprocal of the argument, element-wise.
power() Returns base raised to the power of exponent.
real() Return the real part of the complex argument.
imag() Return the imaginary part of the complex argument.
conjugate() Return the complex conjugate, element-wise.
angle() Return the angle of the complex argument.

Lets discuss these functions in detail:

Basic Arithmetic Operations

Basic arithmetic operations can be performed on given arrays, element-wise using add(), subtract(), multiply() and divide() functions. The syntax for using this function is given below:

Syntax

numpy.add(x1, x2, out=None)
numpy.subtract(x1, x2, out=None)
numpy.multiply(x1, x2, out=None)
numpy.divide(x1, x2, out=None)

Parameters

x1, x2 Required. Specify the arrays. If x1.shape != x2.shape, they must be broadcastable to a common shape.
out Optional. Specify a location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned.

Example:

The example below shows the usage of add() function.

import numpy as np
Arr1 = np.array([[10,20,30], [40,50,60]])
Arr2 = np.array([[5,10,15]])

print("Arr1 is:")
print(Arr1)

print("\nArr2 is:")
print(Arr2)

#Here, Arr1 and Arr2 are broadcastable
#add elements of Arr1 and Arr2
print("\nadd(Arr1, Arr2) returns:")
print(np.add(Arr1, Arr2))

#subtract elements of Arr2 from Arr1
print("\nsubtract(Arr1, Arr2) returns:")
print(np.subtract(Arr1, Arr2))

#multiply elements of Arr1 with Arr2
print("\nmultiply(Arr1, Arr2) returns:")
print(np.multiply(Arr1, Arr2))

#divide elements of Arr1 by Arr2
print("\ndivide(Arr1, Arr2) returns:")
print(np.divide(Arr1, Arr2))

The output of the above code will be:

Arr1 is:
[[10 20 30]
 [40 50 60]]

Arr2 is:
[[ 5 10 15]]

add(Arr1, Arr2) returns:
[[15 30 45]
 [45 60 75]]

subtract(Arr1, Arr2) returns:
[[ 5 10 15]
 [35 40 45]]

multiply(Arr1, Arr2) returns:
[[ 50 200 450]
 [200 500 900]]

divide(Arr1, Arr2) returns:
[[2. 2. 2.]
 [8. 5. 4.]]

numpy.reciprocal() function

The numpy.reciprocal() function returns the reciprocal of the argument, element-wise.

Syntax

numpy.reciprocal(x, out=None, dtype=None)

Parameters

x Required. Specify the input array.
out Optional. Specify a location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned.
dtype Optional. Specify the desired data type of returned array.

Example:

The example below shows the usage of reciprocal() function.

import numpy as np
Arr = np.array([[2,4],[5,10]])

print("Arr is:")
print(Arr)

#reciprocal of Arr
print("\nReciprocal array is:")
print(np.reciprocal(Arr, dtype=float))

The output of the above code will be:

Arr is:
[[ 2  4]
 [ 5 10]]

Reciprocal array is:
[[0.5  0.25]
 [0.2  0.1 ]]

numpy.mod() function

The numpy.mod() function returns element-wise remainder of division.

Syntax

numpy.mod(x1, x2, out=None)

Parameters

x1, x2 Required. Specify arrays to be divided: x1 as dividend and x2 as divisor. If x1.shape != x2.shape, they must be broadcastable to a common shape.
out Optional. Specify a location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned.

Example:

The example below shows the usage of mod() function.

import numpy as np
Arr1 = np.array([[10,20,30],[40,50,60]])
Arr2 = np.array([[6,7,9]])

print("Arr1 is:")
print(Arr1)

print("\nArr2 is:")
print(Arr2)

#Here, Arr1 and Arr2 are broadcastable
#Remainder when Arr1 is divided by Arr2
print("\nmod(Arr1, Arr2) returns:")
print(np.mod(Arr1, Arr2))

The output of the above code will be:

Arr1 is:
[[10 20 30]
 [40 50 60]]

Arr2 is:
[[6 7 9]]

mod(Arr1, Arr2) returns:
[[4 6 3]
 [4 1 6]]

numpy.power() function

The numpy.power() function is used to calculate first array elements raised to powers from second array, element-wise.

Syntax

numpy.power(x, y, out=None)

Parameters

x Required. Specify first array (array_like) containing elements as base.
y Required. Specify second array (array_like) containing elements as exponential. If x.shape != y.shape, they must be broadcastable to a common shape (which becomes the shape of the output).
out Optional. Specify a location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned.

Example:

This example below shows the usage of power() function.

import numpy as np

base = np.array([5, 6, 7])
exponent = np.array([0, 1, 2])

print("The power of values:")
print(np.power(base, exponent))

The output of the above code will be:

The power of values:
[ 1  6 49]

Handling Complex Numbers

The NumPy package provides following functions which can be used while working with complex numbers.

  • real(): Return the real part of the complex argument.
  • imag(): Return the imaginary part of the complex argument.
  • conjugate(): Return the complex conjugate, element-wise.
  • angle(): Return the angle of the complex argument.

Syntax

numpy.real(z)
numpy.imag(z)
numpy.conjugate(z, out=None)
numpy.angle(z, deg=False)

Parameters

z Required. Specify the input array.
deg Optional. Return angle in degrees if True, radians if False (default).
out Optional. Specify a location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned.

Example:

The example below shows the usage of above mentioned functions.

import numpy as np

Arr = np.array([[1+1j,1+2j],
                [1+3j,1+4j]])

print("Arr is:")
print(Arr)

#real part of Arr
print("\nReal part of Arr is:")
print(np.real(Arr))

#imaginary part of Arr
print("\nImaginary part of Arr is:")
print(np.imag(Arr))

#conjugate of Arr
print("\nConjuagte of Arr is:")
print(np.conjugate(Arr))

#calculating angle of Arr
print("\nAngle of Arr is:")
print(np.angle(Arr, deg=True))

The output of the above code will be:

Arr is:
[[1.+1.j 1.+2.j]
 [1.+3.j 1.+4.j]]

Real part of Arr is:
[[1. 1.]
 [1. 1.]]

Imaginary part of Arr is:
[[1. 2.]
 [3. 4.]]

Conjugate of Arr is:
[[1.-1.j 1.-2.j]
 [1.-3.j 1.-4.j]]

Angle of Arr is:
[[45.         63.43494882]
 [71.56505118 75.96375653]]