Skip to content

Lec 04-23-2026: Angles & The Dot Product (cont.)

Recall that uv=u+(v)\vec{u} - \vec{v} = \vec{u} + (-\vec{v}):

LaTeX diagram

Thm (Law of Cosines): In any triangle ABCABC, we have

a2=b2+c22bccos(A)a^2 = b^2 + c^2 - 2bc\cos(A)
Proof (acute case)

All angles are less than 90°90°. The case for an obtuse triangle is similar.

Suppose ABC\triangle ABC is an acute triangle. Drop a perpendicular from CC down to ABAB — this splits the triangle into two right triangles, letting us use the Pythagorean Theorem on each piece.

LaTeX diagram

LaTeX diagram

By the Pythagorean Theorem, x2+h2=b2    h2=b2x2(Eq 1)x^2 + h^2 = b^2 \implies \boxed{h^2 = b^2 - x^2} \qquad \text{(Eq 1)}

Again by the Pythagorean Theorem, (cx)2+h2=a2(Eq 2)\boxed{(c - x)^2 + h^2 = a^2} \qquad \text{(Eq 2)}

Substituting Eq 1 (h2=b2x2h^2 = b^2 - x^2) into Eq 2:

a2=(cx)2+h2    a2=(cx)2+b2x2a^2 = (c - x)^2 + h^2 \implies a^2 = (c - x)^2 + b^2 - x^2

Expanding (cx)2=c22cx+x2(c - x)^2 = c^2 - 2cx + x^2 and canceling the x2x^2 terms:

a2=c22cx+x2+b2x2a2=b2+c22cx\begin{align*} a^2 &= c^2 - 2cx + \cancel{x^2} + b^2 - \cancel{x^2} \\ a^2 &= b^2 + c^2 - 2cx \end{align*}

Looking back at our diagram, by SOH CAH TOA: cos(A)=xb\cos(A) = \dfrac{x}{b}, so bcos(A)=x\boxed{b\cos(A) = x}.

Substituting x=bcos(A)x = b\cos(A):

a2=b2+c22bccos(A)a^2 = b^2 + c^2 - 2bc\cos(A) \qquad \square

Let’s now link vector subtraction to the Law of Cosines!

LaTeX diagram

Layer on uv\vec{u} - \vec{v}:

LaTeX diagram

Call the angle between u\vec{u} & v\vec{v}: θ\theta, and study the lengths:

LaTeX diagram

Side Diagram: Subtraction uv=u+(v)\vec{u} - \vec{v} = \vec{u} + (-\vec{v})

LaTeX diagram

uv=u+(v)\vec{u} - \vec{v} = \vec{u} + (-\vec{v}) v=[31],v=[31]\vec{v} = \begin{bmatrix} 3 \\ 1 \end{bmatrix}, \qquad -\vec{v} = \begin{bmatrix} -3 \\ -1 \end{bmatrix}

Thm: If θ\theta is the angle between two non-zero vectors u\vec{u} and v\vec{v}, where 0θπ0 \leq \theta \leq \pi, then

cosθ=u1v1+u2v2++unvnuv\cos\theta = \frac{u_1v_1 + u_2v_2 + \cdots + u_nv_n}{\|\vec{u}\|\|\vec{v}\|}
Proof

Apply the Law of Cosines to the triangle above. The side opposite the angle θ\theta is uv\vec{u} - \vec{v}, and the two sides forming θ\theta have lengths u\|\vec{u}\| and v\|\vec{v}\|. Matching to a2=b2+c22bccos(A)a^2 = b^2 + c^2 - 2bc\cos(A): auva \leftrightarrow \|\vec{u}-\vec{v}\|, bub \leftrightarrow \|\vec{u}\|, cvc \leftrightarrow \|\vec{v}\|, AθA \leftrightarrow \theta:

uv2LHS=u2+v22uvcosθRHS\underbrace{\|\vec{u} - \vec{v}\|^2}_{\text{LHS}} = \underbrace{\|\vec{u}\|^2 + \|\vec{v}\|^2 - 2\|\vec{u}\|\|\vec{v}\|\cos\theta}_{\text{RHS}}

LHS:

