WHY do we care (the AI-ML reason)? Cosine similarity, gradient descent (dot of gradient with a direction), attention scores (query·key), and projections onto principal components are all dot products. Master this and half of ML geometry unlocks.
We derive the geometric form using the Law of Cosines. Consider the triangle formed by a, b, and the vector a−b (the side connecting their tips).
Step 1 — Law of cosines. The side opposite angle θ has length:
∥a−b∥2=∥a∥2+∥b∥2−2∥a∥∥b∥cosθWhy this step? This is pure geometry — the generalization of Pythagoras when the angle isn't 90°.
Step 2 — Expand the left side algebraically. Using ∥v∥2=v⋅v:
∥a−b∥2=(a−b)⋅(a−b)=a⋅a−2a⋅b+b⋅bWhy this step? The dot product distributes just like multiplication, so we can FOIL it out.
=∥a∥2−2a⋅b+∥b∥2
Step 3 — Equate the two expressions and cancel ∥a∥2+∥b∥2:
−2a⋅b=−2∥a∥∥b∥cosθa⋅b=∥a∥∥b∥cosθWhy this step? Dividing by −2 isolates the result. The algebraic sum ∑aibi and the geometric ∥a∥∥b∥cosθ are provably identical.
Derivation. The scalar projection is the length of a in the direction of b:
compba=∥a∥cosθWhy? Basic trig: adjacent side = hypotenuse ×cos. Now substitute cosθ=∥a∥∥b∥a⋅b:
compba=∥a∥⋅∥a∥∥b∥a⋅b=∥b∥a⋅b
The vector projection points along the unit vector b^=b/∥b∥:
projba=(∥b∥a⋅b)∥b∥b=b⋅ba⋅bbWhy b⋅b? Because ∥b∥2=b⋅b — cleaner and avoids square roots.
Recall Predict before revealing (Forecast-then-Verify)
What is (1,0,0)⋅(0,1,0) and what does it tell you?
Rewrite ∥v∥2 using a dot product.
Why do we divide by b⋅b in the vector projection?
Answers: 1) 0 → orthogonal axes. 2) ∥v∥2=v⋅v. 3) Because ∥b∥2=b⋅b, and we need to scale b down to unit length twice (once for direction, once inside comp).
Recall Feynman: explain to a 12-year-old
Imagine two people pushing a shopping cart. If they push in the same direction, the cart moves fast — the dot product is big. If one pushes sideways to the other, that sideways push doesn't help at all — dot product is zero. If they push against each other, the dot product goes negative. The dot product just measures how much teamwork is in the same direction, times how hard each pushes.
Dekho, dot product basically batata hai ki do vectors kitna "same direction" mein point kar rahe hain. Do definitions hain jo actually same number dete hain: ek algebraic (a1b1+a2b2+…, yaani matching components multiply karke add karo), aur ek geometric (∥a∥∥b∥cosθ). Jab dono aligned hote hain toh cosine 1 hota hai aur value badi positive; jab perpendicular hote hain toh cos90°=0, isliye dot product zero — yeh orthogonality ka test hai. Aur agar opposite direction mein hain toh negative.
Angle nikalne ke liye formula simple hai: cosθ=∥a∥∥b∥a⋅b. ML mein isko cosine similarity bolte hain — yeh magnitude ko ignore karke sirf direction compare karta hai. Isliye do documents ya embeddings alag-alag length ke ho sakte hain, par agar direction same hai toh similarity 1 aayegi. Yahi reason hai ki hum normalize karte hain.
Projection ka intuition "shadow" wala hai: agar tum b ki line par upar se light maaro, toh a ka jo shadow banega, woh projection hai. Formula: projba=b⋅ba⋅bb. Denominator mein b⋅b isliye kyunki woh ∥b∥2 hai, aur do baar length se divide karna padta hai (ek direction ke liye, ek component ke liye).
Do galtiyan avoid karo: (1) bada dot product = zyada similar — galat, kyunki usme magnitude ghusi hoti hai, isliye cosine use karo. (2) Negative dot product ka matlab perpendicular nahi, balki obtuse angle (90° se zyada) hota hai. Perpendicular ka matlab exactly zero. Yeh concept matrix multiplication, attention scores, aur gradient descent — sab jagah kaam aata hai, isliye achhe se pakad lo.