Foundations to Frontier
Tests
A. Mathematical Foundations › A2 Linear Algebra › Vectors and spaces

Dot Product, Norms, Angles and Cosine Similarity

How much two vectors agree, how long a vector is, and the different ways to measure distance. These are the tools behind search engines, regularization and the physics of work.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Do two arrows agree?

Two friends push a box. If they push in the same direction, their efforts add up perfectly. If they push at right angles, neither helps the other. If they push in opposite directions, they fight each other. The dot product is a single number that captures this: big and positive when the arrows agree, zero when they’re at right angles, and negative when they disagree.

Shadows

Shine a torch straight down on an arrow. Its shadow on the floor shows how much of the arrow points along the floor. The dot product is closely related to the length of that shadow.

Many ways to measure distance

How far is the shop that’s 3 blocks east and 4 blocks north?

  • A bird flies straight there: 5 blocks (the straight line).
  • A taxi must follow the streets: 3 + 4 = 7 blocks.
  • A chess king, which moves diagonally for free, needs 4 moves (the bigger of 3 and 4).

All three are sensible ways to measure distance. Mathematicians call them norms.

📝 Quick check (Level 0)
0/3 answered
  1. 1. Two people push a cart in exactly opposite directions. Their 'agreement' number is…
  2. 2. A taxi goes 5 blocks east and 2 north. How many blocks does it drive?
  3. 3. Two arrows at a perfect right angle. Their agreement number is…

Level 1: Beginner

★★☆☆☆ Basic⏱ 15 minSecondary-school mathematics: arithmetic and elementary algebra.

The dot product

Multiply matching components and add:

u⋅v=u1v1+u2v2+⋯+unvn\mathbf u \cdot \mathbf v = u_1v_1 + u_2v_2 + \dots + u_nv_n

Example. (1,2)⋅(3,4)=3+8=11(1,2)\cdot(3,4) = 3 + 8 = 11, and (2,−1)⋅(1,2)=2−2=0(2,-1)\cdot(1,2) = 2 - 2 = 0.

Sign of u⋅v\mathbf u\cdot\mathbf v Angle between them Meaning
positive less than 90° roughly the same direction
zero exactly 90° perpendicular (orthogonal)
negative more than 90° roughly opposite

Length and distance from the dot product

∥v∥=v⋅v,d(u,v)=∥u−v∥\lVert\mathbf v\rVert = \sqrt{\mathbf v\cdot\mathbf v}, \qquad d(\mathbf u, \mathbf v) = \lVert\mathbf u - \mathbf v\rVert

The distance between (1,1)(1,1) and (4,5)(4,5) is ∥(3,4)∥=5\lVert(3,4)\rVert = 5.

Try the playground. Make the dot product zero and watch the angle become exactly 90°:

Dot product, angle and projection
projuv
u·v = 6 · angle = 53.13° · cosine similarity = 0.60 · projection of u onto v = (0.60, 1.80)
📝 Quick check (Level 1)
0/3 answered
  1. 1. (1, 2, 3)·(4, −5, 6) = ?
  2. 2. (3, 1)·(−1, 3) = 0. The vectors are…
  3. 3. Distance between (0, 0) and (6, 8)?

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 35 minUndergraduate students and practising engineers.

The geometric formula

u⋅v=∥u∥ ∥v∥cos⁡θ⟹cos⁡θ=u⋅v∥u∥ ∥v∥\mathbf u\cdot\mathbf v = \lVert\mathbf u\rVert\,\lVert\mathbf v\rVert\cos\theta \quad\Longrightarrow\quad \cos\theta = \frac{\mathbf u\cdot\mathbf v}{\lVert\mathbf u\rVert\,\lVert\mathbf v\rVert}

The right-hand side is the cosine similarity, which ranges from −1 (opposite) through 0 (unrelated) to 1 (same direction). It ignores length and only compares direction. That’s why it’s the standard way to compare text embeddings in search and RAG systems (F10).

Projection

The projection of u\mathbf u onto the direction of v\mathbf v is the “shadow” from Level 0:

