Lecture 4 · Week 1· 17:52
Dot product, length and angle
Norm, angle, Cauchy–Schwarz and triangle inequalities.
Saved in this browser only. No account needed.
TranscriptThe full text of the lecture. Click any sentence to play the video from there.
Measuring length
Welcome back. So far, vectors have been things we add, scale and combine. But two of the most natural questions about arrows have no answer yet. How long is a vector? And what is the angle between two vectors? Today we answer both, with one new operation: the dot product. It turns two vectors into a single number, and from that number we can read off lengths, angles and right angles, in any number of dimensions. Let's start with length. Take the vector (three, four) in the plane. Its arrow is the long side of a right triangle, with a horizontal side of three and a vertical side of four.
By the theorem of Pythagoras, the square of the long side is three squared plus four squared. That is nine plus sixteen, or twenty five. So the length of the vector is the square root of twenty five, which is five. In three dimensions, we use Pythagoras twice. Take the vector (one, two, two). Its shadow on the floor is the vector (one, two, zero), whose length squared is one plus four, which is five. The arrow itself is the long side of a second right triangle, standing upright on that shadow, with a vertical side of two. So its length squared is five plus four, which is nine, and its length is three.
Look where that five came from: it is one squared plus two squared, the squares of the shadow's components. So the length squared of v is one squared, plus two squared, plus two squared: the squares of all three of its components. The pattern is clear. Square every component, add the squares, and take the square root. In R n we cannot draw the triangles, so we turn the pattern into a definition. The norm, or length, of a vector v in R n is the square root of the sum of the squares of its entries. We write it with double bars on each side of v.
A norm is never negative, and it is zero only for the zero vector, because a sum of squares is zero only when every entry is zero.
The dot product
The sum of squares inside the norm is a special case of a more general recipe, one that takes two vectors instead of one. That recipe is the dot product. The dot product of two vectors v and w in R n is the number we get by multiplying matching entries, and adding up the results. v one times w one, plus v two times w two, and so on, up to v n times w n. Notice that the answer is a number, a scalar, not a vector. That is why the dot product is also called the scalar product.
Let's compute one, one coordinate at a time. Take v equal to (two, one), and w equal to (one, three). In the first coordinate, the entry of v is two, and the entry of w is one. Two times one is two. In the second coordinate, the entry of v is one, and the entry of w is three. One times three is three.
Now we add the products. Two plus three is five. So v dot w equals five. Now take the dot product of a vector with itself. Each term is an entry times itself, so v dot v is the sum of the squares of the entries. That is exactly the norm, squared. So length comes from the dot product. The norm of v is the square root of v dot v. For our vector (two, one), v dot v is four plus one, which is five, so its length is the square root of five.
Length also gives us distance. The distance between the tips of v and w is the length of the arrow that joins them, and that arrow is the vector w minus v. In the first coordinate, one minus two is minus one. In the second coordinate, three minus one is two. So w minus v is (minus one, two), and its length is the square root of one plus four, which is root five.
Rules of the dot product
Before we use the dot product, we need the rules it obeys. There are four of them, and together they are all we will ever need. First, symmetry. v dot w equals w dot v. The order does not matter, because each product of entries is the same in either order. Second, the dot product distributes over addition. u dot the sum v plus w equals u dot v, plus u dot w.
Third, scalars come out. c times v, dotted with w, equals c times v dot w. And fourth, positivity. v dot v is never negative, and it is zero only when v is the zero vector. Let's prove the second rule, to see how such proofs go. Write out u dot the sum v plus w, using the definition. Each entry of v plus w is an entry of v plus the matching entry of w, so each term is an entry of u times such a sum.
Multiply out each term. u one times v one, plus u one times w one, and so on. Now collect all the first pieces into one sum, and all the second pieces into another. The first sum is u dot v, and the second is u dot w. That proves the rule. The other three rules are proved the same way, straight from the definition, and the book writes them out in full. Here is a first consequence. When we scale a vector by c, its length is scaled by the absolute value of c. The squared length of c v is c v dot c v. Both scalars come out, giving c squared times v dot v. And the square root of c squared is the absolute value of c.
So doubling a vector doubles its length, and minus one times a vector has the same length, just as the arrow picture told us in the first lecture. A vector of length one is called a unit vector. Any nonzero vector can be turned into one by dividing it by its own length. This is called normalising the vector. For example, the vector (three, four) has length five, so we divide each entry by five. In the first coordinate, three divided by five is three fifths. In the second coordinate, four divided by five is four fifths.
The result points in the same direction as before, and its length is exactly one. Its tip lies on the circle of radius one around the origin.
The angle between two vectors
Now for the geometry. What does the number v dot w tell us about the arrows v and w? To find out, let's look at the triangle they form. Draw v and w from the origin, and call the angle between them theta. The third side of the triangle runs from the tip of v to the tip of w. That side is the vector w minus v. Let's compute the squared length of that third side, using only the rules of the dot product. It is w minus v, dotted with itself.
Expanding with the rules, we get w dot w, minus two times v dot w, plus v dot v. In other words, the squared length of v, plus the squared length of w, minus two v dot w. But geometry gives a second formula for the same side, the law of cosines. The third side squared is the squared length of v, plus the squared length of w, minus two times the length of v, times the length of w, times the cosine of theta. The two formulas describe the same length, so they must agree. The first two terms are the same in both, so the last terms must be equal too. And that gives the key formula.
v dot w equals the length of v, times the length of w, times the cosine of theta. The dot product measures how much two vectors point the same way. That derivation packed a lot into a few steps. If any of it went by too quickly, feel free to pause here, or to go back and watch it again slowly. It is worth seeing exactly where this formula comes from, because the rest of the lecture is built on it. Let's use it on our example. v is (two, one) and w is (one, three). We found that v dot w is five, and that v has length root five. And w has length the square root of one plus nine, which is root ten.
So the cosine of theta is five, divided by root five times root ten. Root five times root ten is root fifty, which is five root two. So the cosine is one over root two, and the angle is forty five degrees. The sign of the dot product tells us what kind of angle we have. When v dot w is positive, the cosine is positive, and the angle is acute, less than ninety degrees. When it is negative, the angle is obtuse. And when the dot product is exactly zero, the angle is exactly ninety degrees. The vectors are perpendicular.
Orthogonality
That last case is so important that it gets its own name. Two vectors v and w in R n are called orthogonal when their dot product is zero. For example, take (two, one) and (minus one, two). In the first coordinate, two times minus one is minus two. In the second coordinate, one times two is two. And minus two plus two is zero. So these two vectors are orthogonal, and indeed their arrows meet at a right angle.
The zero vector is orthogonal to every vector, since its dot product with anything is zero. Orthogonal means dot product zero, even when one of the arrows has no direction at all. Orthogonality gives us the theorem of Pythagoras, now in every dimension. If v and w are orthogonal, then the squared length of v plus w equals the squared length of v, plus the squared length of w. The proof takes two lines. Expand the squared length of v plus w with the rules. We get v dot v, plus two times v dot w, plus w dot w.
Since v and w are orthogonal, the middle term is zero, and what remains is the squared length of v plus the squared length of w. And the argument also runs backwards. If the equation holds, the middle term must be zero. So in R n, the Pythagorean equation holds exactly for orthogonal vectors. Orthogonality runs through the rest of the course, in orthogonal projections, in orthonormal bases, and in the method of least squares.
The Cauchy-Schwarz inequality
We found the angle formula by drawing a triangle in the plane. But in R four, or R ten, there is no picture to draw. Can we still talk about angles there? Here is the idea. Turn the formula around, and make it the definition. For nonzero vectors, we define the angle between v and w as the angle whose cosine is v dot w, divided by the product of their lengths. But there is a catch. A cosine always lies between minus one and one. For this definition to make sense, the fraction must always lie in that range, in every dimension. That is the content of a famous theorem.
The Cauchy-Schwarz inequality. For all vectors v and w in R n, the absolute value of v dot w is at most the length of v times the length of w. And equality holds exactly when one of the vectors is a multiple of the other. Let's check it on our example. v dot w is five. The product of the lengths is root fifty, about seven point zero seven. And five is indeed at most seven point zero seven. The proof that comes next is the most demanding part of this lecture, and it may well feel confusing the first time through. That is completely normal. Feel free to pause at any step, or to watch it again slowly, until you can see exactly why each line follows from the one before.
Now the proof. If w is the zero vector, both sides are zero, and there is nothing to prove. So suppose w is not zero, and consider the vector v minus t times w, where t can be any real number. Soon it will be clear why we chose this particular vector. As t changes, the tip of v minus t w slides along a line through the tip of v, parallel to w. Its length changes, but whatever t is, its squared length can never be negative. Let's expand that squared length with the rules. v minus t w, dotted with itself, is v dot v, minus two t times v dot w, plus t squared times w dot w.
Grouping the terms by powers of t, this is the squared length of w, times t squared, minus two v dot w times t, plus the squared length of v. Let's focus on this expression, and call it q of t. This is a quadratic in t, and it is never negative, because it is a squared length. So its graph, a parabola, never dips below the axis. That means the quadratic has at most one real root, so its discriminant cannot be positive. And thanks to our choice of v minus t w, that discriminant is built from v and w themselves, so it tells us something about them. The discriminant is b squared minus four a c. Here, that is four times v dot w squared, minus four times the squared length of v, times the squared length of w. And this is at most zero.
Dividing by four, v dot w squared is at most the squared length of v times the squared length of w. Taking square roots of both sides gives the inequality, and that completes the proof. And when is there equality? Exactly when the discriminant is zero, so the parabola just touches the axis. Then for one value of t, v minus t w has length zero, which says that v equals t times w, a multiple of w. So the definition of angle is safe in every dimension. Here is an example in R four. Take the diagonal vector (one, one, one, one), and the first axis vector (one, zero, zero, zero).
We multiply matching entries, one coordinate at a time. One times one is one. Then one times zero, three times over, is zero each time. So the dot product is one. The lengths are the square root of four, which is two, and the square root of one, which is one. So the cosine of the angle is one half, and the angle is sixty degrees. In four dimensions, the long diagonal of a cube makes an angle of sixty degrees with each edge. Angles in high dimensions are used every day. A search engine can record a document as a long vector of word counts, and judge two documents to be similar when the angle between their vectors is small. This measure is called cosine similarity. Cauchy-Schwarz
The triangle inequality
has a consequence that every traveller knows. Going straight is never longer than taking a detour. This is the triangle inequality. For all vectors v and w in R n, the length of v plus w is at most the length of v, plus the length of w. In the picture, v plus w is one side of a triangle, and v and w are the other two sides. One side of a triangle is never longer than the other two together.
Here is the proof. Both sides are lengths, so they are not negative, and it is enough to compare their squares. Expanding with the rules, the squared length of v plus w is the squared length of v, plus two v dot w, plus the squared length of w. Now use Cauchy-Schwarz on the middle term. v dot w is at most its absolute value, which is at most the length of v times the length of w. So the whole expression is at most the squared length of v, plus two times the length of v times the length of w, plus the squared length of w. And that is exactly the square of the length of v plus the length of w.
Taking square roots finishes the proof. Equality holds exactly when one of the vectors is a non-negative multiple of the other. When both are nonzero, they point in the same direction, and the triangle is flattened into a straight line. For our example, we add v and w one coordinate at a time. In the first coordinate, two plus one is three. In the second coordinate, one plus three is four. So v plus w is (three, four), with length five. The lengths of v and w add up to root five plus root ten, which is about five point four. And five is indeed less than five point four.
For points, the triangle inequality says that the distance from one point to another is at most the distance along any detour through a third point. A detour never makes the trip shorter.
Summary
Let's summarise. The dot product of two vectors multiplies matching entries and adds the results. It is symmetric, it distributes over addition, scalars come out of it, and it is positive on every nonzero vector. The norm, or length, of a vector is the square root of its dot product with itself, and the distance between two vectors is the length of their difference. The dot product equals the product of the lengths times the cosine of the angle between the vectors. Its sign tells us whether the angle is acute, right or obtuse, and it is zero exactly for orthogonal vectors.
The Cauchy-Schwarz inequality makes angles meaningful in every dimension, and it gives us the triangle inequality. In week twelve we will see that all of this works for other inner products too, so that even polynomials and functions get lengths and angles. Before the next lecture, read section one point four of the course book, where every proof is written out, and try the dot product problems on sheet one. Next time, we use the dot product to describe lines and planes, and we meet a product that exists only in three dimensions, the cross product. See you there.
In this lecture
Materials
Check yourself
3 questions, from warm-up to stretch
Work each one out on paper, type your answer and press Check. Stuck? Open a hint; the worked solution is there when you need it.
- Question 1 of 3 Warm-up
Let and . Compute . Are and orthogonal?
Hint 1 of 2
Multiply matching entries and add: .
Hint 2 of 2
Two vectors are orthogonal exactly when their dot product is .
Worked solution
- .
- The dot product is , so and are orthogonal.
- Question 2 of 3 Core
Find the length of .
Hint 1 of 2
: square each entry, add, then take the square root.
Hint 2 of 2
.
Worked solution
- .
- .
- Question 3 of 3 Stretch
Find the angle between and , in degrees.
Hint 1 of 2
.
Hint 2 of 2
, and , so . Which angle has that cosine?
Worked solution
- , and .
- .
- The angle between and with that cosine is .
How did it go?