# Dot Product

The dot product is an operation to *find the similarity* of two vectors.

In general it is defined as&#x20;

$$
\vec{a}\bullet\vec{b}=|\vec{a}|\cdot|\vec{b}|\cdot\cos(\theta)
$$

The result is always a single number. Visually that equation means taking $$\vec{b}$$, squashing it onto $$\vec{a}$$ and then multiplying the length of the squashed vector with the length of $$\vec{a}$$.

![](https://3012238209-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3CiKZGX91xk3wTXNDkac%2Fuploads%2FOMffPDcBh5boVeVn9vAu%2Fdot%20product.gif?alt=media\&token=4ee7cf74-79ac-4cc1-b78f-6ed7ce7adbd0)

In code this would look like the following.

```java
Vector greenVector = new Vector(4,0,2);
Vector orangeVector = new Vector(1,0,2);
double dotProduct = greenVector.dot(orangeVector);
```

You may be able to tell already that the dot product conveys information about the similarity between two vectors. The result can be any number when used on any vector. This can be somewhat hindering because how may one judge the similiarity if it can be any number. For example the dot product is a very large number when the two vectors point into almost the same direction ($$\theta$$ is small) but have massively different lengths. The key is how we define "similarity". Most of the times the length of the vector is not relevant as we keep a length of 1 for most vectors when doing math around the origin. Therefore "similarity" for us essentially means we only care about the angle between two unit vectors.  This also simplifies the equation to

$$
\vec{a}\bullet\vec{b}=\cos(\theta)
$$

The cosinus is always between -1 and 1 and for that reason the dot product will also be in this range. Here are some quick look-ups (angle in degree):

| angle | dot product |                                                   |
| ----- | ----------- | ------------------------------------------------- |
| 0     | 1           | identical                                         |
| 90    | 0           | form a 90 degree angle (orthogonal to each other) |
| 180   | -1          | exactly opposite of each other                    |

![](https://3012238209-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3CiKZGX91xk3wTXNDkac%2Fuploads%2FnEcIyScJIlHFIa3Y4JY9%2FDotProduct.gif?alt=media\&token=e3992f26-ba48-4736-bb4b-561650d79a2c)
