Skip to content

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

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

LaTeX diagram

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

a2=b2+c2−2bccos⁡(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=b2−x2(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, (c−x)2+h2=a2(Eq 2)\boxed{(c - x)^2 + h^2 = a^2} \qquad \text{(Eq 2)}

Substituting Eq 1 (h2=b2−x2h^2 = b^2 - x^2) into Eq 2:

a2=(c−x)2+h2  ⟹  a2=(c−x)2+b2−x2a^2 = (c - x)^2 + h^2 \implies a^2 = (c - x)^2 + b^2 - x^2

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

a2=c2−2cx+x2+b2−x2a2=b2+c2−2cx\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+c2−2bccos⁡(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 u⃗−v⃗\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 u⃗−v⃗=u⃗+(−v⃗)\vec{u} - \vec{v} = \vec{u} + (-\vec{v})

LaTeX diagram

u⃗−v⃗=u⃗+(−v⃗)\vec{u} - \vec{v} = \vec{u} + (-\vec{v}) v⃗=[31],−v⃗=[−3−1]\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+⋯+unvn∥u⃗∥∥v⃗∥\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 u⃗−v⃗\vec{u} - \vec{v}, and the two sides forming θ\theta have lengths ∥u⃗∥\|\vec{u}\| and ∥v⃗∥\|\vec{v}\|. Matching to a2=b2+c2−2bccos⁡(A)a^2 = b^2 + c^2 - 2bc\cos(A): a↔∥u⃗−v⃗∥a \leftrightarrow \|\vec{u}-\vec{v}\|, b↔∥u⃗∥b \leftrightarrow \|\vec{u}\|, c↔∥v⃗∥c \leftrightarrow \|\vec{v}\|, A↔θA \leftrightarrow \theta:

∥u⃗−v⃗∥2⏟LHS=∥u⃗∥2+∥v⃗∥2−2∥u⃗∥∥v⃗∥cos⁡θ⏟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:

∥u⃗−v⃗∥2=∥[u1u2⋮un]−[v1v2⋮vn]∥2=∥[u1−v1u2−v2⋮un−vn]∥2=((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)\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:

∥u⃗∥2+∥v⃗∥2−2∥u⃗∥∥v⃗∥cos⁡θ=(u12+u22+⋯+un2) ⁣2+(v12+v22+⋯+vn2) ⁣2−2∥u⃗∥∥v⃗∥cos⁡θ=(u12+u22+⋯+un2)+(v12+v22+⋯+vn2)−2∥u⃗∥∥v⃗∥cos⁡θ\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)=−2∥u⃗∥∥v⃗∥cos⁡θ-2(u_1v_1 + u_2v_2 + \cdots + u_nv_n) = -2\|\vec{u}\|\|\vec{v}\|\cos\theta   ⟹  u1v1+u2v2+⋯+unvn=∥u⃗∥∥v⃗∥cos⁡θ\implies u_1v_1 + u_2v_2 + \cdots + u_nv_n = \|\vec{u}\|\|\vec{v}\|\cos\theta   ⟹  cos⁡θ=u1v1+u2v2+⋯+unvn∥u⃗∥∥v⃗∥\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 u⃗Tv⃗\vec{u}^T\vec{v}.

The notation makes sense: u⃗T\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⃗=[u1u2⋮un]\vec{u} = \begin{bmatrix} u_1 \\ u_2 \\ \vdots \\ u_n \end{bmatrix} and v⃗=[v1v2⋮vn]\vec{v} = \begin{bmatrix} v_1 \\ v_2 \\ \vdots \\ v_n \end{bmatrix}:

u⃗Tv⃗=[u1u2⋯un][v1v2⋮vn]:=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⁡θ=u⃗Tv⃗∥u⃗∥∥v⃗∥\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 ∥u⃗∥∥v⃗∥\|\vec{u}\|\|\vec{v}\| is always positive, and therefore the sign of u⃗Tv⃗\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 u⃗Tv⃗\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 u⃗Tv⃗\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 ∥u⃗∥∥v⃗∥\|\vec{u}\|\|\vec{v}\| is always positive:

cos⁡θ=u⃗Tv⃗∥u⃗∥∥v⃗∥  ⟹  u⃗Tv⃗ 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⁡θ=u⃗Tv⃗∥u⃗∥∥v⃗∥  ⟹  u⃗Tv⃗ 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⁡θ=u⃗Tv⃗∥u⃗∥∥v⃗∥  ⟹  u⃗Tv⃗=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⁡θ=u⃗Tv⃗∥u⃗∥∥v⃗∥  ⟹  u⃗Tv⃗ 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⁡θ=u⃗Tv⃗∥u⃗∥∥v⃗∥  ⟹  u⃗Tv⃗ 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⁡θ=u⃗Tv⃗∥u⃗∥∥v⃗∥\cos\theta = \dfrac{\vec{u}^T\vec{v}}{\|\vec{u}\|\|\vec{v}\|} and the denominator is always positive, the sign of u⃗Tv⃗\vec{u}^T\vec{v} alone tells us the relationship between the two vectors:

  • If u⃗Tv⃗>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 u⃗Tv⃗=0\vec{u}^T\vec{v} = 0, then θ=π2\theta = \tfrac{\pi}{2} — u⃗\vec{u} & v⃗\vec{v} are orthogonal (perpendicular).
  • If u⃗Tv⃗<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.