proj⁡vu=u⋅vv⋅v v\operatorname{proj}_{\mathbf v}\mathbf u = \frac{\mathbf u\cdot\mathbf v}{\mathbf v\cdot\mathbf v}\,\mathbf v

What’s left over, u−proj⁡vu\mathbf u - \operatorname{proj}_{\mathbf v}\mathbf u, is perpendicular to v\mathbf v. Splitting a vector into “along” and “across” parts is the idea behind least squares (A2.4), Gram–Schmidt, and decomposing a robot’s force into normal and tangential parts.

Properties

The dot product is symmetric (u⋅v=v⋅u\mathbf u\cdot\mathbf v = \mathbf v\cdot\mathbf u), linear in each argument ((au+bw)⋅v=a u⋅v+b w⋅v(a\mathbf u + b\mathbf w)\cdot\mathbf v = a\,\mathbf u\cdot\mathbf v + b\,\mathbf w\cdot\mathbf v), and positive definite (v⋅v>0\mathbf v\cdot\mathbf v > 0 unless v=0\mathbf v = \mathbf 0). Any operation with these three properties is called an inner product (A2.4).

The p-norms

∥x∥p=(∑i∣xi∣p)1/p,∥x∥1=∑i∣xi∣,∥x∥2=∑ixi2,∥x∥∞=max⁡i∣xi∣\lVert\mathbf x\rVert_p = \Big(\sum_i\lvert x_i\rvert^p\Big)^{1/p}, \qquad \lVert\mathbf x\rVert_1 = \sum_i\lvert x_i\rvert, \quad \lVert\mathbf x\rVert_2 = \sqrt{\textstyle\sum_i x_i^2}, \quad \lVert\mathbf x\rVert_\infty = \max_i\lvert x_i\rvert

These are the bird (L2L^2), taxi (L1L^1, “Manhattan”) and king (L∞L^\infty, “Chebyshev”) distances. The unit ball {x:∥x∥p≤1}\{\mathbf x : \lVert\mathbf x\rVert_p \le 1\} changes shape with pp: a diamond at p=1p = 1, a circle at p=2p = 2, and close to a square as p→∞p \to \infty:

📈 The unit 'circle' ‖x‖ₚ = 1 for different p
-101-101
upper halflower half
p = 1: diamond. p = 2: circle. Large p: almost a square. Below p = 1 the shape caves in, and it's no longer a norm.
Why L1 regularization makes weights sparse

Lasso adds λ∥w∥1\lambda\lVert\mathbf w\rVert_1 to the loss, while Ridge adds λ∥w∥22\lambda\lVert\mathbf w\rVert_2^2. The L1L^1 ball has sharp corners on the axes, so the best point within it often lands on a corner, where some weights are exactly zero. The round L2L^2 ball has no corners, so weights shrink but rarely reach zero (D2).

🐍 Exercise 1: dot, norms, cosine and projection
Write dot, norm (any p, including math.inf), cosine and project. The tests check that the leftover u − proj is perpendicular to v.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Cosine similarity of (2, 0) and (5, 0)?
  2. 2. ‖(3, −4)‖₁ and ‖(3, −4)‖∞ are…
  3. 3. Projection of (2, 3) onto (1, 0)?

Level 3: Advanced

★★★★☆ Advanced⏱ 40 minGraduate level, with full derivations and proofs.

Why the geometric formula is true

Theorem

u⋅v=∥u∥∥v∥cos⁡θ\mathbf u\cdot\mathbf v = \lVert\mathbf u\rVert\lVert\mathbf v\rVert\cos\theta, where θ\theta is the angle between u\mathbf u and v\mathbf v.

Proof. Expand using linearity: ∥u−v∥2=(u−v)⋅(u−v)=∥u∥2+∥v∥2−2 u⋅v\lVert\mathbf u - \mathbf v\rVert^2 = (\mathbf u-\mathbf v)\cdot(\mathbf u-\mathbf v) = \lVert\mathbf u\rVert^2 + \lVert\mathbf v\rVert^2 - 2\,\mathbf u\cdot\mathbf v. The triangle with sides u\mathbf u, v\mathbf v and u−v\mathbf u - \mathbf v also satisfies the law of cosines: ∥u−v∥2=∥u∥2+∥v∥2−2∥u∥∥v∥cos⁡θ\lVert\mathbf u-\mathbf v\rVert^2 = \lVert\mathbf u\rVert^2 + \lVert\mathbf v\rVert^2 - 2\lVert\mathbf u\rVert\lVert\mathbf v\rVert\cos\theta. Compare the two. ∎

