How much do two arrows point the same way? Watch one arrow’s shadow on the other grow, vanish and turn negative.
The dot product is a shadow
v = (3, 1) stays put; w turns on a circle and casts its shadow on the line of v.
w lies along v: its shadow is all of w. v · w = 7.071: the length of v, 3.162, times the shadow, 2.236. That is the largest v · w can be.
Describe this picture
Two panels. In the first, a grid with a fixed arrow , the dashed line through it, and a second arrow of fixed length on a dotted circle. A dashed drop line from the tip of meets the line of at a right angle, marked, and the shadow of is a band along that line. In the second, beside it (below it on a phone), a plot of against the angle between the arrows, traced as turns: a cosine curve. The arrow starts along , turns to , 45° away, and rests; turns on to the right angle and rests; then turns until it points opposite to . While turns from along to the right angle, the shadow shrinks to nothing and the product falls to 0; beyond the right angle the shadow points backwards and the product is negative. Readouts give , the shadow’s signed length and the angle. One control, once the clip has finished: drag the tip of round its circle, or turn it with the arrow keys a degree at a time; it snaps onto the right angle when it comes within 2° of it.
Length
Before I compare two arrows, I need the length of one. The arrow goes 3 across and 1 up. Those two moves are the short sides of a right triangle, and the arrow is its long side. Pythagoras gives the length:
The length of a vector is written with double bars, . In two dimensions, . In three, use Pythagoras twice: the arrow has length . In dimensions you keep going, one square per component:
An arrow of length 1 is a unit vector. To make one that points the same way as , divide by its length: . Its length is 1, and it keeps only the direction.
The dot product
Now take two vectors and multiply them component by component, then add the products. That number is the dot product:
For and it is . The answer is a number, not a vector.
Dot a vector with itself and you get the sum of its squares, which is its length squared: . So length is the dot product’s first use.
The recipe hides what the number means. Here is the picture. Let be the angle between the two arrows. Then
Read the right-hand side in two parts. is the shadow of on the line of : drop a line from the tip of straight down onto that line, at a right angle, and measure from the origin. The dot product is that shadow times the length of .
Why the two formulas agree
The two arrows and the arrow between their tips form a triangle. The law of cosines, from school geometry, gives its third side from the other two and the angle between them:
Now work out the same length from the components. Length squared is a vector dotted with itself, and the dot product multiplies out like ordinary brackets, one component at a time:
The two right-hand sides are equal. Cancel and from both, divide by , and the sum of products equals . Any two arrows lie in one flat plane, even in many dimensions, so the triangle argument works in every dimension.
Angles and right angles
Go back to the picture at the top of the page and drag round slowly. Watch the sign of the product.
- When points along , the angle is 0°, , and the product is as large as it can be.
- As turns away, the shadow shrinks and so does the product.
- When reaches 90° the shadow is a single point and the product is exactly 0.
- Past 90° the shadow points backwards along the line, and the product is negative.
So the sign tells you the rough direction. Positive: the arrows lean the same way, less than 90° apart. Zero: they are at a right angle. Negative: they lean apart, more than 90°.
Turn the formula round and you can find the angle from the components alone:
Two vectors with a dot product of 0 are at a right angle; they are orthogonal, or perpendicular. For example . This test works in any number of dimensions, where you can’t draw a right angle: , so these two arrows in space are orthogonal.
A zero dot product does not mean one of the vectors is zero. Both and have length . The product is zero because the arrows are at a right angle.
Two recordings
The same test compares things you can’t draw as arrows at all. Take two short frames of a recording, four samples each. Each frame is a vector in , and the cosine of the angle between them says how alike their shapes are.
Two recordings
Two frames of four samples, compared by the angle between them.
Two recordings of four samples each, (1, 2, 1, 0) and (0, 1, 2, 1): two vectors in four dimensions.
Describe this picture
Two frames of four samples each, and , drawn as two rows of bars. Beside them (below them on a phone) the same two frames are drawn as two arrows, each long, in the flat plane that contains them both. The angle between them is marked, θ = 48.2° (0.8411 rad), and then the shadow of the second on the first, long, with its right angle. A readout shows . There is no control.
Notice that the two arrows in that picture are drawn in their own plane, not along the axes. Four numbers each still make two arrows, and two arrows always lie in one plane.
Two limits
The picture gives two facts that hold for every pair of vectors.
The dot product is never bigger than the two lengths multiplied. A cosine lies between and 1, so
This is the Cauchy–Schwarz inequality. The two sides are equal only when the arrows lie on one line. In the clip at the top of the page the product never climbs above , the height the cosine curve reaches when lies along .
The straight path is never longer than the bent one. The sum is the third side of the tip-to-tail triangle from Vectors, so
This is the triangle inequality. It follows from the first one. Square the left side:
By Cauchy–Schwarz the middle term is at most , and is a perfect square. So
and taking the square root of both sides gives the triangle inequality.
Worked example
Take and .
- Dot product. .
- Lengths. and .
- Angle. , so the angle is 45°, which is rad. Check it another way: points 18.4° above the horizontal and points 63.4° above it, and 63.4° − 18.4° = 45°.
- Cauchy–Schwarz. .
- Triangle. has length , and .
- Unit vector. .
- Right angles. ; in space, , and .
Predict before you drag: at which angle is the shadow longest? At 0°, when lies along . And if , and is 60°, whose cosine is , the product is .
Where you’ll meet this
Work done by a force. Push a box with a force of newtons while it slides metres along the floor. Only the part of the force along the motion does work: joules. The sideways part, 2 newtons, does none.
How alike are two recordings? The frames and have dot product , and each has length . Their cosine is , an angle of 48.2°. This number, the cosine similarity, is how text search tools compare documents and how audio tools compare sound frames: 1 means the same shape, 0 means no shared shape (the frames are orthogonal).
Signal processing runs on dot products. Correlation slides one signal along another and takes a dot product at each shift. Each value of The DFT is a dot product of the signal with a probe wave. Matched filters and detection uses Cauchy–Schwarz to prove that the best detector for a known pulse is a dot product with that pulse.
Where this leads · correlation as a cosine
In statistics, the correlation of two sets of measurements is the cosine between them as vectors, after subtracting each set’s average. It is 1 when one set rises exactly with the other and 0 when the two are unrelated in this sense.
Reference card
| Quantity | Formula | Notes |
|---|---|---|
| Length | Pythagoras, one square per component | |
| Unit vector | length 1, same direction | |
| Dot product | a number; | |
| Shadow form | shadow of on , times | |
| Angle | positive under 90°, negative over | |
| Orthogonal | a right angle, in any number of dimensions | |
| Cauchy–Schwarz | equal only when the arrows share a line | |
| Triangle inequality | the straight path is never longer |