# Basic arithmetic operation

Lesson 7 Chapter 3

Let's first cover the basic arithmetic operations with some examples:

# Import Numpy library import numpy as np # Create two arrays a = np.array([[1,5],[0,8]], dtype=np.float32) b = np.array([[2,1],[5,2]], dtype=np.float32) print('a= ', a) print('b= ', b) # Elementwise adding # a + b and np.add(a, b) are the same. # Check to see if a + b and np.add(a, b) are the same using ``assert``. # .all() method check if all elements of a matrix is True. print('a + b= ', a + b) assert((a + b == np.add(a, b)).all()) # Elementwise subtraction print('a - b= ', a - b) assert((a - b == np.subtract(a, b)).all()) # Elementwise multiplication print('a * b= ', a * b) assert((a * b == np.multiply(a, b)).all()) # Elementwise division print('a / b= ',a / b) assert((a / b == np.divide(a, b)).all()) # Elementwise square print('a^2= ', np.square(a)) assert((a ** 2 == np.square(a)).all())

a= [[1. 5.] [0. 8.]] b= [[2. 1.] [5. 2.]] a + b= [[ 3. 6.] [ 5. 10.]] a - b= [[-1. 4.] [-5. 6.]] a * b= [[ 2. 5.] [ 0. 16.]] a / b= [[0.5 5. ] [0. 4. ]] a^2= [[ 1. 25.] [ 0. 64.]]