3.1. Array Arithmetic

3.1.1. Rationale

Scalar

Single Value

Vectorized Operations

Single statement without a loop that explains a looping concept. Applies operation to each element.

import numpy as np


a = np.array([1, 2, 3])

a + 1
# array([2, 3, 4])

3.1.2. Addition

import numpy as np


a = np.array([[1, 2, 3],
              [4, 5, 6]])

a + 2
# array([[3, 4, 5],
#        [6, 7, 8]])

3.1.3. Subtraction

import numpy as np


a = np.array([[1, 2, 3],
              [4, 5, 6]])

a - 2
# array([[-1,  0,  1],
#        [ 2,  3,  4]])

3.1.4. Division

import numpy as np


a = np.array([[1, 2, 3],
              [4, 5, 6]])

a / 2
# array([[0.5, 1. , 1.5],
#        [2. , 2.5, 3. ]])

3.1.5. True Division

import numpy as np


a = np.array([[1, 2, 3],
              [4, 5, 6]])

a // 2
# array([[0, 1, 1],
#        [2, 2, 3]])

3.1.6. Modulo

import numpy as np


a = np.array([[1, 2, 3],
              [4, 5, 6]])

a % 2
# array([[1, 0, 1],
#        [0, 1, 0]])

3.1.7. Multiplication

import numpy as np


a = np.array([[1, 2, 3],
              [4, 5, 6]])

a * 2
# array([[ 2,  4,  6],
#        [ 8, 10, 12]])

3.1.8. Power

import numpy as np


a = np.array([[1, 2, 3],
              [4, 5, 6]])

a ** 2
# array([[ 1,  4,  9],
#        [16, 25, 36]])

3.1.9. Roots

import numpy as np


a = np.array([[1, 2, 3],
              [4, 5, 6]])

a ** (1/2)
# array([[1.        , 1.41421356, 1.73205081],
#        [2.        , 2.23606798, 2.44948974]])

3.1.10. Assignments

Todo

Create assignments