Two consequences:

  • Pythagoras in nn dimensions: if u⊥v\mathbf u\perp\mathbf v, then ∥u+v∥2=∥u∥2+∥v∥2\lVert\mathbf u + \mathbf v\rVert^2 = \lVert\mathbf u\rVert^2 + \lVert\mathbf v\rVert^2.
  • Cauchy–Schwarz: since ∣cos⁡θ∣≤1\lvert\cos\theta\rvert \le 1, ∣u⋅v∣≤∥u∥∥v∥\lvert\mathbf u\cdot\mathbf v\rvert \le \lVert\mathbf u\rVert\lVert\mathbf v\rVert. The algebraic proof, which works in any dimension without needing angles, is in A1-6. In Rn\mathbb{R}^n we define the angle through this formula, and Cauchy–Schwarz guarantees it makes sense.

What makes a norm

A norm satisfies: ∥x∥≥0\lVert\mathbf x\rVert \ge 0 with equality only for 0\mathbf 0, ∥cx∥=∣c∣∥x∥\lVert c\mathbf x\rVert = \lvert c\rvert\lVert\mathbf x\rVert, and the triangle inequality. Every ∥⋅∥p\lVert\cdot\rVert_p with p≥1p \ge 1 is a norm (Minkowski, A1-6).

The parallelogram law. ∥u+v∥2+∥u−v∥2=2∥u∥2+2∥v∥2\lVert\mathbf u+\mathbf v\rVert^2 + \lVert\mathbf u-\mathbf v\rVert^2 = 2\lVert\mathbf u\rVert^2 + 2\lVert\mathbf v\rVert^2 holds for L2L^2, but not for L1L^1 or L∞L^\infty. In fact, a norm comes from an inner product if and only if it satisfies this law (Jordan–von Neumann theorem, 1935).

All norms on ℝⁿ are equivalent

Theorem

For all x∈Rn\mathbf x\in\mathbb{R}^n: ∥x∥∞≤∥x∥2≤n ∥x∥∞\lVert\mathbf x\rVert_\infty \le \lVert\mathbf x\rVert_2 \le \sqrt n\,\lVert\mathbf x\rVert_\infty and ∥x∥2≤∥x∥1≤n ∥x∥2\lVert\mathbf x\rVert_2 \le \lVert\mathbf x\rVert_1 \le \sqrt n\,\lVert\mathbf x\rVert_2.

Proof. If m=max⁡∣xi∣m = \max\lvert x_i\rvert, then m2≤∑xi2≤nm2m^2 \le \sum x_i^2 \le n m^2, which gives the first pair. For the second: (∑∣xi∣)2=∑xi2+∑i≠j∣xi∣∣xj∣≥∑xi2(\sum\lvert x_i\rvert)^2 = \sum x_i^2 + \sum_{i\ne j}\lvert x_i\rvert\lvert x_j\rvert \ge \sum x_i^2. And by Cauchy–Schwarz with the all-ones vector, ∑∣xi∣=(1,…,1)⋅(∣x1∣,… )≤n ∥x∥2\sum\lvert x_i\rvert = (1,\dots,1)\cdot(\lvert x_1\rvert,\dots) \le \sqrt n\,\lVert\mathbf x\rVert_2. ∎

So in finite dimensions, “converges” means the same thing whichever norm you use. Only the constants change, and those constants grow with nn, which matters in high-dimensional ML.

Dual norms. ∣u⋅v∣≤∥u∥1∥v∥∞\lvert\mathbf u\cdot\mathbf v\rvert \le \lVert\mathbf u\rVert_1\lVert\mathbf v\rVert_\infty. L1L^1 and L∞L^\infty are dual to each other, and L2L^2 is its own dual. Duality appears in adversarial robustness (an L∞L^\infty attack budget leads to an L1L^1 weight penalty, M1) and in optimization (A6).

