The Dot Product

How the angle between two vectors hides in their components.

1. The Question

The components of a vector tell us everything about it: its length, and which way it points. So given two vectors $\bar{a}$ and $\bar{b}$, their components should also tell us how the two vectors sit relative to each other. In particular, can we find the angle between them?

To measure that angle, draw the two vectors from the same point. The angle between $\bar{a}$ and $\bar{b}$ is the angle $\alpha$ at that point, from 0° when they point the same way up to 180° when they point in opposite directions.

This page answers the question. Along the way, one particular combination of the components turns up, and it is so useful that it gets its own name: the dot product.

2. A Triangle from Two Vectors

With $\bar{a}$ and $\bar{b}$ drawn from a point $C$, their heads $B$ and $A$ can always be joined by a third vector. As the Adding and Scaling page showed, the vector from the head of $\bar{a}$ to the head of $\bar{b}$ is their difference:

$$\bar{c} = \bar{b} - \bar{a}$$
Vectors a and b drawn from the same point C, a up to B and b along to A, with the angle alpha between them. An orange arrow c = b − a runs from B to A, closing the triangle.
Fig 1: $\bar{a}$, $\bar{b}$ and $\bar{c} = \bar{b} - \bar{a}$ form a triangle, with the angle $\alpha$ at $C$.

The angle between the two vectors is now an angle of a triangle, with sides $|\bar{a}|$, $|\bar{b}|$ and $|\bar{c}|$. And for triangles there is already a rule that connects an angle to the lengths of the sides: the law of cosines.

3. The Law of Cosines

Trigonometry has a rule for exactly this situation, the law of cosines. In a triangle where two sides of lengths $a$ and $b$ meet at an angle $\theta$, the third side $c$ satisfies

$$c^2 = a^2 + b^2 - 2ab\cos\theta$$

That page derives it from the definitions of sine and cosine. In our triangle, the two sides are $|\bar{a}|$ and $|\bar{b}|$, the angle between them is $\alpha$, and the third side is $|\bar{c}|$:

$$|\bar{c}|^2 = |\bar{a}|^2 + |\bar{b}|^2 - 2|\bar{a}||\bar{b}|\cos\alpha$$

Now solve for $\cos\alpha$. Move $2|\bar{a}||\bar{b}|\cos\alpha$ to the left side and $|\bar{c}|^2$ to the right, then divide by $2|\bar{a}||\bar{b}|$:

$$\cos\alpha = \frac{|\bar{a}|^2 + |\bar{b}|^2 - |\bar{c}|^2}{2|\bar{a}||\bar{b}|}$$

This already finds the angle, from the lengths of the three sides.

4. From Lengths to Components

Every length in that fraction can be worked out from components, using the formula from the Magnitude page. So let us write the top of the fraction in components and see what is left. Let $\bar{a} = (a_1, a_2, a_3)$ and $\bar{b} = (b_1, b_2, b_3)$. Subtracting matching components,

$$\bar{c} = (b_1 - a_1,\ b_2 - a_2,\ b_3 - a_3)$$

The square of each length is the sum of the squares of its components:

$$\begin{align} |\bar{a}|^2 &= a_1^2 + a_2^2 + a_3^2 \\ |\bar{b}|^2 &= b_1^2 + b_2^2 + b_3^2 \\ |\bar{c}|^2 &= (b_1 - a_1)^2 + (b_2 - a_2)^2 + (b_3 - a_3)^2 \end{align}$$

In $|\bar{a}|^2 + |\bar{b}|^2 - |\bar{c}|^2$, gather the terms that use the first components. Expanding $(b_1 - a_1)^2$ with $(x - y)^2 = (x - y)(x - y) = x^2 - 2xy + y^2$ gives $b_1^2 - 2a_1 b_1 + a_1^2$, and the minus sign in front of it then changes the sign of each of its terms:

$$a_1^2 + b_1^2 - (b_1 - a_1)^2 = a_1^2 + b_1^2 - b_1^2 + 2a_1 b_1 - a_1^2 = 2a_1 b_1$$

The squares cancel, and only the product of the two first components is left. The second and third components work the same way, so

$$|\bar{a}|^2 + |\bar{b}|^2 - |\bar{c}|^2 = 2(a_1 b_1 + a_2 b_2 + a_3 b_3)$$

