# Dot Product Deep(ish) Dive

The dot product is helpful in finding the distance of a point to a line. The dot product, as we mentioned here, is the the sum of the element-wise products of the vector components. Given two vectors $$\mathtt{v}$$ and $$\mathtt{w}$$, their dot product is $\begin{bmatrix}\mathtt{v_1}\\\mathtt{v_2}\end{bmatrix} \cdot \begin{bmatrix}\mathtt{w_1}\\\mathtt{w_2}\end{bmatrix}\mathtt{= v_1w_1 + v_2w_2}$

The result of this computation is not another vector, but just a number, a scalar quantity. And, given that the dot product of two perpendicular vectors is 0, it would be nice if the dot product were related to cosine in some way, since the cosine of 90° is also 0. So let’s take a look at some vector pairs and their dot products and think about any patterns we see. $$\mathtt{v \cdot w=-4}$$     $$\mathtt{θ=180^{\circ}}$$     $$\mathtt{\textrm{cos}(θ)=-1}$$ $$\mathtt{v \cdot w=-2}$$     $$\mathtt{θ=120^{\circ}}$$     $$\mathtt{\textrm{cos}(θ)=-\frac{1}{2}}$$ $$\mathtt{v \cdot w=4}$$     $$\mathtt{θ=45^{\circ}}$$     $$\mathtt{\textrm{cos}(θ)=\frac{\sqrt{2}}{2}}$$ $$\mathtt{v \cdot w=2}$$     $$\mathtt{θ=60^{\circ}}$$     $$\mathtt{\textrm{cos}(θ)=\frac{1}{2}}$$

Well, so, the dot products have the same signs as the cosines. That’s a start. And in all but one case shown, we can divide the dot product by 4 to get the cosine. What makes the 45° case different?

Each of the vectors shown, with the exception of the vector (2, 2) has a length, a magnitude, of 2. To determine the magnitude, or length, of a vector, you treat the components of the vector as the legs of a right triangle and the vector itself as the hypotenuse. So, $|\begin{bmatrix}\mathtt{-1}\\\mathtt{\sqrt{3}}\end{bmatrix}|=\sqrt{(-1)^2+(\sqrt{3})^2}=2$

But the length of (2, 2) is $$\mathtt{\sqrt{8}}$$. If we were to give that vector a length of 2, without changing the angle between v and w, then the vector would become ($$\mathtt{\sqrt{2}, \sqrt{2}}$$). And, lo, the dot product would become $$\mathtt{2\sqrt{2}}$$, which, when divided by 4, would yield the cosine.

The 4 that we divide by isn’t random. It’s the product of the lengths of the vectors. If we leave the 45° angled vectors alone, the product of their lengths is $$\mathtt{2\sqrt{8}}$$. Dividing 4 by this product does indeed yield the correct cosine. So, we have an initial conjecture that the dot product of two vectors v and w relates to cosine like this: $\mathtt{\frac{v \cdot w}{|v||w|} = cos(θ)}$

Perpendicular vectors will still have a dot product of 0 with this formula, so that’s good. And we can scale the vectors however we want and the cosine should remain the same—as it should be—though it may take a little manipulation to see that that’s true. But we are still left with the puzzle of proving this conjecture, more or less, or at least demonstrating to our satisfaction that the result is general. Although the derivation doesn’t go beyond the Pythagorean Theorem, really, it gets a little symbol heavy, so let’s start with something simpler. We can write the cosine of θ at the right as $\mathtt{\textrm{cos}(θ)=\frac{|w|}{|v|}}$ If we think of w here as truly horizontal, its length is simply $$\mathtt{v_1}$$, the length of the horizontal component of v. Combining this fact with the length of v, we can rewrite the cosine equation above as $\mathtt{\textrm{cos}(θ)=\frac{v_1}{\sqrt{v_{1}^2+v_{2}^2}}}$

Since w is horizontal (has a second component of 0), the dot product $$\mathtt{v \cdot w}$$ becomes simply $$\mathtt{v_{1}^2}$$. Dividing this by the product of the lengths of the vectors v and w (where the length of w is just $$\mathtt{v_1}$$), we get this equation for cosine: $\mathtt{\,\,\,\,\,\textrm{cos}(θ)=\frac{v_{1}^2}{(v_1)(\sqrt{v_{1}^2+v_{2}^2})}}$ And that’s clearly equal to the above. So, while it is by no means definitive, we can have a little more confidence at this point that we have the right equation for cosine using the dot product. We can get more formal and sure about it later. Next time we’ll look at how it can help us determine the distance from a point to a line. 