← mapVectors & Matrices

The Dot Product

⚗ Dr. Möbius, from the lab

Two vectors walk into my lab. I ask them one question: "How aligned are you?" They spit out a single number. That number is the dot product, and it is the most important operation in all of computational mathematics — I will die on this hill. Perpendicularity, projection, length, angles — ALL of it is just the dot product in a different hat. The first time I saw this as a student I said "holy shit" out loud in the library. You'll understand why in about ten minutes.

THE BIG IDEA

The dot product takes two vectors and returns a scalar that encodes both the alignment of their directions and a product of their magnitudes, simultaneously.

The componentwise definition

Given v=(v1,v2)\vec{v} = (v_1, v_2) and w=(w1,w2)\vec{w} = (w_1, w_2), the dot product is:

vw=v1w1+v2w2\vec{v} \cdot \vec{w} = v_1 w_1 + v_2 w_2

Multiply corresponding components, then sum. In Rn\mathbb{R}^n:

vw=i=1nviwi=v1w1+v2w2++vnwn\vec{v} \cdot \vec{w} = \sum_{i=1}^{n} v_i w_i = v_1 w_1 + v_2 w_2 + \cdots + v_n w_n

The result is a scalar — a single real number. This operation eats two vectors and produces a number. Shockingly, beautifully useful.

The first thing to check: what happens when you dot a vector with itself?

vv=v12+v22=v2\vec{v} \cdot \vec{v} = v_1^2 + v_2^2 = \|\vec{v}\|^2

The dot product of a vector with itself is the square of its length. So v=vv\|\vec{v}\| = \sqrt{\vec{v} \cdot \vec{v}}. In other words: length is a special case of the dot product. The whole length formula from the Vectors lesson was secretly a dot product wearing a disguise.

The geometric formula — two definitions, one number

Here's the jaw-dropping part. The same dot product has a geometric interpretation:

vw=vwcosθ\vec{v} \cdot \vec{w} = \|\vec{v}\| \|\vec{w}\| \cos\theta

where θ\theta is the angle between the vectors. Same number, two formulas. This is not a coincidence — it's a theorem.

Proof via the law of cosines. Consider the triangle formed by v\vec{v}, w\vec{w}, and vw\vec{v} - \vec{w} (the arrow from the tip of w\vec{w} to the tip of v\vec{v}). The law of cosines says:

vw2=v2+w22vwcosθ\|\vec{v} - \vec{w}\|^2 = \|\vec{v}\|^2 + \|\vec{w}\|^2 - 2\|\vec{v}\|\|\vec{w}\|\cos\theta

Now expand the left side using the componentwise definition and vv=v2\vec{v} \cdot \vec{v} = \|\vec{v}\|^2:

vw2=(vw)(vw)=vv2vw+ww=v22vw+w2\|\vec{v} - \vec{w}\|^2 = (\vec{v} - \vec{w}) \cdot (\vec{v} - \vec{w}) = \vec{v}\cdot\vec{v} - 2\vec{v}\cdot\vec{w} + \vec{w}\cdot\vec{w} = \|\vec{v}\|^2 - 2\vec{v}\cdot\vec{w} + \|\vec{w}\|^2

Substituting and simplifying:

v22vw+w2=v2+w22vwcosθ\|\vec{v}\|^2 - 2\vec{v}\cdot\vec{w} + \|\vec{w}\|^2 = \|\vec{v}\|^2 + \|\vec{w}\|^2 - 2\|\vec{v}\|\|\vec{w}\|\cos\theta

The v2+w2\|\vec{v}\|^2 + \|\vec{w}\|^2 terms cancel, leaving:

2vw=2vwcosθvw=vwcosθ-2\vec{v}\cdot\vec{w} = -2\|\vec{v}\|\|\vec{w}\|\cos\theta \quad \Rightarrow \quad \vec{v}\cdot\vec{w} = \|\vec{v}\|\|\vec{w}\|\cos\theta

This is what it looks like when algebra and geometry agree completely. Two independent paths to the same formula — that is mathematics telling you something real, something deep, something that is not an accident. When two completely different derivations converge on the same answer, pay the hell attention.

Reading the sign of the dot product

From the geometric formula, the sign of vw\vec{v} \cdot \vec{w} is entirely determined by cosθ\cos\theta:

  • vw>0\vec{v} \cdot \vec{w} > 0: cosθ>0\cos\theta > 0, so 0θ<90°0 \le \theta < 90° — the vectors point in a generally similar direction. Acute angle.
  • vw=0\vec{v} \cdot \vec{w} = 0: cosθ=0\cos\theta = 0, so θ=90°\theta = 90° — the vectors are perpendicular (orthogonal). Huge.
  • vw<0\vec{v} \cdot \vec{w} < 0: cosθ<0\cos\theta < 0, so 90°<θ180°90° < \theta \le 180° — the vectors point in a generally opposite direction. Obtuse angle.

