# Cosine Formula for Dot Product

## Theorem

Let $\mathbf v,\mathbf w$ be two non-zero vectors in $\R^n$.

The dot product of $\mathbf v$ and $\mathbf w$ can be calculated by:

$\mathbf v \cdot \mathbf w = \norm {\mathbf v} \norm {\mathbf w} \cos \theta$

where:

$\norm {\, \cdot \,}$ denotes vector length and
$\theta$ is the angle between $\mathbf v$ and $\mathbf w$.

## Proof

There are two cases, the first where the two vectors are not scalar multiples of each other, and the second where they are.

### Case 1

Let the two vectors $\mathbf v$ and $\mathbf w$ not be scalar multiples of each other.

Then by the definition of angle between vectors, we have $\theta$ defined as in the triangle as shown above.

(Note that from Angle Between Non-Zero Vectors Always Defined, such a triangle is guaranteed to exist).

By the Law of Cosines:

$\norm {\mathbf v - \mathbf w}^2 = \norm {\mathbf w}^2 + \norm {\mathbf v}^2 - 2 \norm {\mathbf v} \norm {\mathbf w} \cos \theta$

Now, observe that:

 $\displaystyle \norm {\mathbf v - \mathbf w}^2$ $=$ $\displaystyle \paren {\mathbf v - \mathbf w}\ \cdot \paren {\mathbf v - \mathbf w}$ $\quad$ Dot Product of Vector with Itself $\quad$ $\displaystyle$ $=$ $\displaystyle \mathbf v \cdot \mathbf v - 2 \paren {\mathbf v \cdot \mathbf w} + \mathbf w \cdot \mathbf w$ $\quad$ Properties of Dot Product $\quad$ $\displaystyle$ $=$ $\displaystyle \norm {\mathbf v}^2 + \norm {\mathbf w}^2 - 2 \paren {\mathbf v \cdot \mathbf w}$ $\quad$ Dot Product of Vector with Itself $\quad$

Equating these two expressions for $\norm {\mathbf v - \mathbf w}^2$ gives:

 $\displaystyle \norm {\mathbf w}^2 + \norm {\mathbf v}^2 - 2 \norm {\mathbf v} \norm {\mathbf w} \cos \theta$ $=$ $\displaystyle \norm {\mathbf v}^2 + \norm {\mathbf w}^2 - 2 \paren {\mathbf v \cdot \mathbf w}$ $\quad$ $\quad$ $\displaystyle -2 \paren {\norm {\mathbf v} \norm {\mathbf w} \cos \theta}$ $=$ $\displaystyle -2 \paren {\mathbf v \cdot \mathbf w}$ $\quad$ $\quad$ $\displaystyle \norm {\mathbf v} \norm {\mathbf w} \cos \theta$ $=$ $\displaystyle \mathbf v \cdot \mathbf w$ $\quad$ $\quad$

which is exactly the desired result.

$\Box$

### Case 2

Let $\mathbf v = \paren {v_1, v_2, \ldots, v_n}$ and $\mathbf w = \paren {w_1, w_2, \ldots, w_n}$.

Without loss of generality, let $\mathbf v = c \mathbf w$, where $c$ is some scalar.

If $c > 0$, then by the definition of angle between vectors:

$\theta = 0 \implies \cos \theta = 1$

If $c < 0$, then by the definition of angle between vectors:

$\theta = \pi \implies \cos \theta = -1$

(Note that $c$ cannot be $0$ because we have stipulated $\mathbf v$ and $\mathbf w$ to be non-zero).

Then:

 $\displaystyle \mathbf v \cdot \mathbf w$ $=$ $\displaystyle \paren {c \mathbf w} \cdot \mathbf w$ $\quad$ $\quad$ $\displaystyle$ $=$ $\displaystyle c \paren {\mathbf w \cdot \mathbf w}$ $\quad$ Properties of Dot Product $\quad$ $\displaystyle$ $=$ $\displaystyle c \paren {\norm {\mathbf w}^2}$ $\quad$ Dot Product of Vector with Itself $\quad$ $\displaystyle$ $=$ $\displaystyle \paren {c \norm {\mathbf w} } \norm {\mathbf w}$ $\quad$ $\quad$ $\displaystyle$ $=$ $\displaystyle c \sqrt {\sum_{i \mathop = 1}^n w_i^2} \norm {\mathbf w}$ $\quad$ Definition of Vector Length in $\R^n$ $\quad$ $\displaystyle$ $=$ $\displaystyle \sgn \paren c \sqrt{c^2 \sum_{i \mathop = 1}^n w_i^2} \norm {\mathbf w}$ $\quad$ where $\sgn \paren c$ is the signum function, that is the algebraic sign of $c$ $\quad$ $\displaystyle$ $=$ $\displaystyle \sgn \paren c \sqrt {\sum_{i \mathop = 1}^n \paren {c w_i}^2} \norm {\mathbf w}$ $\quad$ $\quad$ $\displaystyle$ $=$ $\displaystyle \sgn \paren c \sqrt {\sum_{i \mathop = 1}^n v_i^2} \norm {\mathbf w}$ $\quad$ by hypothesis $\quad$ $\displaystyle$ $=$ $\displaystyle \sgn \paren c \norm {\mathbf v} \norm {\mathbf w}$ $\quad$ Definition of $\norm {\mathbf u}$ $\quad$ $\displaystyle$ $=$ $\displaystyle \norm {\mathbf v} \norm {\mathbf w} \cos \theta$ $\quad$ $\sgn \paren c = \cos \theta$ $\quad$

$\blacksquare$