Skip to section
Foundationsfor rotation-free search
Section 5 of 5210% of course
Contents
Chapter 1 Section 1.5 60 min

Part I · Mathematical foundations

The dot product, built one operation at a time

Combine matching products and a finite sum into one scalar.

First multiply; then collapse the list

You already know both ingredients for this. Coordinatewise multiplication produces a list [a0b0, …]. A finite sum takes a list and adds every entry. If we compose them in that exact order, we get a dot product.

two listsa, b
matching productspi = aibi
one total∑pi
a · b = ∑i=0D−1 aibi

You'd read it aloud like this: “a dot b equals the sum, from i equals zero through D minus one, of a at i times b at i.”

Worked example

Calculate with a table

Matching-position multiplication
position i0123left value aᵢ2-131right value bᵢ301-1product aᵢbᵢ603-1
a · b = 6 + 0 + 3 − 1 = 8
Animated visual · ManimThe dot-product machine

Matching pairs multiply one at a time, then enter a running total. Unmute for narration.

Worked example

Calculate with a loop

score = 0
for i in 0..D:
    score += a[i] * b[i]

At each step in the loop, one matching product joins the running total. After D iterations, score is just a single number.

Read a dot product as a contribution ledger

Think of each coordinate as contributing one signed amount, aibi, to a final score. A large positive contribution raises the score. A negative contribution lowers it. A zero on either side silences that coordinate completely. Because the final scalar hides all this history, it's always a good idea to inspect the individual products if a score surprises you.

Worked example

Cancellation can hide large activity

For a=[100,100] and b=[1,−1], the two contributions are +100 and −100. The dot product is zero, even though neither list was empty and neither contribution was small. A zero score just means the signed contributions completely cancelled each other out; it definitely doesn't mean “nothing happened.”

Simple properties you can verify

Swap the lists
a · b = b · a

Ordinary multiplication is commutative at every coordinate.

Dot a list with itself
a · a = a₀² + … + aD−1²

Squares are always nonnegative, so a · a is always ≥ 0.

Use a zero list
a · 0 = 0

Every single coordinate product will contain a zero.

Check your understanding

a ⊙ b is a list, while a · b is what?

Optional geometric meaning

If you prefer to think of vectors as arrows in space, the dot product acts as a measure of how aligned they are:

a · b = ‖a‖ ‖b‖ cos(θ)

This is a super useful way to visualize it, but it isn't required for the arithmetic. The ordered-list definition works perfectly on its own.

The symbol ‖a‖ just means the length of the arrow represented by a, which we compute as √(a·a). The angle θ measures the turn from one arrow to the other. Because cos(0)=1, arrows pointing the same way have a positive dot product. Since cos(π/2)=0, perpendicular arrows have a dot product of zero. And arrows pointing in opposite directions? They'll give you a negative dot product.

Experiment with the full calculation

Use the reactive notebook below to edit coordinates, inspect every signed contribution, and compare the raw dot product with cosine similarity. Try making one coordinate zero, or negating an entire input. Before you make an edit, try to predict which quantities will change.

Reactive Python laboratory · marimo + PyodideVectors from boxes to similarity

Edit coordinates and inspect alignment, contribution traces, norms, dot products, and cosine similarity.

Open full-screen lab ↗

Runs entirely in this browser. Python executes in Pyodide WebAssembly with no remote kernel. The construction code stays visible while reactive dependents recompute whenever you change an input.

Account for the work

A length-D dot product performs exactly D multiplications and D−1 additions (when D is positive). Its running time grows in direct proportion to D, which we write as O(D). This isn't a performance bottleneck yet, but taking a precise inventory of the work is the first step before any later optimization.

Check your understanding

What is [1, −2, 4] · [3, 5, −1]?

Retrieval check: derive the formula instead of reciting it

Start with matching products pi=aibi. Sum that list across all valid indices: a·b=∑ from i=0 through D−1 of aibi.

Section summary

  • A dot product is just coordinatewise multiplication followed by a finite sum.
  • It takes two length-D lists and spits out a single number.
  • The compact formula is literally just a loop written in math syntax.
  • Geometry is a fun bonus, not a strict prerequisite.
Reactive Python laboratory · marimo + PyodideVectors from boxes to similarity

Edit coordinates and inspect alignment, contribution traces, norms, dot products, and cosine similarity.

Open full-screen lab ↗

Runs entirely in this browser. Python executes in Pyodide WebAssembly with no remote kernel. The construction code stays visible while reactive dependents recompute whenever you change an input.

Repository layer · second pass

How does a dot product turn many local comparisons into one score?

The dot product is a two-stage operation: multiply matching positions, then add every product. The intermediate product list explains the final scalar. Keeping it visible is especially valuable when signs, scales, or unexpected scores need debugging.

The result depends on both direction and magnitude. A large positive product supports alignment, a large negative product opposes it, and a zero product contributes nothing. Only much later will normalization let us interpret this number primarily as directional similarity.

Reasoning chain

  1. 1

    Check the shared dimension D.

  2. 2

    Form D matching products.

  3. 3

    Accumulate them into one scalar.

  4. 4

    Audit individual contributions before interpreting the total.

  5. 5

    Separate the mathematical score from any later ranking policy.

Worked trace

Audit a score by contribution

  1. Use q = [1, −2, 4] and v = [3, 5, −1].
  2. Products are [3, −10, −4].
  3. The running totals are 3, −7, −11.
  4. The negative second coordinate dominates the result.

Result. q·v = −11; the contribution trace explains why.

Executable lens · Python

Make the hidden state visible

def dot(left, right):
    if len(left) != len(right):
        raise ValueError("dimensions must match")
    return sum(a * b for a, b in zip(left, right))

assert dot([1, -2, 4], [3, 5, -1]) == -11

Retype this example, predict each intermediate value, and then change one input that touches a boundary.

Misconception clinic

Tempting mistakes

  • Multiplying every left value by every right value; that is a different pairing rule.
  • Interpreting a raw dot product as cosine similarity without controlling lengths.

Retrieval and transfer

Close the book first

  1. Find two nonzero vectors with dot product zero.
  2. Change one coordinate in the worked example to make the score positive.
  3. Explain why a dot product returns one number even though it performs D multiplications.