The Law of Cosines
The Law of Cosines (also called the cosine rule) is a fact about triangles: it relates the length of one side to the other two sides and the angle between them. In the research note, it is the step that lets us go from a special case — comparing vectors of length 1 — to the general cosine similarity formula used for sentence vectors of any length. This page proves the Law of Cosines from scratch, using nothing but the Pythagorean theorem and basic right-triangle trigonometry, then shows how it leads directly to that formula.
The cosine rule
Take any triangle with sides of length a, b, and c, and let θ be the angle between sides b and c (Exhibit A). The Law of Cosines states:
a² = b² + c² − 2bc cos(θ)
Proving it: drop a perpendicular
The proof needs only one construction: from the vertex where sides a and b meet, drop a perpendicular line down onto side c (or its extension), meeting it at a point we’ll call F. This splits side c into two pieces — a piece e next to θ, and the remaining piece c − e, and creates a new segment d, the length of the perpendicular itself (Exhibit B).
Quick refresher. In a right triangle, for an angle θ: cos(θ) = adjacent ÷ hypotenuse, and sin(θ) = opposite ÷ hypotenuse.
For the triangles with three sides, e, b, d, we have:
e / b = cos(θ) ⇒ e = b cos(θ)
d / b = sin(θ) ⇒ d = b sin(θ)
For the triangles with three sides, (c − e), d, a, we have:
(c − e)² + d² = a²
Substituting e = b cos(θ) and d = b sin(θ):
(c − b cosθ)² + (b sinθ)² = a²
c² − 2bc cosθ + b²cos²θ + b²sin²θ = a²
c² − 2bc cosθ + b²(cos²θ + sin²θ) = a²
c² − 2bc cosθ + b² = a²
that is, a² = b² + c² − 2bc cosθ
Since cos²(θ) + sin²(θ) always equals 1 as shown below
That’s the Law of Cosines, built from nothing more than a dropped perpendicular, the definitions of sine and cosine, and the Pythagorean theorem.
From triangle sides to vectors
To use this for sentence similarity, we reinterpret b and c as vectors sharing a common starting point (the origin), rather than as plain lengths. The third side, a, becomes the vector connecting their tips: a = c − b (Exhibit C) — exactly the setup used in the research note’s own derivation.
Writing each vector by its coordinates, b = [b1, b2] and c = [c1, c2], gives:
a = c − b = [c1 − b1, c2 − b2]
Cosine similarity from the cosine rule
Now combine two ways of writing |a|². The Law of Cosines gives one:
|a|² = |b|² + |c|² − 2|b||c|cos(θ)
and the coordinates of a = c − b give another, since the squared length of a vector is just the sum of its squared coordinates:
|a|² = (c1 − b1)² + (c2 − b2)²
Setting these equal and solving for the cosine term:1
2|b||c|cosθ = |b|² + |c|² − |c − b|²
2|b||c|cosθ = (b1²+b2²) + (c1²+c2²) − [(c1−b1)² + (c2−b2)²]
2|b||c|cosθ = (b1²+b2²) + (c1²+c2²) − [(c1²−2c1b1+b1²) + (c2²−2c2b2+b2²)]
2|b||c|cosθ = 2c1b1 + 2c2b2
Dividing both sides by 2 and then by |b||c| leaves:
cos(θ) = (c1b1 + c2b2) / (|b| × |c|) = (b · c) / (|b| × |c|)
This is exactly the cosine similarity formula from the research note’s “Generalizing beyond unit vectors: the Law of Cosines” section: the dot product of two vectors, divided by the product of their lengths. Everything above followed from one construction (a dropped perpendicular) and two facts you likely already knew: the Pythagorean theorem, and what sine and cosine mean in a right triangle.
Endnotes
- This derivation, connecting the Law of Cosines to the cosine similarity formula, is based on Yarun, “How Exactly Cosine Similarity Measures the Similarity? Maths behind and Usage in Python,” Towards Data Science, September 27, 2020, accessed December 3, 2025, towardsdatascience.com. ↩