The dot product
Compute dot products, angles and cosine similarity for nonzero vectors; recognize orthogonal vectors.
It opens on
A query has counts q = (3,0). Document A has counts (4,3), and document B has counts (2,0). Which document is closer to the query in its mix of words? Make a prediction; we will check it.
Why it matters
Cosine similarity compares the mix of words in documents without automatically rewarding a document for having more words.
Good to know first
- Coordinates in a fixed order
- linear combinations
Step by step
Matched counts
Compute a dot product by multiplying matching coordinates and adding.
For q and A, the data product is 3 × 4 = 12; the model product is 0 × 3 = 0.
A common mix-up
The dot product compares directions alone.
In fact: A scores 12 against B’s 6, even though B points exactly along q.
Remove length
Find cosine similarity and the angle between two nonzero vectors.
||q|| = √(3² + 0²) = 3, ||A|| = 5, and ||B|| = √(2² + 0²) = 2, in word counts.
A common mix-up
Cosine similarity is just the dot product.
In fact: Dividing by both lengths changes the ranking here.
Right angles
Test orthogonality with a dot product and interpret zero similarity for these word counts.
The original document A is not orthogonal to q: q·A = 12 ≠ 0, and their angle is about 36.9°.
A common mix-up
A zero dot product means one vector is zero.
In fact: q and C are both nonzero, but each matched-coordinate product is zero.
Try it
Replace A with A′ = (5,3). Find q·A′.Answer
15
Hint: Multiply matching counts, then add
Replace A with A′ = (4,0). What is its cosine similarity to q?Answer
1
Hint: Find ||A′||, then divide by both lengths
Replace C with C′ = (1,5). Is C′ orthogonal to q?Answer
No; q·C′ = 3
Hint: Compute the data-with-data product first
Change only the document to B. Write the dot-product calculation for q·B.Answer
(3)(2) + (0)(0) = 6.
A’s horizontal and vertical parts have lengths 4 and 3. What is ||A||?Answer
√(4² + 3²) = 5.
