Thm (Law of Cosines): In any triangle ABC, we have
a2=b2+c2−2bccos(A)Proof (acute case)
All angles are less than 90°. The case for an obtuse triangle is similar.
Suppose △ABC is an acute triangle. Drop a perpendicular from C down to AB — this splits the triangle into two right triangles, letting us use the Pythagorean Theorem on each piece.
By the Pythagorean Theorem, x2+h2=b2⟹h2=b2−x2(Eq 1)
Again by the Pythagorean Theorem, (c−x)2+h2=a2(Eq 2)
Substituting Eq 1 (h2=b2−x2) into Eq 2:
a2=(c−x)2+h2⟹a2=(c−x)2+b2−x2
Expanding (c−x)2=c2−2cx+x2 and canceling the x2 terms:
a2a2=c2−2cx+x2+b2−x2=b2+c2−2cx
Looking back at our diagram, by SOH CAH TOA: cos(A)=bx, so bcos(A)=x.
Substituting x=bcos(A):
a2=b2+c2−2bccos(A)□
Let’s now link vector subtraction to the Law of Cosines!
Layer on u−v:
Call the angle between u & v: θ, and study the lengths:
Side Diagram: Subtraction u−v=u+(−v)
u−v=u+(−v)v=[31],−v=[−3−1]
Thm: If θ is the angle between two non-zero vectors u and v, where 0≤θ≤π, then
cosθ=∥u∥∥v∥u1v1+u2v2+⋯+unvnProof
Apply the Law of Cosines to the triangle above. The side opposite the angle θ is u−v, and the two sides forming θ have lengths ∥u∥ and ∥v∥. Matching to a2=b2+c2−2bccos(A): a↔∥u−v∥, b↔∥u∥, c↔∥v∥, A↔θ:
LHS∥u−v∥2=RHS∥u∥2+∥v∥2−2∥u∥∥v∥cosθ
LHS:
∥u−v∥2=u1u2⋮un−v1v2⋮vn2=u1−v1u2−v2⋮un−vn2=((u1−v1)2+(u2−v2)2+⋯+(un−vn)2)2=(u1−v1)2+(u2−v2)2+⋯+(un−vn)2(expanding each (ui−vi)2=ui2−2uivi+vi2)=u12−2u1v1+v12+u22−2u2v2+v22+⋯+un2−2unvn+vn2=(u12+u22+⋯+un2)+(v12+v22+⋯+vn2)−2(u1v1+u2v2+⋯+unvn)
The term in the numerator, u1v1+u2v2+⋯+unvn, is a special quantity known as the Dot Product. We denote it by uTv.
The notation makes sense: uT transposes u from a column into a row vector, and then multiplying a row vector by a column vector gives a single number (a scalar) by matching up and summing the products of corresponding entries.
Note that ∥u∥ and ∥v∥ are always positive for non-zero vectors - squaring each component makes it non-negative, so the square root is always positive. This means the denominator ∥u∥∥v∥ is always positive, and therefore the sign of uTv determines the sign of cosθ.
∥u∥=u12+⋯+un2 and ∥v∥=v12+⋯+vn2
Claim: Intuitively, the dot product uTv is a measure of directional similarity between u and v - it tells us how much the two vectors point in the same direction, regardless of where they start.
Song Example: Recommendation (collaborative filtering)
The Setup
Say a music app has three users, A, B, and C, who have each rated some songs on a scale of 1 to 5. The songs are The Message, Normal, and APT. Not every user has listened to every song, so some entries are unknown (marked with ?):
Each row is a user, each column is a song, and each entry is the rating that user gave that song. The ? entries are songs a user hasn’t rated yet — and those are what we want to predict.
Each user’s row of ratings is a vector in R3 (one dimension per song). For the entries we know:
a=5?4,b=?55,c=??5
The ? entries are the holes we want to fill. Notice that each row can be thought of as a vector — and if two users’ rating vectors point in a similar direction, their tastes are probably similar. This is where the dot product becomes relevant.
How do we optimize this matrix? That is to say, how do we use those directional similarities to fill in the missing entries?
Q I (0 to 2π) — All positive: sin,cos,tan all >0
Q II (2π to π) — Sine positive: only sin>0, so cos<0
Q III (π to 23π) — Tangent positive: sin and cos both <0
Q IV (23π to 2π) — Cosine positive: only cos>0
In our cases below, θ∈[0,π] only covers Q I and Q II.
Since θ∈[0,π], the sign of cosθ — and therefore the sign of uTv — depends on where θ falls. There are 5 cases to consider:
θ=0
0<θ<2π
θ=2π
2π<θ<π
θ=π
Case 1: If θ=0, then the angle between u & v is 0 — they point in exactly the same direction.
Since cos(0)=1>0 and the denominator ∥u∥∥v∥ is always positive:
cosθ=∥u∥∥v∥uTv⟹uTv is positive.
Case 2: If 0<θ<2π, then the angle between u & v is less than 90°.
Since cosθ>0 for θ∈(0,2π) and the denominator is always positive:
cosθ=∥u∥∥v∥uTv⟹uTv is positive.
Case 3: If θ=2π, then the angle between u & v is 90° — they are orthogonal. Orthogonal is the generalization of “perpendicular” to any number of dimensions; in R2 and R3 it means exactly the same thing as perpendicular.
Since cos(2π)=0:
cosθ=∥u∥∥v∥uTv⟹uTv=0
Case 4: If 2π<θ<π, then u & v look like:
So u & v are pointing in very different directions! Since cosθ<0 for θ∈(2π,π) and the denominator is always positive:
cosθ=∥u∥∥v∥uTv⟹uTv is NEGATIVE.
Case 5: If θ=π, then u & v point in exactly opposite directions:
Since cos(π)<0 and the denominator is always positive: