Skip to content

Length and the dot product

Measure an arrow’s length, see the dot product as a shadow, and use it to find angles and right angles in any number of dimensions.

Helps to know firstVectors

Helps to know first
This topic

First, the picture

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.

v · w
7.071
shadow of w
2.236
angle
0°
0.00 / 12.10 s
Describe this picture

Two panels. In the first, a grid with a fixed arrow v=(3,1)\mathbf{v}=(3,1), the dashed line through it, and a second arrow w\mathbf{w} of fixed length 5\sqrt5 on a dotted circle. A dashed drop line from the tip of w\mathbf{w} meets the line of v\mathbf{v} at a right angle, marked, and the shadow of w\mathbf{w} is a band along that line. In the second, beside it (below it on a phone), a plot of v⋅w\mathbf{v}\cdot\mathbf{w} against the angle between the arrows, traced as w\mathbf{w} turns: a cosine curve. The arrow w\mathbf{w} starts along v\mathbf{v}, turns to (1,2)(1,2), 45° away, and rests; turns on to the right angle and rests; then turns until it points opposite to v\mathbf{v}. While w\mathbf{w} turns from along v\mathbf{v} 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 v⋅w\mathbf{v}\cdot\mathbf{w}, the shadow’s signed length and the angle. One control, once the clip has finished: drag the tip of w\mathbf{w} 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 v=(3,1)\mathbf{v}=(3,1) 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:

32+12=10=3.162.\sqrt{3^2+1^2}=\sqrt{10}=3.162.

The length of a vector is written with double bars, ∥v∥\lVert\mathbf{v}\rVert. In two dimensions, ∥v∥=v12+v22\lVert\mathbf{v}\rVert=\sqrt{v_1^2+v_2^2}. In three, use Pythagoras twice: the arrow (1,2,2)(1,2,2) has length 1+4+4=3\sqrt{1+4+4}=3. In nn dimensions you keep going, one square per component:

∥v∥=v12+v22+⋯+vn2.\lVert\mathbf{v}\rVert=\sqrt{v_1^2+v_2^2+\dots+v_n^2}.

An arrow of length 1 is a unit vector. To make one that points the same way as v\mathbf{v}, divide v\mathbf{v} by its length: v/∥v∥=(3,1)/3.162=(0.949,  0.316)\mathbf{v}/\lVert\mathbf{v}\rVert=(3,1)/3.162=(0.949,\;0.316). 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:

v⋅w=v1w1+v2w2+⋯+vnwn=∑q=1nvqwq.\begin{aligned} \mathbf{v}\cdot\mathbf{w}&=v_1w_1+v_2w_2+\dots+v_nw_n\\ &=\sum_{q=1}^{n}v_qw_q. \end{aligned}

For v=(3,1)\mathbf{v}=(3,1) and w=(1,2)\mathbf{w}=(1,2) it is 3⋅1+1⋅2=53\cdot1+1\cdot2=5. 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: v⋅v=∥v∥2\mathbf{v}\cdot\mathbf{v}=\lVert\mathbf{v}\rVert^2. So length is the dot product’s first use.

The recipe hides what the number means. Here is the picture. Let θ\theta be the angle between the two arrows. Then

v⋅w=∥v∥ ∥w∥cos⁡θ.\mathbf{v}\cdot\mathbf{w}=\lVert\mathbf{v}\rVert\,\lVert\mathbf{w}\rVert\cos\theta.

Read the right-hand side in two parts. ∥w∥cos⁡θ\lVert\mathbf{w}\rVert\cos\theta is the shadow of w\mathbf{w} on the line of v\mathbf{v}: drop a line from the tip of w\mathbf{w} straight down onto that line, at a right angle, and measure from the origin. The dot product is that shadow times the length of v\mathbf{v}.

Why the two formulas agree

The two arrows and the arrow w−v\mathbf{w}-\mathbf{v} 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:

∥w−v∥2=∥v∥2+∥w∥2−2 ∥v∥ ∥w∥cos⁡θ.\begin{aligned} \lVert\mathbf{w}-\mathbf{v}\rVert^2&=\lVert\mathbf{v}\rVert^2+\lVert\mathbf{w}\rVert^2\\ &\quad-2\,\lVert\mathbf{v}\rVert\,\lVert\mathbf{w}\rVert\cos\theta. \end{aligned}

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:

∥w−v∥2=(w−v)⋅(w−v)=∥w∥2−2 v⋅w+∥v∥2.\begin{aligned} \lVert\mathbf{w}-\mathbf{v}\rVert^2&=(\mathbf{w}-\mathbf{v})\cdot(\mathbf{w}-\mathbf{v})\\ &=\lVert\mathbf{w}\rVert^2-2\,\mathbf{v}\cdot\mathbf{w}+\lVert\mathbf{v}\rVert^2. \end{aligned}

