Skip to content
Road to Intelligence

Concept · Chapter 2: The Math Toolkit

Dot Product

Must knowImplement25 minDifficulty

The dot product multiplies two vectors entry by entry and adds the results, giving one number that is large when the vectors point the same way.

The problem

Once things are vectors, we need a cheap way to ask: how similar — how aligned — are these two?

The solution

Multiply matching entries and sum them. The result grows when the vectors point in similar directions, shrinks toward zero when they're unrelated, and goes negative when they oppose.

The consequence

This single operation becomes the similarity score behind neurons, embedding search and attention — and because it's just multiply-and-add, GPUs can do trillions of them per second.

You should understand first

  1. Vectors
  2. Dot Product

Intuition

You have two vectors and want one number that says how much they agree. The dot product gives exactly that: it rewards dimensions where both vectors are large and have the same sign, and penalizes dimensions where they disagree.

Think of rating films on two axes, action and romance. You like action a lot and romance a little: you = [2, 1]. A film that's pure action is film = [3, 0]. How well does the film match you? Multiply your appetite for each quality by how much the film has of it, and add up.

Tiny numeric example

  1. Multiply matching entries

    [1,2]⋅[3,4][1, 2] \cdot [3, 4] → first entries: 1×3=31 \times 3 = 3; second entries: 2×4=82 \times 4 = 8.

  2. Add them up

    3+8=113 + 8 = 11. That's the dot product: one number, not a vector.

  3. Try an opposite pair

    [1,2]⋅[−1,−2]=−1−4=−5[1, 2] \cdot [-1, -2] = -1 - 4 = -5. Pointing opposite ways gives a negative score.

  4. Try a perpendicular pair

    [1,2]⋅[2,−1]=2−2=0[1, 2] \cdot [2, -1] = 2 - 2 = 0. At right angles, the vectors share nothing: zero.

The equation

a⋅b  =  ∑i=1dai bi  =  ∥a∥ ∥b∥cos⁡θ\mathbf{a} \cdot \mathbf{b} \;=\; \sum_{i=1}^{d} a_i\, b_i \;=\; \lVert \mathbf{a} \rVert\, \lVert \mathbf{b} \rVert \cos\theta

That scaling matters. A very long vector can score high against everything. When you only care about direction, divide the lengths out — that's cosine similarity:

cos⁡θ=a⋅b∥a∥ ∥b∥\cos\theta = \frac{\mathbf{a}\cdot\mathbf{b}}{\lVert\mathbf{a}\rVert\,\lVert\mathbf{b}\rVert}

Try it

Try it

Dot Product Lab

Drag two vectors and watch their dot product and cosine similarity change with the angle between them.

Know well5 min

Where it appears in AI

  • A neuron computes w⋅x+b\mathbf{w}\cdot\mathbf{x} + b: how much the input x\mathbf{x} matches the pattern stored in its weights w\mathbf{w}.
  • Matrix multiplication is a grid of dot products — every row of one matrix against every column of the other. A neural-network layer is thousands of dot products at once.
  • Embedding search / RAG: the documents whose vectors have the highest dot product (or cosine) with your query vector are the ones retrieved.
  • Attention: each token's query is dot-producted with every other token's key. High score = "that token is relevant to me". This is the QK⊤QK^\top in the attention equation.

Why should I care?

As a researcher

Attention scores, similarity functions, projections and many loss functions are dot products. Reading equations like QKᵀ is reading a table of dot products.

As an engineer

Vector databases rank results by dot product or cosine similarity, and choosing between them (normalized or not) changes your search results.

Modern systems that depend on it

  • Neurons (weights · inputs)
  • Attention scores (query · key)
  • Embedding search and RAG
  • Matrix multiplication (a grid of dot products)

Historical context

Before

Similarity in classical systems was often hand-designed: keyword overlap, edit distance, custom rules for each data type.

After

Learned embeddings made the dot product a universal similarity measure: learn vectors so that the dot product means what you want.

Used today

Every forward pass of every neural network, every attention layer in every LLM, and every nearest-neighbour lookup in a vector database.

What to remember

  • a · b = sum of a[i] × b[i].
  • Positive ⇒ roughly the same direction; ~0 ⇒ unrelated (perpendicular); negative ⇒ opposite.
  • a · b = |a| |b| cos θ — it mixes direction and length.
  • Cosine similarity divides out the lengths to keep only direction.
  • In attention, score = query · key: 'how relevant is that token to me?'

Watch