Putting this into the fraction, the 2 on top cancels the 2 below:

$$\cos\alpha = \frac{a_1 b_1 + a_2 b_2 + a_3 b_3}{|\bar{a}||\bar{b}|}$$

5. The Dot Product

This answers the question. The lengths on the bottom come from the components, and so does the top: multiply matching components and add the results. That combination is exactly what the angle depends on, so it is worth a name of its own.

The dot product of $\bar{a} = (a_1, a_2, a_3)$ and $\bar{b} = (b_1, b_2, b_3)$ is

$$\bar{a} \cdot \bar{b} = a_1 b_1 + a_2 b_2 + a_3 b_3$$

It is read “a dot b”. In two dimensions, there is no third term.

With this name, the result of the last section reads

$$\cos\alpha = \frac{\bar{a} \cdot \bar{b}}{|\bar{a}||\bar{b}|}$$

or, multiplying both sides by $|\bar{a}||\bar{b}|$,

$$\bar{a} \cdot \bar{b} = |\bar{a}||\bar{b}|\cos\alpha$$

The dot product of two vectors is a number, not a vector, so it is also called the scalar product. Its definition was not chosen at random: it is the one combination of components that the angle between the vectors requires.

One special case is worth noting. The dot product of a vector with itself is

$$\bar{a} \cdot \bar{a} = a_1^2 + a_2^2 + a_3^2 = |\bar{a}|^2$$

the square of its length.

6. Finding the Angle

To find the angle between two vectors, neither of which is the zero vector, work out their dot product and their lengths:

$$\cos\alpha = \frac{\bar{a} \cdot \bar{b}}{|\bar{a}||\bar{b}|}$$

The lengths are positive, so the sign of $\cos\alpha$ is the sign of $\bar{a} \cdot \bar{b}$. The dot product alone tells us roughly how the vectors are oriented:

  • If $\bar{a} \cdot \bar{b} > 0$, the angle is less than 90°: the vectors point broadly the same way.
  • If $\bar{a} \cdot \bar{b} = 0$, the angle is exactly 90°: the vectors are perpendicular.
  • If $\bar{a} \cdot \bar{b} < 0$, the angle is more than 90°: the vectors point broadly in opposite directions.

The second and third cases use $\cos 90^\circ = 0$ and a negative cosine for angles over 90°, which come from the same definition of cosine for any angle that the unit circle page will give.

Example 1

For $\bar{a} = (1, 2, 2)$ and $\bar{b} = (2, 1, -2)$:

$$\bar{a} \cdot \bar{b} = (1)(2) + (2)(1) + (2)(-2) = 2 + 2 - 4 = 0$$

so $\bar{a}$ and $\bar{b}$ are perpendicular, without our ever drawing them.

Example 2

For $\bar{a} = (1, 0)$ and $\bar{b} = (1, 1)$:

$$\bar{a} \cdot \bar{b} = (1)(1) + (0)(1) = 1$$

Their lengths are $|\bar{a}| = 1$ and $|\bar{b}| = \sqrt{1^2 + 1^2} = \sqrt{2}$, so

$$\cos\alpha = \frac{1}{1 \cdot \sqrt{2}} = \frac{1}{\sqrt{2}}$$

A right triangle whose two shorter sides are both 1 has a hypotenuse of $\sqrt{2}$, and its two other angles are equal, so each is 45°. In it, $\cos 45^\circ = \dfrac{1}{\sqrt{2}}$. So the angle between $\bar{a}$ and $\bar{b}$ is $\alpha = 45^\circ$.

7. N Dimensions

The dot product follows the same pattern in any number of dimensions: multiply matching components and add.

For $\bar{a} = (a_1, \dots, a_n)$ and $\bar{b} = (b_1, \dots, b_n)$,

$$\bar{a} \cdot \bar{b} = a_1 b_1 + a_2 b_2 + \cdots + a_n b_n = \sum_{i=1}^{n} a_i b_i$$

In four or more dimensions we cannot draw the triangle, so, just as with magnitude, we turn the result around and use it to define the angle between two vectors:

$$\cos\alpha = \frac{\bar{a} \cdot \bar{b}}{|\bar{a}||\bar{b}|}$$

This agrees with the angle we can see in two and three dimensions, and it gives a meaning to “perpendicular” in any number of dimensions: two vectors are perpendicular when their dot product is 0.