The two right-hand sides are equal. Cancel ∥v∥2\lVert\mathbf{v}\rVert^2 and ∥w∥2\lVert\mathbf{w}\rVert^2 from both, divide by −2-2, and the sum of products equals ∥v∥ ∥w∥cos⁡θ\lVert\mathbf{v}\rVert\,\lVert\mathbf{w}\rVert\cos\theta. 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 w\mathbf{w} round slowly. Watch the sign of the product.

  • When w\mathbf{w} points along v\mathbf{v}, the angle θ\theta is 0°, cos⁡θ=1\cos\theta=1, and the product is as large as it can be.
  • As w\mathbf{w} turns away, the shadow shrinks and so does the product.
  • When θ\theta 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:

cos⁡θ=v⋅w∥v∥ ∥w∥.\cos\theta=\frac{\mathbf{v}\cdot\mathbf{w}}{\lVert\mathbf{v}\rVert\,\lVert\mathbf{w}\rVert}.

Two vectors with a dot product of 0 are at a right angle; they are orthogonal, or perpendicular. For example (3,1)⋅(−1,3)=−3+3=0(3,1)\cdot(-1,3)=-3+3=0. This test works in any number of dimensions, where you can’t draw a right angle: (1,2,2)⋅(2,1,−2)=2+2−4=0(1,2,2)\cdot(2,1,-2)=2+2-4=0, so these two arrows in space are orthogonal.

A zero dot product does not mean one of the vectors is zero. Both (3,1)(3,1) and (−1,3)(-1,3) have length 10\sqrt{10}. 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 R4\mathbb{R}^4, 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.

cos θ
–
0.00 / 7.40 s
Describe this picture

Two frames of four samples each, (1,2,1,0)(1,2,1,0) and (0,1,2,1)(0,1,2,1), drawn as two rows of bars. Beside them (below them on a phone) the same two frames are drawn as two arrows, each 6\sqrt6 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, 1.6331.633 long, with its right angle. A readout shows cos⁡θ=0.667\cos\theta=0.667. 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 −1-1 and 1, so

∣v⋅w∣≤∥v∥ ∥w∥.\lvert\mathbf{v}\cdot\mathbf{w}\rvert\le\lVert\mathbf{v}\rVert\,\lVert\mathbf{w}\rVert.

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 10⋅5=7.071\sqrt{10}\cdot\sqrt5=7.071, the height the cosine curve reaches when w\mathbf{w} lies along v\mathbf{v}.

The straight path is never longer than the bent one. The sum v+w\mathbf{v}+\mathbf{w} is the third side of the tip-to-tail triangle from Vectors, so

∥v+w∥≤∥v∥+∥w∥.\lVert\mathbf{v}+\mathbf{w}\rVert\le\lVert\mathbf{v}\rVert+\lVert\mathbf{w}\rVert.

This is the triangle inequality. It follows from the first one. Square the left side:

∥v+w∥2=∥v∥2+2 v⋅w+∥w∥2.\lVert\mathbf{v}+\mathbf{w}\rVert^2=\lVert\mathbf{v}\rVert^2+2\,\mathbf{v}\cdot\mathbf{w}+\lVert\mathbf{w}\rVert^2.

By Cauchy–Schwarz the middle term is at most 2 ∥v∥ ∥w∥2\,\lVert\mathbf{v}\rVert\,\lVert\mathbf{w}\rVert, and ∥v∥2+2 ∥v∥ ∥w∥+∥w∥2\lVert\mathbf{v}\rVert^2+2\,\lVert\mathbf{v}\rVert\,\lVert\mathbf{w}\rVert+\lVert\mathbf{w}\rVert^2 is a perfect square. So

∥v+w∥2≤(∥v∥+∥w∥)2,\lVert\mathbf{v}+\mathbf{w}\rVert^2\le\big(\lVert\mathbf{v}\rVert+\lVert\mathbf{w}\rVert\big)^2,

and taking the square root of both sides gives the triangle inequality.

Worked example