uv2=[u1u2un][v1v2vn]2=[u1v1u2v2unvn]2=((u1v1)2+(u2v2)2++(unvn)2)2=(u1v1)2+(u2v2)2++(unvn)2(expanding each (uivi)2=ui22uivi+vi2)=u122u1v1+v12+u222u2v2+v22++un22unvn+vn2=(u12+u22++un2)+(v12+v22++vn2)2(u1v1+u2v2++unvn)\begin{align*} \|\vec{u} - \vec{v}\|^2 &= \left\| \begin{bmatrix} u_1 \\ u_2 \\ \vdots \\ u_n \end{bmatrix} - \begin{bmatrix} v_1 \\ v_2 \\ \vdots \\ v_n \end{bmatrix} \right\|^2 = \left\| \begin{bmatrix} u_1 - v_1 \\ u_2 - v_2 \\ \vdots \\ u_n - v_n \end{bmatrix} \right\|^2 \\ &= \left(\sqrt{(u_1-v_1)^2 + (u_2-v_2)^2 + \cdots + (u_n-v_n)^2}\right)^2 \\ &= (u_1-v_1)^2 + (u_2-v_2)^2 + \cdots + (u_n-v_n)^2 \\ &\quad\text{(expanding each } (u_i - v_i)^2 = u_i^2 - 2u_iv_i + v_i^2 \text{)} \\ &= u_1^2 - 2u_1v_1 + v_1^2 + u_2^2 - 2u_2v_2 + v_2^2 + \cdots + u_n^2 - 2u_nv_n + v_n^2 \\ &= (u_1^2 + u_2^2 + \cdots + u_n^2) + (v_1^2 + v_2^2 + \cdots + v_n^2) - 2(u_1v_1 + u_2v_2 + \cdots + u_nv_n) \end{align*}

RHS:

u2+v22uvcosθ=(u12+u22++un2) ⁣2+(v12+v22++vn2) ⁣22uvcosθ=(u12+u22++un2)+(v12+v22++vn2)2uvcosθ\begin{align*} \|\vec{u}\|^2 + \|\vec{v}\|^2 - 2\|\vec{u}\|\|\vec{v}\|\cos\theta &= \left(\sqrt{u_1^2 + u_2^2 + \cdots + u_n^2}\right)^{\!2} + \left(\sqrt{v_1^2 + v_2^2 + \cdots + v_n^2}\right)^{\!2} - 2\|\vec{u}\|\|\vec{v}\|\cos\theta \\ &= (u_1^2 + u_2^2 + \cdots + u_n^2) + (v_1^2 + v_2^2 + \cdots + v_n^2) - 2\|\vec{u}\|\|\vec{v}\|\cos\theta \end{align*}

Setting LHS == RHS and canceling (u12++un2)+(v12++vn2)(u_1^2 + \cdots + u_n^2) + (v_1^2 + \cdots + v_n^2) from both sides:

2(u1v1+u2v2++unvn)=2uvcosθ-2(u_1v_1 + u_2v_2 + \cdots + u_nv_n) = -2\|\vec{u}\|\|\vec{v}\|\cos\theta     u1v1+u2v2++unvn=uvcosθ\implies u_1v_1 + u_2v_2 + \cdots + u_nv_n = \|\vec{u}\|\|\vec{v}\|\cos\theta     cosθ=u1v1+u2v2++unvnuv\implies \boxed{\cos\theta = \frac{u_1v_1 + u_2v_2 + \cdots + u_nv_n}{\|\vec{u}\|\|\vec{v}\|}}

The term in the numerator, u1v1+u2v2++unvnu_1v_1 + u_2v_2 + \cdots + u_nv_n, is a special quantity known as the Dot Product. We denote it by uTv\vec{u}^T\vec{v}.

