Visual walkthrough — Vector databases and embeddings
This is the visual companion to the parent topic. If you want the same in Hinglish, see 4.4.12 Vector databases and embeddings (Hinglish).
Step 1 — What a vector actually is (an arrow with an address)
WHAT. A vector is just a list of numbers. We write . But that list has a geometric meaning: start at the origin (the point ), walk to the right, then up. The arrow from origin to where you land is the vector.
WHY we care. Embeddings (from the parent note) are vectors with hundreds of numbers. We can't draw 768 dimensions, but every idea we need already shows up in 2 dimensions, so we'll live there. The truth generalizes: nothing below secretly needs the third number.
PICTURE. Two arrows from the origin. Their lengths differ; their directions differ. Those are the only two things an arrow has, and the whole page is about separating them.
Step 2 — Length of an arrow (the norm) via one right triangle
WHAT. The length of is written (two bars, read "the norm of a"). We compute it with Pythagoras:
WHY this tool. Why Pythagoras and not something else? Because the horizontal step and the vertical step meet at a right angle — walk right, then turn 90° and walk up. Any right triangle obeys , and the hypotenuse is our arrow. So Pythagoras is not a choice; it's forced by how we built the arrow.
PICTURE. The arrow is the slanted hypotenuse; the two components are the flat and vertical legs. The little square marks the right angle.
See Vector Norms for the general -dimensional version — the rule is identical, just more squares added under the root.
Step 3 — Multiplying two arrows: the dot product
WHAT. The dot product of and is one number: Multiply matched components, then add. The result is a single scalar (plain number), not an arrow.
WHY define this at all? We are hunting for a number that says "how aligned are these two arrows?" The dot product is the simplest combination that grows when the arrows point the same way and shrinks (goes negative) when they point opposite. Step 4 will reveal exactly how it encodes the angle — that's the payoff.
PICTURE. Line up the two lists, multiply straight down each column, add across.
Step 4 — The hidden angle: dot product = lengths × cosine
WHAT. There is a second, geometric face of the same dot product: Here (theta) is the angle between the two arrows, and (cosine of that angle) is a number that answers: "how much do the two directions overlap?"
WHY cosine and not the angle itself? Because we need a number, and raw degrees don't multiply cleanly with lengths. Cosine is the exact function that converts "angle between arrows" into "fraction of overlap." Watch what it does:
- Arrows pointing the same way → → (full overlap).
- Arrows at a right angle → → (no overlap).
- Arrows pointing opposite ways → → (maximal disagreement).
So slides smoothly from down to as the arrows swing apart. That is precisely the "same-meaning to opposite-meaning" scale we want for embeddings.
PICTURE. Same two arrows, now with the angle marked between them, and a dial showing at , , for the three landmark angles.
Step 5 — The problem: raw dot product is polluted by length
WHAT. Look at the geometric face again: . The number we get depends on three things: both lengths and the angle. But for meaning, we only care about the angle.
WHY this is a real problem. In the parent note: a long document about dogs and a short note about dogs should count as equally "about dogs." Same direction, different lengths. If we used raw dot product, the long document would score higher just for being long — length would drown out meaning.
PICTURE. Two arrows pointing the exact same way (angle ), one long, one short. Their dot product with a query differs wildly, even though their direction — their meaning — is identical.
Step 6 — The fix: divide out both lengths → cosine similarity
WHAT. Take the geometric identity and divide both sides by : The two length factors cancel on the right, leaving only . This survivor is cosine similarity.
WHY divide, specifically, by the product of norms? Because that is the only thing standing between us and a pure angle. The identity literally packages the answer as ; to isolate you divide by the lengths. No other operation removes both length factors at once.
PICTURE. The fraction with the two factors struck out top and bottom, leaving a clean — and the same two arrows rescaled to length so only their directions remain.
Step 7 — Every case, on one number line
WHAT. Because ranges over , cosine similarity can never escape that band. Let's nail down every region so no query ever surprises you.
| Angle | Meaning | |
|---|---|---|
| identical direction — same meaning | ||
| between and | between and | related, "leaning together" |
| orthogonal — unrelated | ||
| between and | between and | leaning apart |
| opposite direction |
WHY cover the negatives too? Embedding models can produce vectors that point away from each other; a search must know that means "actively unlike," not just "a bit less similar." Retrieval keeps the largest similarity (closest to ).
PICTURE. A single horizontal scale from to with sample arrow-pairs drawn above each region.
Step 8 — The degenerate case: the zero vector
WHAT. What if ? Then , and cosine similarity has a in the denominator — division by zero, undefined.
WHY it matters and how it happens. A zero vector is an arrow with no direction — it points nowhere, so "the angle between it and " is a meaningless question. In practice you get zero (or near-zero) vectors from empty chunks or degenerate embeddings. The picture makes the failure obvious: there is no arrow to measure an angle against.
PICTURE. A lone and, at the origin, a dot labelled "zero vector — no direction," with the undefined fraction beside it.
Step 9 — The shortcut databases use: pre-normalize, then just dot
WHAT. To normalize a vector means to shrink or stretch it to length exactly while keeping its direction: . After this, .
WHY this speeds up billions of searches. If every stored vector already has length , then in the cosine formula the denominator , so The expensive divisions vanish — the database only ever computes the cheap dot product (Step 3). Do the normalization once at insert time, reuse it for every future query.
PICTURE. Two long arrows collapsing onto the unit circle (radius-1 ring); once on the ring, only the angle between them remains, so a bare dot product already equals .
The one-picture summary
Everything above, compressed: two raw arrows → measure lengths (Step 2) → dot them (Step 3) → that dot secretly equals lengths × cosine (Step 4) → length pollutes meaning (Step 5) → divide it out (Step 6) → land on the dial (Step 7), minding the zero-vector hole (Step 8) → or just pre-normalize and dot (Step 9).
Recall Feynman: tell it back in plain words
Every sentence becomes an arrow from the centre of a map. An arrow has only two honest facts: how long it is and which way it points. For meaning, only the direction matters — a chatty long paragraph about dogs and a short line about dogs point the same way. To compare two arrows I could just multiply-and-add their numbers (the dot product), but that answer gets bigger just because an arrow is longer, which is cheating. So I divide by both arrows' lengths, and all that survives is — a single number from (same direction, same meaning) through (unrelated) down to (opposite). If an arrow has zero length it points nowhere, so the question is meaningless — I skip it. And if I shrink every arrow to length ahead of time, the dividing is already done, so plain multiply-and-add gives the cosine straight away — which is exactly the trick a vector database uses to search a billion arrows fast.
Recall Quick self-check
Why divide the dot product by both norms? ::: To cancel the two length factors in , leaving only — pure direction. What is cosine similarity of two identical-direction vectors of different length? ::: — direction is the same, length is divided out. What does mean geometrically and semantically? ::: Arrows at (orthogonal); unrelated meaning. Why can a database use a bare dot product for cosine? ::: Vectors are pre-normalized to length , so the denominator is . What breaks cosine similarity, and why? ::: A zero vector — length gives division by zero and has no direction to measure an angle from.
Connections
- Vector Databases and Embeddings — the parent topic this walkthrough serves.
- Dot Product · Vector Norms · Cosine Similarity — the three tools built above.
- HNSW and ANN Indexes — why pre-normalized dot products (Step 9) make search fast.
- Retrieval-Augmented Generation — the search this similarity powers.
- Curse of Dimensionality — what changes when grows huge.