The zero case deserves its own banner: vw\vec{v} \perp \vec{w} if and only if vw=0\vec{v} \cdot \vec{w} = 0. Perpendicularity — a geometric notion about arrows meeting at right angles in space — has been reduced to checking whether a sum of products equals zero. This is one of the greatest trades in mathematics. You have converted geometry into algebra, and that conversion is your ticket to working in any number of dimensions. Your visual cortex can only handle three; the dot product handles all of them. That matters enormously and we will use it relentlessly.

Projection: how much of v\vec{v} lies along w\vec{w}?

Given vectors v\vec{v} and w\vec{w}, the projection of v\vec{v} onto w\vec{w} is the shadow that v\vec{v} casts onto the line through w\vec{w} (with a light source shining perpendicularly to w\vec{w}).

The scalar projection (just the signed length of the shadow) is:

compwv=vww\text{comp}_{\vec{w}}\vec{v} = \frac{\vec{v} \cdot \vec{w}}{\|\vec{w}\|}

From the geometric formula: vw=vwcosθ\vec{v} \cdot \vec{w} = \|\vec{v}\|\|\vec{w}\|\cos\theta, so this equals vcosθ\|\vec{v}\|\cos\theta — exactly the adjacent-side-over-hypotenuse geometry of a right triangle.

The vector projection (the actual shadow vector, pointing along w\vec{w}) is:

projwv=vww2w=vwwww\text{proj}_{\vec{w}}\vec{v} = \frac{\vec{v} \cdot \vec{w}}{\|\vec{w}\|^2}\,\vec{w} = \frac{\vec{v} \cdot \vec{w}}{\vec{w} \cdot \vec{w}}\,\vec{w}

The scalar vww2\frac{\vec{v}\cdot\vec{w}}{\|\vec{w}\|^2} tells you how many copies of w\vec{w} fit in the shadow; multiplying by w\vec{w} converts that count back into a vector.

Drag the vectors below and watch the projection shadow respond in real time:

dot product & projection
uv
u·v = 11θ = 42.3°proj_v(u) = (2.59, 0.65) acute

The formula vwwww\frac{\vec{v}\cdot\vec{w}}{\vec{w}\cdot\vec{w}}\vec{w} might look intimidating, but it's just: dot-product divided by squared-length, times the direction. Memorize the shape of the formula, not the symbols — if you forget it you can re-derive it in ninety seconds from the geometry. That's how you actually own a formula.

🔬 SPECIMENS (worked examples)

Worked example 1 — computing a dot product and catching orthogonality red-handed

Let v=(32)\vec{v} = \begin{pmatrix} 3 \\ -2 \end{pmatrix} and w=(46)\vec{w} = \begin{pmatrix} 4 \\ 6 \end{pmatrix}. (a) Compute vw\vec{v} \cdot \vec{w}. (b) Are v\vec{v} and w\vec{w} orthogonal?

(a) Componentwise: vw=(3)(4)+(2)(6)=1212=0\vec{v} \cdot \vec{w} = (3)(4) + (-2)(6) = 12 - 12 = 0

(b) Yes — the dot product is exactly zero, which means cosθ=0\cos\theta = 0, which means θ=90°\theta = 90°. These vectors are orthogonal. You can verify geometrically: (3,2)(3, -2) has slope 2/3-2/3; (4,6)(4, 6) has slope 6/4=3/26/4 = 3/2. Since (2/3)(3/2)=1(-2/3)(3/2) = -1, the slopes multiply to 1-1, confirming perpendicularity. The algebra and the geometry agree — as they always do.

Worked example 2 — finding the angle between two vectors

Find the angle between a=(10)\vec{a} = \begin{pmatrix} 1 \\ 0 \end{pmatrix} and b=(11)\vec{b} = \begin{pmatrix} 1 \\ 1 \end{pmatrix}.

Use the geometric formula rearranged for θ\theta:

cosθ=abab\cos\theta = \frac{\vec{a}\cdot\vec{b}}{\|\vec{a}\|\,\|\vec{b}\|}

Dot product: ab=(1)(1)+(0)(1)=1\vec{a}\cdot\vec{b} = (1)(1) + (0)(1) = 1

