Skip to content
DoingCamp CS

Make it comfy

Lesson style

Quick mode shows the key picture, a summary and the practice. Good for a refresher.

Theme
Font

Both fonts are designed to be easy to read. Some readers with dyslexia prefer Lexend.

Text size
Spacing
Animations

Lesson 3 of 20 · about 15 min

The dot product

How can a computer tell that two songs, two documents or two people's tastes are alike? It turns them into vectors and measures the angle between them with one small calculation, the dot product. You'll drag two arrows and watch one cast a shadow on the other, find perpendicular arrows, and build a tiny movie recommender.

🧭 What you need first: Linear combinations and span

The story

In 2013, a team at Google published word2vec, a program that read billions of words of news text and turned every word into a vector of a few hundred numbers. Nobody told it what words mean. It only learned which words appear near which other words.

The surprise was in the directions. The vectors for “Paris” and “France” pointed in similar directions; so did “walking” and “running”. Even more famous: take the vector for “king”, subtract “man”, add “woman”, and the nearest word vector (not counting the three words you started with) is “queen”.

To measure “pointing in similar directions” they used one tiny calculation, the same one that search engines use to compare documents, music apps to compare songs, and face unlock to compare faces. It’s called the dot product, and it takes two lines of code.

Another way: a shopping bill

Your basket is a vector of amounts: (3 apples, 2 loaves). The price list is a vector too: (0.50 each, 2.00 each). The bill is 3 × 0.50 + 2 × 2.00 = 5.50: multiply matching entries and add. That’s a dot product. Every time you total a receipt, you’re computing one.

Play with it

Shadows and angles

The dot product a⋅b=a1b1+a2b2a \cdot b = a_1 b_1 + a_2 b_2 looks like pure arithmetic. The picture hides inside it: drop a light straight down onto a’s line, and look at b’s shadow. Predict before you drag:

a = (3, 1). Which b gives a dot product of 0?

🎯 1/4: Make the arrows perpendicular (dot product 0), with neither arrow lying along an axis.

Drag the tips of a (blue) and b (orange). The thick line on a's direction is b's shadow: green when it points along a, red when it points backwards.

ab

a·b = 3×0 + 1×2 = 2

|a| = 3.16, |b| = 2, angle = 71.6°

cosine similarity = a·b / (|a||b|) = 0.316

Positive: they point roughly the same way (angle under 90°).

The sign of the dot product tells you whether two arrows agree (positive), are unrelated (zero) or disagree (negative). Its size grows with both lengths and with how closely they line up.

Name it

The real words

  • The Dot product of two vectors with the same number of components:

    a⋅b=a1b1+a2b2+⋯+anbna \cdot b = a_1 b_1 + a_2 b_2 + \cdots + a_n b_n

    It’s a single number (a scalar). The order doesn’t matter: a⋅b=b⋅aa \cdot b = b \cdot a.

  • Length is a dot product with itself: a⋅a=a12+a22=∣a∣2a \cdot a = a_1^2 + a_2^2 = |a|^2, so ∣a∣=a⋅a|a| = \sqrt{a \cdot a}.

  • The geometric formula, which follows from the law of cosines:

    a⋅b=∣a∣ ∣b∣cos⁡θa \cdot b = |a|\,|b|\cos\theta

    where θ\theta is the angle between the arrows. Since ∣b∣cos⁡θ|b|\cos\theta is the signed length of b’s shadow on a’s line, the dot product is (shadow length) × ∣a∣|a|.

  • Two vectors are perpendicular (also called orthogonal) exactly when a⋅b=0a \cdot b = 0. A quick way to get a perpendicular arrow in 2D: swap the components and negate one, (3,1)→(−1,3)(3, 1) \to (-1, 3).

  • Finding the angle: rearrange to cos⁡θ=a⋅b∣a∣ ∣b∣\cos\theta = \frac{a \cdot b}{|a|\,|b|}, then θ=arccos⁡(… )\theta = \arccos(\dots).

  • Cosine similarity is that same ratio, used to compare directions while ignoring length. A long review and a short review with the same opinions point the same way, so they get similarity 1.

    Try it: the panel now shows the cosine similarity too.

🎯 1/3: Get a cosine similarity of exactly 1 while giving b a different length from a.

Drag the tips of a (blue) and b (orange). The thick line on a's direction is b's shadow: green when it points along a, red when it points backwards.

ab

a·b = 2×−1 + 1×2 = 0

|a| = 2.24, |b| = 2.24, angle = 90°

cosine similarity = a·b / (|a||b|) = 0

Zero: perpendicular! No shadow at all.

Build it

Dot, length, angle

Write dot(a, b) for vectors of any length, length(a) using only dot and math.sqrt, and angle_deg(a, b): the angle in degrees, from cos θ = a·b / (|a||b|). Use math.acos and math.degrees.

Challenge

A tiny recommender

Four friends rated four films from 1 to 5. Each person is a vector of ratings. To recommend films to Alex, find the person whose tastes point in the most similar direction.

Write cosine(a, b) = a·b / (|a||b|). Then most_similar(name, people): people is a dict of name → ratings; return the OTHER person with the highest cosine similarity to people[name].

Cara and Dev use different parts of the rating scale (Dev never gives a 5), but their vectors point almost the same way: similarity about 0.96. Comparing directions instead of raw numbers is exactly why cosine similarity is so popular. Real recommenders work the same way with millions of people and thousands of items.

Remember it

a·b = −6, with |a| = 2 and |b| = 3. What can you say about the angle between a and b?

Got it?

Finish the lesson to color it in on your map and collect its concept cards.

Did this lesson click?