The notation makes sense: uT\vec{u}^T transposes u\vec{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.

Given u=[u1u2un]\vec{u} = \begin{bmatrix} u_1 \\ u_2 \\ \vdots \\ u_n \end{bmatrix} and v=[v1v2vn]\vec{v} = \begin{bmatrix} v_1 \\ v_2 \\ \vdots \\ v_n \end{bmatrix}:

uTv=[u1u2un][v1v2vn]:=u1v1+u2v2++unvn\vec{u}^T\vec{v} = \begin{bmatrix} u_1 & u_2 & \cdots & u_n \end{bmatrix} \begin{bmatrix} v_1 \\ v_2 \\ \vdots \\ v_n \end{bmatrix} := u_1v_1 + u_2v_2 + \cdots + u_nv_n

Hence our theorem becomes:

cosθ=uTvuv\cos\theta = \frac{\vec{u}^T\vec{v}}{\|\vec{u}\|\|\vec{v}\|}

Note that u\|\vec{u}\| and v\|\vec{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 uv\|\vec{u}\|\|\vec{v}\| is always positive, and therefore the sign of uTv\vec{u}^T\vec{v} determines the sign of cosθ\cos\theta.

u=u12++un2\|\vec{u}\| = \sqrt{u_1^2 + \cdots + u_n^2} and v=v12++vn2\|\vec{v}\| = \sqrt{v_1^2 + \cdots + v_n^2}

Claim: Intuitively, the dot product uTv\vec{u}^T\vec{v} is a measure of directional similarity between u\vec{u} and v\vec{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 ??):

Collaborative recommendation rating matrix with rows A, B, C and columns The Message, Normal, APT

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\mathbb{R}^3 (one dimension per song). For the entries we know:

a=[5?4],b=[?55],c=[??5]\vec{a} = \begin{bmatrix} 5 \\ ? \\ 4 \end{bmatrix}, \quad \vec{b} = \begin{bmatrix} ? \\ 5 \\ 5 \end{bmatrix}, \quad \vec{c} = \begin{bmatrix} ? \\ ? \\ 5 \end{bmatrix}

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 (00 to π2\frac{\pi}{2}) — All positive: sin,cos,tan\sin, \cos, \tan all >0> 0
  • Q II (π2\frac{\pi}{2} to π\pi) — Sine positive: only sin>0\sin > 0, so cos<0\cos < 0
  • Q III (π\pi to 3π2\frac{3\pi}{2}) — Tangent positive: sin\sin and cos\cos both <0< 0
  • Q IV (3π2\frac{3\pi}{2} to 2π2\pi) — Cosine positive: only cos>0\cos > 0

In our cases below, θ[0,π]\theta \in [0, \pi] only covers Q I and Q II.

LaTeX diagram

Since θ[0,π]\theta \in [0, \pi], the sign of cosθ\cos\theta — and therefore the sign of uTv\vec{u}^T\vec{v} — depends on where θ\theta falls. There are 5 cases to consider:

  • θ=0\theta = 0
  • 0<θ<π20 < \theta < \dfrac{\pi}{2}
  • θ=π2\theta = \dfrac{\pi}{2}
  • π2<θ<π\dfrac{\pi}{2} < \theta < \pi
  • θ=π\theta = \pi

Case 1: If θ=0\theta = 0, then the angle between u\vec{u} & v\vec{v} is 0 — they point in exactly the same direction.

LaTeX diagram

Since cos(0)=1>0\cos(0) = 1 > 0 and the denominator uv\|\vec{u}\|\|\vec{v}\| is always positive:

cosθ=uTvuv    uTv is positive.\cos\theta = \frac{\vec{u}^T\vec{v}}{\|\vec{u}\|\|\vec{v}\|} \implies \vec{u}^T\vec{v} \text{ is positive.}

Case 2: If 0<θ<π20 < \theta < \dfrac{\pi}{2}, then the angle between u\vec{u} & v\vec{v} is less than 90°90°.

Two vectors u and v with acute angle theta between them

Since cosθ>0\cos\theta > 0 for θ(0,π2)\theta \in \left(0, \frac{\pi}{2}\right) and the denominator is always positive:

cosθ=uTvuv    uTv is positive.\cos\theta = \frac{\vec{u}^T\vec{v}}{\|\vec{u}\|\|\vec{v}\|} \implies \vec{u}^T\vec{v} \text{ is positive.}

Case 3: If θ=π2\theta = \dfrac{\pi}{2}, then the angle between u\vec{u} & v\vec{v} is 90°90° — they are orthogonal. Orthogonal is the generalization of “perpendicular” to any number of dimensions; in R2\mathbb{R}^2 and R3\mathbb{R}^3 it means exactly the same thing as perpendicular.

LaTeX diagram

Since cos ⁣(π2)=0\cos\!\left(\tfrac{\pi}{2}\right) = 0:

cosθ=uTvuv    uTv=0\cos\theta = \frac{\vec{u}^T\vec{v}}{\|\vec{u}\|\|\vec{v}\|} \implies \vec{u}^T\vec{v} = 0

Case 4: If π2<θ<π\dfrac{\pi}{2} < \theta < \pi, then u\vec{u} & v\vec{v} look like:

LaTeX diagram

So u\vec{u} & v\vec{v} are pointing in very different directions! Since cosθ<0\cos\theta < 0 for θ(π2,π)\theta \in \left(\frac{\pi}{2}, \pi\right) and the denominator is always positive:

cosθ=uTvuv    uTv is NEGATIVE.\cos\theta = \frac{\vec{u}^T\vec{v}}{\|\vec{u}\|\|\vec{v}\|} \implies \vec{u}^T\vec{v} \text{ is NEGATIVE.}

Case 5: If θ=π\theta = \pi, then u\vec{u} & v\vec{v} point in exactly opposite directions:

LaTeX diagram

Since cos(π)<0\cos(\pi) < 0 and the denominator is always positive:

cosθ=uTvuv    uTv is NEGATIVE.\cos\theta = \frac{\vec{u}^T\vec{v}}{\|\vec{u}\|\|\vec{v}\|} \implies \vec{u}^T\vec{v} \text{ is NEGATIVE.}

Since cosθ=uTvuv\cos\theta = \dfrac{\vec{u}^T\vec{v}}{\|\vec{u}\|\|\vec{v}\|} and the denominator is always positive, the sign of uTv\vec{u}^T\vec{v} alone tells us the relationship between the two vectors:

  • If uTv>0\vec{u}^T\vec{v} > 0, then θ[0,π2)\theta \in \left[0, \tfrac{\pi}{2}\right)u\vec{u} & v\vec{v} point in the same general direction.
  • If uTv=0\vec{u}^T\vec{v} = 0, then θ=π2\theta = \tfrac{\pi}{2}u\vec{u} & v\vec{v} are orthogonal (perpendicular).
  • If uTv<0\vec{u}^T\vec{v} < 0, then θ(π2,π]\theta \in \left(\tfrac{\pi}{2}, \pi\right]u\vec{u} & v\vec{v} point in opposite general directions.