Lengths: a=12+02=1,b=12+12=2\|\vec{a}\| = \sqrt{1^2 + 0^2} = 1, \quad \|\vec{b}\| = \sqrt{1^2 + 1^2} = \sqrt{2}

Angle: cosθ=112=12\cos\theta = \frac{1}{1 \cdot \sqrt{2}} = \frac{1}{\sqrt{2}}

θ=arccos ⁣(12)=45°\theta = \arccos\!\left(\frac{1}{\sqrt{2}}\right) = 45°

Sanity check: (1,0)(1,0) points along the positive xx-axis; (1,1)(1,1) points northeast at 45°45° from the xx-axis. Of course the angle between them is 45°45°.

Worked example 3 — projection and the shadow

Project v=(43)\vec{v} = \begin{pmatrix} 4 \\ 3 \end{pmatrix} onto w=(10)\vec{w} = \begin{pmatrix} 1 \\ 0 \end{pmatrix}.

Step 1: Compute the dot products. vw=(4)(1)+(3)(0)=4,ww=12+02=1\vec{v}\cdot\vec{w} = (4)(1) + (3)(0) = 4, \quad \vec{w}\cdot\vec{w} = 1^2 + 0^2 = 1

Step 2: Apply the projection formula. projwv=vwwww=41(10)=(40)\text{proj}_{\vec{w}}\vec{v} = \frac{\vec{v}\cdot\vec{w}}{\vec{w}\cdot\vec{w}}\,\vec{w} = \frac{4}{1}\begin{pmatrix}1\\0\end{pmatrix} = \begin{pmatrix}4\\0\end{pmatrix}

This is the shadow of (4,3)(4,3) onto the xx-axis. It makes perfect sense: projecting onto the horizontal axis just strips away the vertical component and keeps the horizontal one. The formula works.

Remainder: vprojwv=(4,3)(4,0)=(0,3)\vec{v} - \text{proj}_{\vec{w}}\vec{v} = (4,3) - (4,0) = (0,3). This remainder is orthogonal to w=(1,0)\vec{w} = (1,0): (0,3)(1,0)=0(0,3)\cdot(1,0) = 0. The projection decomposes v\vec{v} into a w\vec{w}-part and a w\vec{w}-perpendicular part. File that decomposition trick away — it runs the Gram-Schmidt algorithm later.

☠ KNOWN HAZARDS

  • The dot product is a scalar, not a vector. vw\vec{v}\cdot\vec{w} produces a number, not an arrow. Writing vw=(23)\vec{v}\cdot\vec{w} = \begin{pmatrix}2\\3\end{pmatrix} is incoherent — that's a type error, and in this lab type errors are grounds for expulsion.

  • Dividing by w\|\vec{w}\| vs w2\|\vec{w}\|^2 in the projection formula. The scalar projection uses w\|\vec{w}\| in the denominator; the VECTOR projection uses w2\|\vec{w}\|^2 (or equivalently ww\vec{w}\cdot\vec{w}). Confusing these gives a scalar when you need a vector, or a vector that's the wrong length.

  • Assuming vw=0\vec{v}\cdot\vec{w} = 0 implies v=0\vec{v} = \vec{0} or w=0\vec{w} = \vec{0}. The zero product rule from real numbers does NOT apply here in the same way. The dot product is zero whenever the vectors are perpendicular — which has nothing to do with either being zero.

  • The angle formula requires v,w0\|\vec{v}\|, \|\vec{w}\| \ne 0. If either vector is zero, cosθ\cos\theta is undefined. The zero vector has no direction, so "the angle between 0\vec{0} and anything" doesn't exist. Every theorem about angles has "v,w0\vec{v}, \vec{w} \ne \vec{0}" baked in.

TL;DR

  • Componentwise: vw=v1w1+v2w2+\vec{v}\cdot\vec{w} = v_1 w_1 + v_2 w_2 + \cdots. Result is a scalar.

  • Geometric: vw=vwcosθ\vec{v}\cdot\vec{w} = \|\vec{v}\|\|\vec{w}\|\cos\theta. The angle is encoded in the sign and magnitude.

  • vv=v2\vec{v}\cdot\vec{v} = \|\vec{v}\|^2 — length is a special case of the dot product.

  • vw\vec{v}\perp\vec{w} if and only if vw=0\vec{v}\cdot\vec{w} = 0 — perpendicularity becomes algebra.

  • Projection of v\vec{v} onto w\vec{w}: the vector vwwww\frac{\vec{v}\cdot\vec{w}}{\vec{w}\cdot\vec{w}}\,\vec{w}.

unlocks