The Law of Cosines

A supplementary explainer for the “Generalizing beyond unit vectors: the Law of Cosines” section of Natural Language Processing: Foundations and Applications without Hallucination from Generative AI.

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(θ)

Exhibit A A triangle with sides a, b, and c, and angle theta between sides b and c. θ b a c
Exhibit A — A general triangle. Sides a, b, and c, with θ the angle between b and c.

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).

Exhibit B The same triangle with a perpendicular dropped from the vertex of a and b onto side c, splitting c into e and c minus e, and creating segment d. θ b a e c − e d F
Exhibit B — Dropping a perpendicular. The perpendicular from the a–b vertex meets side c at F, splitting it into e and c − e, with d the length of the perpendicular. The small square at F marks the right angle.

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

Exhibit D A right triangle with hypotenuse 1, legs cos theta and sin theta. By the Pythagorean theorem, sin squared theta plus cos squared theta equals 1 squared, equals 1. sin²θ + cos²θ = 1² = 1 cos(θ) sin(θ) 1
Exhibit D — Why cos²(θ) + sin²(θ) = 1. In a right triangle with hypotenuse 1, the legs are cos(θ) and sin(θ). The Pythagorean theorem then gives cos²(θ) + sin²(θ) = 1² = 1 — the same fact used in the research note’s Exhibit 6.

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.

Exhibit C Vectors b and c drawn from a common origin, with vector a equal to c minus b forming the third side of the triangle, from the tip of b to the tip of c. θ b c a = c − b
Exhibit C — Sides as vectors. b and c share the origin; a = c − b is the vector from the tip of b to the tip of c.

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

  1. 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. ↩

← Back to the research note