LINEAR ALGEBRA / 7. DOT PRODUCT & NORMS
Dot Product, Norms & Orthogonality
Measuring similarity, distance, and angles in vector space
EXPLANATION
The dot product is the most fundamental operation in ML. It appears in: • Cosine similarity in RAG retrieval • Attention scores in transformers: score = QKᵀ/√d_k • Linear layer: z = Wx + b (each row of W dots with x) • Perceptron: fires if w·x + b > 0 Dot product formula: a · b = Σ aᵢbᵢ = ||a|| · ||b|| · cos(θ) Orthogonality: a·b = 0 → vectors are perpendicular. Orthonormal: vectors are both orthogonal AND unit length. Norms measure vector size: • L1: Σ|xᵢ| → "Manhattan distance," leads to sparse solutions (Lasso) • L2: √(Σxᵢ²) → "Euclidean length," standard distance • L∞: max|xᵢ| → worst-case size Orthogonal matrices Q (where QQᵀ = I): • Columns are orthonormal vectors • ||Qx|| = ||x|| → preserves vector lengths • det(Q) = ±1 → pure rotation (or reflection) • Qᵀ = Q⁻¹ → easy to invert! Gram-Schmidt: algorithm to orthogonalize any set of vectors.
DIAGRAM
Dot product geometry:
a·b = ||a||·||b||·cos(θ)
Parallel (θ=0): a·b = ||a||·||b|| (max)
Perpendicular(θ=90°): a·b = 0
Opposite (θ=180°): a·b = -||a||·||b|| (min)
Cosine similarity in RAG:
query_vec = embed("how does RAG work?")
chunk_vec1 = embed("RAG retrieves relevant context...")
chunk_vec2 = embed("Paris is the capital of France")
sim1 = cos_sim(query, chunk1) ≈ 0.89 (relevant!)
sim2 = cos_sim(query, chunk2) ≈ 0.12 (irrelevant)
Attention: scores = Q @ Kᵀ / √d_k
Each row of scores = one query dotted with all keysCODE