Updated On : Jul-16,2020 Time Investment : ~10 mins

Learning Numpy - Simple Tutorial For Beginners - NumPy - Arithmetic Operations Part 15

Input arrays for performing arithmetic operations such as add(), subtract(), multiply(), and divide() must be either of the same shape or should conform to array broadcasting rules.

Understand The Example Step By Step

import numpy as np
a = np.arange(9, dtype = np.float_).reshape(3,3)
print ('First array:')
print (a)
print ('\n')
First array:
[[0. 1. 2.]
 [3. 4. 5.]
 [6. 7. 8.]]


print ('Second array:')
b = np.array([10,10,10])
print (b)
print ('\n')
Second array:
[10 10 10]


print ('Add the two arrays:')
print (np.add(a,b))
print ('\n')
Add the two arrays:
[[10. 11. 12.]
 [13. 14. 15.]
 [16. 17. 18.]]


print ('Subtract the two arrays:')
print (np.subtract(a,b))
print ('\n')
Subtract the two arrays:
[[-10.  -9.  -8.]
 [ -7.  -6.  -5.]
 [ -4.  -3.  -2.]]


print ('Multiply the two arrays:')
print (np.multiply(a,b))
print ('\n')
Multiply the two arrays:
[[ 0. 10. 20.]
 [30. 40. 50.]
 [60. 70. 80.]]


print ('Divide the two arrays:')
print (np.divide(a,b))
Divide the two arrays:
[[0.  0.1 0.2]
 [0.3 0.4 0.5]
 [0.6 0.7 0.8]]

numpy.reciprocal()

This function returns the reciprocal of argument, element-wise. For elements with absolute values larger than 1, the result is always 0 because of the way in which Python handles integer division. For integer 0, an overflow warning is issued.

import numpy as np
a = np.array([0.25, 1.33, 1, 33, 100])

print ('Our array is:')
print (a)
print ('\n')

print ('After applying reciprocal function:')
print (np.reciprocal(a))
print ('\n')

b = np.array([100], dtype = int)
print ('The second array is:')
print (b)
print ('\n')
Our array is:
[  0.25   1.33   1.    33.   100.  ]


After applying reciprocal function:
[4.         0.7518797  1.         0.03030303 0.01      ]


The second array is:
[100]


numpy.power()

This function treats elements in the first input array as base and returns it raised to the power of the corresponding element in the second input array.

import numpy as np
a = np.array([10,100,1000])

print ('Our array is:')
print (a)
print ('\n')

print ('Applying power function:')
print (np.power(a,2))
print ('\n')

print ('Second array:')
b = np.array([1,2,3])
print (b)
print ('\n')

print ('Applying power function again:')
print (np.power(a,b))
Our array is:
[  10  100 1000]


Applying power function:
[    100   10000 1000000]


Second array:
[1 2 3]


Applying power function again:
[        10      10000 1000000000]

numpy.mod()

This function returns the remainder of division of the corresponding elements in the input array. The function numpy.remainder() also produces the same result.

import numpy as np
a = np.array([10,20,30])
b = np.array([3,5,7])

print ('First array:')
print (a)
print ('\n')

print ('Second array:')
print (b)
print ('\n')

print ('Applying mod() function:')
print (np.mod(a,b))
print ('\n')

print ('Applying remainder() function:')
print (np.remainder(a,b))
First array:
[10 20 30]


Second array:
[3 5 7]


Applying mod() function:
[1 0 2]


Applying remainder() function:
[1 0 2]

The following functions are used to perform operations on array with complex numbers.

  • numpy.real() − returns the real part of the complex data type argument.

  • numpy.imag() − returns the imaginary part of the complex data type argument.

  • numpy.conj() − returns the complex conjugate, which is obtained by changing the sign of the imaginary part.

  • numpy.angle() − returns the angle of the complex argument. The function has degree parameter. If true, the angle in the degree is returned, otherwise the angle is in radians.

Understand The Example Step By Step

import numpy as np
a = np.array([-5.6j, 0.2j, 11. , 1+1j])
print ('Our array is:')
print (a)
Our array is:
[-0.-5.6j  0.+0.2j 11.+0.j   1.+1.j ]
print ('Applying real() function:')
print (np.real(a))
Applying real() function:
[-0.  0. 11.  1.]
print ('Applying imag() function:')
print (np.imag(a))
Applying imag() function:
[-5.6  0.2  0.   1. ]
print ('Applying conj() function:')
print (np.conj(a))
Applying conj() function:
[-0.+5.6j  0.-0.2j 11.-0.j   1.-1.j ]
print ('Applying angle() function:')
print (np.angle(a))
Applying angle() function:
[-1.57079633  1.57079633  0.          0.78539816]
print ('Applying angle() function again (result in degrees)')
print (np.angle(a, deg = True))
Applying angle() function again (result in degrees)
[-90.  90.   0.  45.]
Dolly Solanki  Dolly Solanki

Share Views Stuck Somewhere? Need Help with Coding? Have Doubts About the Topic/Code?

When going through coding examples, it's quite common to have doubts and errors.

If you have doubts about some code examples or are stuck somewhere when trying our code, send us an email at coderzcolumn07@gmail.com. We'll help you or point you in the direction where you can find a solution to your problem.

You can even send us a mail if you are trying something new and need guidance regarding coding. We'll try to respond as soon as possible.

Share Views Want to Share Your Views? Have Any Suggestions?

If you want to

  • provide some suggestions on topic
  • share your views
  • include some details in tutorial
  • suggest some new topics on which we should create tutorials/blogs
Please feel free to contact us at coderzcolumn07@gmail.com. We appreciate and value your feedbacks. You can also support us with a small contribution by clicking DONATE.