Take v=(3,1)\mathbf{v}=(3,1) and w=(1,2)\mathbf{w}=(1,2).

  1. Dot product. v⋅w=3⋅1+1⋅2=5\mathbf{v}\cdot\mathbf{w}=3\cdot1+1\cdot2=5.
  2. Lengths. ∥v∥=10=3.162\lVert\mathbf{v}\rVert=\sqrt{10}=3.162 and ∥w∥=5=2.236\lVert\mathbf{w}\rVert=\sqrt5=2.236.
  3. Angle. cos⁡θ=5/(10 5)=5/50=0.707\cos\theta=5/(\sqrt{10}\,\sqrt5)=5/\sqrt{50}=0.707, so the angle θ\theta is 45°, which is π/4≈0.7854\pi/4\approx0.7854 rad. Check it another way: v\mathbf{v} points 18.4° above the horizontal and w\mathbf{w} points 63.4° above it, and 63.4° − 18.4° = 45°.
  4. Cauchy–Schwarz. 5≤50=7.0715\le\sqrt{50}=7.071.
  5. Triangle. v+w=(4,3)\mathbf{v}+\mathbf{w}=(4,3) has length 16+9=5\sqrt{16+9}=5, and 5≤10+5=5.3985\le\sqrt{10}+\sqrt5=5.398.
  6. Unit vector. v/∥v∥=(0.949,  0.316)\mathbf{v}/\lVert\mathbf{v}\rVert=(0.949,\;0.316).
  7. Right angles. (3,1)⋅(−1,3)=0(3,1)\cdot(-1,3)=0; in space, (1,2,2)⋅(2,1,−2)=0(1,2,2)\cdot(2,1,-2)=0, and ∥(1,2,2)∥=3\lVert(1,2,2)\rVert=3.

Predict before you drag: at which angle is the shadow longest? At 0°, when w\mathbf{w} lies along v\mathbf{v}. And if ∥v∥=2\lVert\mathbf{v}\rVert=2, ∥w∥=3\lVert\mathbf{w}\rVert=3 and θ\theta is 60°, whose cosine is 12\tfrac12, the product is 2⋅3⋅12=32\cdot3\cdot\tfrac12=3.

Where you’ll meet this

Work done by a force. Push a box with a force of (5,2)(5,2) newtons while it slides (3,0)(3,0) metres along the floor. Only the part of the force along the motion does work: (5,2)⋅(3,0)=15(5,2)\cdot(3,0)=15 joules. The sideways part, 2 newtons, does none.

How alike are two recordings? The frames (1,2,1,0)(1,2,1,0) and (0,1,2,1)(0,1,2,1) have dot product 0+2+2+0=40+2+2+0=4, and each has length 6\sqrt6. Their cosine is 4/6=0.6674/6=0.667, 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

QuantityFormulaNotes
Length∥v∥=v12+⋯+vn2\lVert\mathbf{v}\rVert=\sqrt{v_1^2+\dots+v_n^2}Pythagoras, one square per component
Unit vectorv/∥v∥\mathbf{v}/\lVert\mathbf{v}\rVertlength 1, same direction
Dot productv⋅w=∑q=1nvqwq\mathbf{v}\cdot\mathbf{w}=\sum_{q=1}^{n}v_qw_qa number; v⋅v=∥v∥2\mathbf{v}\cdot\mathbf{v}=\lVert\mathbf{v}\rVert^2
Shadow formv⋅w=∥v∥ ∥w∥cos⁡θ\mathbf{v}\cdot\mathbf{w}=\lVert\mathbf{v}\rVert\,\lVert\mathbf{w}\rVert\cos\thetashadow of w\mathbf{w} on v\mathbf{v}, times ∥v∥\lVert\mathbf{v}\rVert
Anglecos⁡θ=v⋅w/(∥v∥ ∥w∥)\cos\theta=\mathbf{v}\cdot\mathbf{w}/(\lVert\mathbf{v}\rVert\,\lVert\mathbf{w}\rVert)positive under 90°, negative over
Orthogonalv⋅w=0\mathbf{v}\cdot\mathbf{w}=0a right angle, in any number of dimensions
Cauchy–Schwarz∣v⋅w∣≤∥v∥ ∥w∥\lvert\mathbf{v}\cdot\mathbf{w}\rvert\le\lVert\mathbf{v}\rVert\,\lVert\mathbf{w}\rVertequal only when the arrows share a line
Triangle inequality∥v+w∥≤∥v∥+∥w∥\lVert\mathbf{v}+\mathbf{w}\rVert\le\lVert\mathbf{v}\rVert+\lVert\mathbf{w}\rVertthe straight path is never longer

Keep reading with a free account.

By creating an account you accept the terms of use.

Sign in

Already have an account? Use the service you signed up with.

Or keep reading: get new lessons by email

Are you 13 or over?

Accounts and email updates are for people aged 13 or over. We keep only your answer, never your date of birth.

End of the lesson

Where to go next.

Phasorium

Account

Sign inKeep your place on any device

Subjects

Site

Look