Exercise 3.1: Check the parallelogram law for u = (1,0) and v = (0,1) in L1

∥u+v∥12+∥u−v∥12=4+4=8\lVert\mathbf u+\mathbf v\rVert_1^2 + \lVert\mathbf u-\mathbf v\rVert_1^2 = 4 + 4 = 8, but 2∥u∥12+2∥v∥12=42\lVert\mathbf u\rVert_1^2 + 2\lVert\mathbf v\rVert_1^2 = 4. The law fails, so L1L^1 doesn’t come from any inner product.

📝 Quick check (Level 3)
0/3 answered
  1. 1. If u ⊥ v with ‖u‖ = 6 and ‖v‖ = 8, then ‖u + v‖ = ?
  2. 2. For x ∈ ℝ¹⁰⁰, the largest possible ‖x‖₁ / ‖x‖₂ is…
  3. 3. Which norm satisfies the parallelogram law?

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

The curse of dimensionality for distances

As the dimension dd grows, distances from a query to random points concentrate: the nearest and farthest points become almost equally far away. Beyer et al. (1999) showed that under broad conditions, (Dmax⁡−Dmin⁡)/Dmin⁡→0(D_{\max} - D_{\min})/D_{\min} \to 0, which makes “nearest neighbour” less meaningful. Practical consequences:

  • Similarity search over embeddings relies on learned representations that are far from random, not raw high-dimensional data.
  • Search engines and RAG systems use approximate nearest-neighbour indexes such as HNSW graphs (Malkov and Yashunin, 2020) and quantization, trading a little accuracy for huge speed-ups (F10).
🐍 Exercise 2 (research): distance concentration
For random points in [0,1]^d, compute (max − min)/min of the distances from a random query. Watch it shrink as d grows.

Dot products at the heart of Transformers

Attention scores are dot products q⋅k\mathbf q\cdot\mathbf k. If the entries are independent with variance 1, the dot product of two dkd_k-dimensional vectors has variance dkd_k. Dividing by dk\sqrt{d_k} brings it back to variance 1, which keeps the softmax from saturating (Vaswani et al., 2017). Some models go further and normalize queries and keys (QK-norm), so the score is a scaled cosine similarity. This has been reported to stabilize large-scale training (F5).

Dot products in robotics and physics

  • Work and power: W=F⋅ΔxW = \mathbf F\cdot\Delta\mathbf x and P=F⋅vP = \mathbf F\cdot\mathbf v. A motor that pushes perpendicular to the motion does no work. Practice Code 5 shows that a constant force does the same work on every path between two points.
  • Joint power: for a robot arm, mechanical power is τ⋅q˙\boldsymbol\tau\cdot\dot{\mathbf q} (torques times joint speeds), which is used in energy-aware control.
  • Contact and collision: the normal component of a contact force is (F⋅n)n(\mathbf F\cdot\mathbf n)\mathbf n. Friction acts on the tangential remainder (R4).
  • Visibility and lighting: a surface faces a camera when n⋅v<0\mathbf n\cdot\mathbf v < 0, which is used in rendering and 3D perception (G4).

References

  1. P. Jordan and J. von Neumann (1935). “On inner products in linear, metric spaces.” Annals of Mathematics, 36(3).
  2. R. Tibshirani (1996). “Regression shrinkage and selection via the lasso.” Journal of the Royal Statistical Society B, 58(1).
  3. K. Beyer, J. Goldstein, R. Ramakrishnan, U. Shaft (1999). “When is ‘nearest neighbor’ meaningful?” ICDT.
  4. A. Vaswani et al. (2017). “Attention is all you need.” NeurIPS.
  5. Y. Malkov and D. Yashunin (2020). “Efficient and robust approximate nearest neighbor search using hierarchical navigable small world graphs.” IEEE TPAMI, 42(4).
  6. G. Strang (2016). Introduction to Linear Algebra, 5th ed. Chapter 1.2.