# 3.2. Array Arithmetic¶

## 3.2.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])


import numpy as np

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

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


## 3.2.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.2.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.2.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.2.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.2.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.2.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.2.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]])