The dot product is an algebraic operation which takes two equal-sized vectors and returns a single scalar (which is why it is sometimes referred to as the scalar product). In Euclidean geometry, the dot product between the Cartesian components of two vectors is often referred to as the inner product.

The dot product is represented by a dot operator:

It is defined as:

In Python, one way to calulate the dot product would be taking the sum of a list comprehension performing element-wise multiplication.

```
# Define x and y
x = [1, 3, -5]
y = [4, -2, -1]
```

```
def dot(x, y):
"""Dot product as sum of list comprehension doing element-wise multiplication"""
return sum(x_i*y_i for x_i, y_i in zip(x, y))
print("The dot product of x and y is", dot(x, y))
```

```
The dot product of x and y is 3
```

Python 3.5 onwards also has an explicit operator `@`

for the dot product (applies to numpy arrays NOT lists):

```
dot_product = np.array(x) @ np.array(y)
print("The dot product of x and y is", dot_product)
```

```
The dot product of x and y is 3
```

Alternatively, we can use the `np.dot()`

function.

```
dot_product = np.dot(x, y)
print("The dot product of x and y is", dot_product)
```

```
The dot product of x and y is 3
```

Keeping to the convention of having $\mathbf{x}$ and $\mathbf{y}$ as column vectors, the dot product is equal to the matrix multiplication $\mathbf{x}^T\mathbf{y}$.

Taking $\mathbf{x}, \mathbf{y} \in \mathbb{R}^{n \times 1}$ ($n$-rows, $1$-column therefore column-vectors) and analysing the dimensions during multiplication, we see that $(n \times 1)^T(n \times 1) = (1 \times n)(n \times 1) = (1 \times 1)$ as expected.

Using NumPy (and defining the column vectors as matrices):

```
x = np.expand_dims(np.array(x), axis=1)
y = np.expand_dims(np.array(y), axis=1)
print("x has shape {} and y has shape {}".format(x.shape, y.shape))
```

```
x has shape (3, 1) and y has shape (3, 1)
```

```
dot_product = np.matmul(x.T, y) # Note that we have transposed x
print("The dot product of x and y using matrix multiplication is", dot_product)
print("The result has shape {}".format(dot_product.shape))
```

```
The dot product of x and y using matrix multiplication is [[3]]
The result has shape (1, 1)
```

The result of the multiplication is a $1 \times 1$ matrix as expected. In practice, a $1 \times 1$ is commonly also referred to as a scalar.

### Geometric Definition

In Euclidean space, a Euclidean vector has both magnitude and direction. The magnitude of a vector $\mathbf{x}$ is denoted by $\left|\mathbf{x} \right|$. The dot product of two Euclidean vectors $\mathbf{x}$ and $\mathbf{y}$ is defined by:

Where $\theta$ is the angle between the two vectors.

This gives us an easy way to test for orthogonality between vectors. If $\mathbf{x}$ and $\mathbf{y}$ are orthogonal (the angle between vectors is $90°$) then since $\cos(90^{\circ })=0$, it implies that **the dot product of any two orthogonal vectors must be $0$**.

```
# Let's test this by defining two vectors we know are orthogonal
x = [1, 0, 0]
y = [0, 1, 0]
print("The dot product of x and y is", dot(x, y))
```

```
The dot product of x and y is 0
```

For more interesting properties, see: https://en.wikipedia.org/wiki/Dot_product

## Share this post

Twitter

Facebook

Reddit

LinkedIn

Google+

StumbleUpon

Pinterest

Email