# Math Symbols Explained with Python

When working with Machine Learning projects, you will come across a wide variety of equations that you need to implement in code. Mathematical notations capture a concept so eloquently but unfamiliarity with them makes them obscure.

In this post, I’ll be explaining the most common math notations by connecting it with its analogous concept in Python. Once you learn them, you will be able to intuitively grasp the intention of an equation and be able to implement it in code.

\[ \frac{1}{N} \sum_{i=1}^{N} (y_i - \hat{y_i})^2 \]

## Indexing

\[ x_i \]

This symbol is taking the value at i\(^{th}\) index of a vector.

```
= [10, 20, 30]
x = 0
i print(x[i]) # 10
```

This can be extended for 2D vectors and so on. \[ x_{ij} \]

```
= [ [10, 20, 30], [40, 50, 60] ]
x = 0
i = 1
j print(x[i][j]) # 20
```

## Sigma

\[ \sum_{i=1}^{N} x_i \]

This symbol finds the sum of all elements in a vector for a given range. Both lower and upper limits are inclusive. In Python, it is equivalent to looping over a vector from index 0 to index N-1. Notice how we’re using the previously explained \(x_i\) symbol to get the value at index.

```
= [1, 2, 3, 4, 5]
x = 0
result = len(x)
N for i in range(N):
= result + x[i]
result print(result)
```

The above code can even be shortened using built-in functions in Python as

```
= [1, 2, 3, 4, 5]
x = sum(x) result
```

## Average

\[ \frac{1}{N}\sum_{i=1}^{N} x_i \]

Here we reuse the sigma notation and divide by the number of elements to get an average.

```
= [1, 2, 3, 4, 5]
x = 0
result = len(x)
N for i in range(N):
= result + x[i]
result = result / N
average print(average)
```

The above code can even be shortened in Python as

```
= [1, 2, 3, 4, 5]
x = sum(x) / len(x) result
```

## PI

\[ \prod_{i=1}^{N} x_i \]

This symbol finds the product of all elements in a vector for a given range. In Python, it is equivalent to looping over a vector from index 0 to index N-1 and multiplying them.

```
= [1, 2, 3, 4, 5]
x = 1
result = len(x)
N for i in range(N):
= result * x[i]
result print(result)
```

## Pipe

The pipe symbol can mean different things based on where it’s applied.

### Absolute Value

\[ \lVert x \rVert \]

\[ \lVert y \rVert \]

This symbol denotes the absolute value of a number i.e. without a sign.

```
= 10
x = -20
y abs(x) # 10
abs(y) # 20
```

### Norm of vector

\[ \lVert x \rVert \]

The norm is used to calculate the magnitude of a vector. In Python, this means squaring each element of an array, summing them and then taking the square root.

```
import math
= [1, 2, 3]
x 0]**2 + x[1]**2 + x[2]**2) math.sqrt(x[
```

## Belongs to

\[ 3\ \in\ X \]

This symbol checks if an element is part of a set. In Python, this would be equivalent to

```
= {1, 2, 3}
X 3 in X
```

## Function

\[ f: X \rightarrow Y \]

This denotes a function which takes a domain X and maps it to range Y. In Python, it’s equivalent to taking a pool of values X, doing some operation on it to calculate pool of values Y.

```
def f(X):
= ...
Y return Y
```

You will encounter the following symbols in place of X and Y. Here are what they mean:

\[ f: R \rightarrow R \]

`R`

means input and outputs are real numbers and can take any value (integer, float, irrational, rational). In Python, this is equivalent to any value except complex numbers.

```
import math
= 1
x = 2.5
y = math.pi z
```

You will also encounter symbols such as

\[ f: R^d \rightarrow R \]

\[R^d\] means d-dimensional vector of real numbers.

Let’s assume d = 2. In Python, an example can be a function that takes 2-D array and returns it’s sum. It will be mapping a \(R^d\) to \(R\)

```
= [1, 2]
X = sum
f = f(X) Y
```

## Tensors

### Transpose

\[ X^{\mathsf{T}} \]

This is basically exchanging the rows and columns. In Python, this would be equivalent to

```
import numpy as np
= [[1, 2, 3],
X 4, 5, 6]]
[ np.transpose(X)
```

Output would be a list with exchanged rows and columns.

```
[[1, 4],
[2, 5],
[3, 6]]
```

### Element wise multiplication

\[ z = x \odot y \]

It means multiplying the corresponding elements in two tensors. In Python, this would be equivalent to multiplying the corresponding elements in two lists.

```
import numpy as np
= [[1, 2],
x 3, 4]]
[= [[2, 2],
y 2, 2]]
[= np.multiply(x, y) z
```

Output is

```
[[2, 4]],
[[6, 8]]
```

### Dot Product

\[ xy \]

\[ x \cdot y \]

It gives the sum of the products of the corresponding entries of the two sequences of numbers.

```
= [1, 2, 3]
x = [4, 5, 6]
y = sum([i*j for i, j in zip(x, y)])
dot # 1*4 + 2*5 + 3*6
# 32
```

### Hat

\[ \hat{x} \]

The hat gives the unit vector. This means dividing each component in a vector by it’s length(norm).

```
import math
= [1, 2, 3]
x = math.sqrt(sum([e**2 for e in x]))
length = [e/length for e in x] x_hat
```

This makes the magnitude of the vector 1 and only keeps the direction.

```
import math
sum([e**2 for e in x_hat]))
math.sqrt(# 1.0
```

## Exclamation

\[ x! \]

This denotes the factorial of a number. It is the product of numbers starting from 1 to that number. In Python, it can be calculated as

```
= 5
x = 1
fact for i in range(x, 0, -1):
= fact * i
fact print(fact)
```

The same thing can also be calculated using built-in function.

```
import math
= 5
x math.factorial(x)
```

The output is

```
# 5*4*3*2*1
120
```