MFML Interactive/ Unit 3 · Analytic Geometry Checks 0/10
Unit 3 · Covers Session 3 · ZC416 MFML · Prof. Saurabh

Analytic Geometry

Units 1–2 built the board; nothing on it has a size yet. This unit installs the ruler and the protractor: norms for length, inner products for angles, distances for "how far." Then the twist — you get to design the ruler — and half of machine learning turns out to be exactly that: choosing the right way to measure similarity.

≈ 50 min read + play 7 interactive widgets · Gram–Schmidt in 3D 10 inline checks
1

The quest for "similar"

Imagine this

Netflix wants to recommend your next film. Behind the screen, you are a vector; every movie is a vector; and the recommender's whole job is to find movies whose vectors are close to yours. Spotify does it with songs, search engines with documents, LLMs with meanings. One question runs the show: what exactly does "close" mean?

Your school reflex says: straight-line (Euclidean) distance. Perfectly valid — and just one option among many. If "director" matters more than "runtime" when comparing movies, you'd want a measure that weights the director dimension more heavily. The standard ruler isn't wrong; it's merely default. This unit is about building custom rulers — safely, so that geometry never breaks.

The route: ① pin down what any notion of length must satisfy (norms); ② reverse-engineer the dot product into a blueprint and generalize it (inner products); ③ meet the engine that powers every legal ruler (symmetric positive-definite matrices); ④ collect the payoff — distances, angles, orthogonality, and the cleanest coordinate systems in mathematics (orthonormal bases, via Gram–Schmidt).

2

Norms — what "length" is allowed to mean

A norm is any function :VR\|\cdot\|:V\to\mathbb{R} that behaves the way length must. Three non-negotiables: absolute homogeneity λx=λx\|\lambda\mathbf{x}\|=|\lambda|\,\|\mathbf{x}\| (double the vector, double the length — flipping sign changes nothing); the triangle inequality x+yx+y\|\mathbf{x}+\mathbf{y}\|\le\|\mathbf{x}\|+\|\mathbf{y}\| (no detour is shorter than the direct route); and positive definiteness x0\|\mathbf{x}\|\ge 0, with x=0\|\mathbf{x}\|=0 only for x=0\mathbf{x}=\mathbf{0} (nothing real has negative size; only nothing has size zero).

Two rulers you already own:

x1=i=1nxi(Manhattan)x2=i=1nxi2(Euclidean)\|\mathbf{x}\|_1=\sum_{i=1}^{n}|x_i| \quad\text{(Manhattan)}\qquad\qquad \|\mathbf{x}\|_2=\sqrt{\sum_{i=1}^{n}x_i^2}\quad\text{(Euclidean)}
Two rulers, one citythe crow flies ℓ₂ · the taxi drives ℓ₁
3
4
‖x‖₂ (crow) = 5 ‖x‖₁ (taxi) = 7

The dashed shapes are each norm's unit ball — "everything of length 1." The crow's is a circle; the taxi's is a diamond, because trading x-blocks for y-blocks costs the same. The shape of a norm's unit ball is its fingerprint — hold that thought for Section 4, where we'll bend the circle into an ellipse.

Pause & predict

Define f(x)f(\mathbf{x}) = the number of non-zero entries of x\mathbf{x} (ML calls it "ℓ₀"). Is it a norm?

Pause & predict

A taxi drives from the origin to the point (3,4)(3,4) on a perfect street grid. How far does the meter run — and how far did the crow fly?

3

Inner products — the blueprint under the dot product

The dot product xTy=ixiyi\mathbf{x}^T\mathbf{y}=\sum_i x_iy_i looks like mere arithmetic, but it's secretly an alignment meter: positive when vectors point the same general way, negative when opposed, zero when perpendicular. Take it apart and exactly three load-bearing properties appear. Bilinearity: linear in each slot — Ω(λx+ψy,z)=λΩ(x,z)+ψΩ(y,z)\Omega(\lambda\mathbf{x}+\psi\mathbf{y},\mathbf{z})=\lambda\,\Omega(\mathbf{x},\mathbf{z})+\psi\,\Omega(\mathbf{y},\mathbf{z}), and the same in the second argument. Symmetry: Ω(x,y)=Ω(y,x)\Omega(\mathbf{x},\mathbf{y})=\Omega(\mathbf{y},\mathbf{x}) — x's similarity to y is y's similarity to x. Positive definiteness: Ω(x,x)>0\Omega(\mathbf{x},\mathbf{x})>0 for every x0\mathbf{x}\neq\mathbf{0} — self-similarity is positive, because it's about to play the role of squared length.

Aha

The creative leap of the whole unit: stop treating those three properties as observations about the dot product, and start treating them as a blueprint. ANY function ,:V×VR\langle\cdot,\cdot\rangle:V\times V\to\mathbb{R} that is bilinear, symmetric, and positive-definite is called an inner product, and the pair (V,,)(V,\langle\cdot,\cdot\rangle) an inner product space. The dot product becomes one citizen of a big family — and every member of the family will hand us lengths, distances, and angles that behave. The obvious next question: how do we manufacture new members?

Pause & predict

Define Ω(x,y)=xT ⁣(1001) ⁣y\Omega(\mathbf{x},\mathbf{y})=\mathbf{x}^T\!\begin{pmatrix}1&0\\0&-1\end{pmatrix}\!\mathbf{y}. It's bilinear and symmetric. Which axiom does it break?

4

The engine room — symmetric positive-definite matrices

Here's the master recipe. Notice first that the humble dot product is a matrix sandwich in disguise: xy=xTIy\mathbf{x}\cdot\mathbf{y}=\mathbf{x}^T I\,\mathbf{y} — the identity matrix is its engine. Now swap engines. The slides' theorem: on a finite-dimensional space with a chosen basis, ,\langle\cdot,\cdot\rangle is an inner product iff there is a symmetric, positive-definite (SPD) matrix AA with

x,y=x^TAy^.\langle\mathbf{x},\mathbf{y}\rangle=\hat{\mathbf{x}}^T A\,\hat{\mathbf{y}}.

Symmetric: A=ATA=A^T (inherits the symmetry axiom). Positive-definite: xTAx>0\mathbf{x}^TA\mathbf{x}>0 for all x0\mathbf{x}\neq\mathbf{0} (inherits the length axiom). Two facts fall out immediately, both from Unit 1's toolbox. SPD ⇒ full rank: xTAx>0\mathbf{x}^TA\mathbf{x}>0 for every non-zero x\mathbf{x} means no non-zero vector is allowed in the null space — the blind spot is {0}\{\mathbf{0}\}, so AA is invertible. Diagonals are positive: feed the canonical basis vector ei\mathbf{e}_i into both slots: eiTAei=Aii>0\mathbf{e}_i^TA\mathbf{e}_i=A_{ii}>0.

What does the engine do, geometrically? It reshapes space's sense of size. Watch the fingerprint — the unit ball — as you tune the engine:

The geometry lensone space, many rulers — the unit ball is the fingerprint
A = a₂₁ mirrors a₁₂
(symmetry, enforced)
det = 1 SPD

The test vectors are the slides' pair x=(1,1)\mathbf{x}=(1,1), y=(1,1)\mathbf{y}=(-1,1). Under II they're orthogonal; switch to diag(2, 1) and their inner product becomes −1, cosω=13\cos\omega=-\tfrac13orthogonality is a property of the ruler, not just the vectors. And the broken engine shows why SPD is non-negotiable: directions appear whose "squared length" is negative — the unit ball tears open into a hyperbola, and geometry stops making sense.

Pause & predict

Why must every diagonal entry AiiA_{ii} of a positive-definite matrix be strictly positive?

5

Lengths, distances — and the inequality that runs everything

Every inner product ships with a free ruler: the induced norm x=x,x\|\mathbf{x}\|=\sqrt{\langle\mathbf{x},\mathbf{x}\rangle}. (The traffic doesn't flow backwards: the Manhattan norm is a perfectly legal norm that comes from no inner product — inner-product geometry is the smoother, "round-ball" subfamily.) From the norm comes a distance: d(x,y)=xy=xy,xyd(\mathbf{x},\mathbf{y})=\|\mathbf{x}-\mathbf{y}\|=\sqrt{\langle\mathbf{x}-\mathbf{y},\,\mathbf{x}-\mathbf{y}\rangle} — with the dot product, that's the Euclidean distance. A distance function (a metric) keeps four promises: d0d\ge 0; d(x,y)=0    x=yd(\mathbf{x},\mathbf{y})=0\iff\mathbf{x}=\mathbf{y}; symmetry; and the triangle inequality d(x,z)d(x,y)+d(y,z)d(\mathbf{x},\mathbf{z})\le d(\mathbf{x},\mathbf{y})+d(\mathbf{y},\mathbf{z}).

Watch out — two gauges, opposite directions

Inner product and metric feel like twins but read in opposite directions: when x\mathbf{x} and y\mathbf{y} huddle close, the distance is small while the inner product is typically large; pull them apart and distance grows while the inner product shrinks. Recommender systems use both dialects — "minimize distance" and "maximize similarity" are the same instinct on different gauges. Don't mix them up mid-formula.

And now the inequality the whole unit leans on — Cauchy–Schwarz:

x,y    xy|\langle\mathbf{x},\mathbf{y}\rangle|\;\le\;\|\mathbf{x}\|\,\|\mathbf{y}\|

Alignment can never exceed the product of the sizes. It's the fact that makes "the angle between two vectors" a legal phrase — coming up next.

The slides' two-line proof — one clever vector does all the work

Take any α\alpha and look at the vector uαv\mathbf{u}-\alpha\mathbf{v}. Its squared length can't be negative: uαv2=uTu2αuTv+α2vTv0\|\mathbf{u}-\alpha\mathbf{v}\|^2=\mathbf{u}^T\mathbf{u}-2\alpha\,\mathbf{u}^T\mathbf{v}+\alpha^2\mathbf{v}^T\mathbf{v}\ge 0. Now make the smartest possible choice, α=uTvvTv\alpha=\frac{\mathbf{u}^T\mathbf{v}}{\mathbf{v}^T\mathbf{v}} (you'll recognize it in Section 10 as the shadow coefficient), and the inequality collapses to uTu(uTv)2vTv0\mathbf{u}^T\mathbf{u}-\frac{(\mathbf{u}^T\mathbf{v})^2}{\mathbf{v}^T\mathbf{v}}\ge 0, i.e. (uTu)(vTv)(uTv)2(\mathbf{u}^T\mathbf{u})(\mathbf{v}^T\mathbf{v})\ge(\mathbf{u}^T\mathbf{v})^2 — Cauchy–Schwarz. Nothing used the dot product specifically: the proof runs verbatim for any inner product. Geometry: the residual after removing u's shadow along v has non-negative length — that's the whole theorem.

Pause & predict

Why is Cauchy–Schwarz exactly what makes "the angle between x\mathbf{x} and y\mathbf{y}" well-defined?

6

Angles — the similarity dial

Cauchy–Schwarz says the ratio below always lands in [1,1][-1,1], so there is a unique angle ω[0,π]\omega\in[0,\pi] with

cos(ω)=x,yxy\cos(\omega)=\frac{\langle\mathbf{x},\mathbf{y}\rangle}{\|\mathbf{x}\|\,\|\mathbf{y}\|}

— and this number, cosine similarity, is ML's favorite similarity dial: +1 same direction, 0 unrelated (perpendicular), −1 opposite. It measures orientation agreement while ignoring size — which is exactly why search engines compare a three-word query to a thousand-word document with it. Two vectors with x,y=0\langle\mathbf{x},\mathbf{y}\rangle=0 are orthogonal, written xy\mathbf{x}\perp\mathbf{y} (the zero vector, ever the freeloader, is orthogonal to everything).

The similarity dialtwo vectors, one protractor, both gauges
18°
2.2
74°
1.6
⟨x,y⟩ = ω = d(x,y) =

Watch both gauges as you swing y toward x: the distance chip falls while the inner product climbs — opposite directions, same event. And notice the meter ignores the length sliders entirely: cosine similarity cares about where you point, not how far you reach.

Pause & predict

Two embedding vectors drift closer and closer together. What happens to the two gauges?

7

The high-dimension miracle

The slides end one page with a dare: pick two vectors at random in high dimensions — what happens to the angle between them? Write a small program to see… The program is written; it's below. Sample pairs of random vectors (each coordinate an independent Gaussian), compute their cosine similarity, and pile up a histogram. Then drag the dimension slider and watch a law of nature emerge.

The slides' dare, run livecos ω for 300 random pairs in ℝⁿ
2
mean |cos ω| = typical spread ≈ 1/√n =

In ℝ² random pairs land anywhere on the dial. By ℝ¹⁰⁰⁰ the histogram is a needle at zero: random directions in high dimensions are almost always almost orthogonal (spread shrinks like 1/n1/\sqrt{n}). This is why a 1000-dimensional embedding space can host millions of concepts with barely any interference — there is an absurd amount of "perpendicular room." It's also why cosine similarity scores in the 0.2–0.3 range can be hugely significant for real embeddings: against a baseline of ≈0, they're enormous.

Pause & predict

You sample two random vectors in R1000\mathbb{R}^{1000}. Their angle is almost certainly…

8

Orthogonality is in the eye of the ruler

Here's the unit's sneakiest lesson, straight from the slides. Take x=(1,1)\mathbf{x}=(1,1) and y=(1,1)\mathbf{y}=(-1,1). Under the dot product: x,y=(1)(1)+(1)(1)=0\langle\mathbf{x},\mathbf{y}\rangle=(1)(-1)+(1)(1)=0 — perfectly orthogonal. Now switch the engine to A=(2001)A=\begin{pmatrix}2&0\\0&1\end{pmatrix}:

x,yA=xTAy=2x1y1+x2y2=2+1=1,cosω=133=13.\langle\mathbf{x},\mathbf{y}\rangle_A=\mathbf{x}^TA\mathbf{y}=2x_1y_1+x_2y_2=-2+1=-1,\qquad \cos\omega=\frac{-1}{\sqrt{3}\cdot\sqrt{3}}=-\frac13.

Same two arrows, new ruler — and they are no longer perpendicular (the angle is about 109.5109.5^\circ). Nothing moved except our definition of measurement. If you haven't already, hit the diag(2, 1) preset in Section 4's lens and watch the unit circle deform while the verdict flips. The deep point: "perpendicular" is not a property of two vectors; it's a property of two vectors plus a ruler. Choose the ruler to match the problem — that's feature weighting, Mahalanobis distance, and attention temperature, all in embryo.

Pause & predict

x=(1,1)\mathbf{x}=(1,1) and y=(1,1)\mathbf{y}=(-1,1) are orthogonal under the dot product. Under ,A\langle\cdot,\cdot\rangle_A with A=diag(2,1)A=\operatorname{diag}(2,1), they are…

9

Orthonormal matrices — the motions that don't lie

A square matrix whose columns are orthonormal (mutually \perp, each of unit length) is called an orthogonal matrix, and it earns the cleanest identity in the subject:

ATA=I=AATAT=A1.A^TA=I=AA^T\qquad\Longleftrightarrow\qquad A^T=A^{-1}.

The inverse is free — just the transpose. (And if the columns are orthonormal, so are the rows: for square matrices a left inverse is automatically the right inverse — if BA=I=ACBA=I=AC then B=B(AC)=(BA)C=CB=B(AC)=(BA)C=C.) The payoff is behavioral: these transformations cannot distort measurement. Lengths survive: Ax2=(Ax)TAx=xTATAx=xTx=x2\|A\mathbf{x}\|^2=(A\mathbf{x})^TA\mathbf{x}=\mathbf{x}^TA^TA\mathbf{x}=\mathbf{x}^T\mathbf{x}=\|\mathbf{x}\|^2. Angles survive by the same cancellation inside cosω\cos\omega. They are geometry's rigid motions — rotations and reflections, like the rotation matrix (cosθsinθsinθcosθ)\begin{pmatrix}\cos\theta&-\sin\theta\\\sin\theta&\cos\theta\end{pmatrix} — and everything else (shears, stretches) is a liar that bends the ruler as it moves things.

Honest motion vs. lying motiona rotation keeps every promise; a shear breaks them all
35°
‖u‖: angle(u,v): AᵀA = I ✓

The ghost pair is the original u, v; the solid pair is after the motion. Rotation: every chip frozen — the transform is invisible to the ruler. Shear: lengths and angles drift with the slider — measurement is being bent. This is why ML treasures orthonormal maps: rotate an embedding space and every similarity score, every distance, every nearest neighbor is exactly preserved.

Pause & predict

Why does an orthonormal AA preserve every length?

10

The shadow machine — projection

Imagine this

A streetlamp shines straight down onto a road that runs along direction b\mathbf{b}. You hold a stick a\mathbf{a} at an angle. The stick's shadow on the road is the part of a\mathbf{a} that lies along b\mathbf{b}; the rest of the stick sticks straight up, perpendicular to the road. That split — "the part along, plus the part across" — is projection, and it's the single move Gram–Schmidt repeats to build perfect coordinate systems.

The formula falls out of one demand: the leftover must be orthogonal to b\mathbf{b}. Write the shadow as αb\alpha\mathbf{b} and require aαb,b=0\langle\mathbf{a}-\alpha\mathbf{b},\,\mathbf{b}\rangle=0; linearity gives α=a,bb,b\alpha=\frac{\langle\mathbf{a},\mathbf{b}\rangle}{\langle\mathbf{b},\mathbf{b}\rangle}, so

projb(a)=a,bb,bbandaprojb(a)    b.\operatorname{proj}_{\mathbf{b}}(\mathbf{a})=\frac{\langle\mathbf{a},\mathbf{b}\rangle}{\langle\mathbf{b},\mathbf{b}\rangle}\,\mathbf{b}\qquad\text{and}\qquad \mathbf{a}-\operatorname{proj}_{\mathbf{b}}(\mathbf{a})\;\perp\;\mathbf{b}.

(You met α\alpha already — it's the "smartest choice" from the Cauchy–Schwarz proof. Same coefficient, same geometry.)

The shadow machinesplit any vector into along + across
64°
2.4
12°
shadow coeff α = residual ⊥ b:

Orange on the road = the shadow (α·b). Violet = the leftover, always at a perfect right angle to the road — the square marker certifies it. Swing the stick past perpendicular and watch α go negative: the shadow falls behind the lamp post. This one picture is least squares, attention scores, and PCA's first move, all waiting to happen.

11

Gram–Schmidt — cleaning a messy basis

Unit 2 taught you to find a basis. This unit's finale upgrades it to the best kind: an orthonormal basis {e1,,en}\{\mathbf{e}_1,\dots,\mathbf{e}_n\} with ei,ej=0\langle\mathbf{e}_i,\mathbf{e}_j\rangle=0 for iji\neq j and ei,ei=1\langle\mathbf{e}_i,\mathbf{e}_i\rangle=1 — every direction perpendicular, every direction unit length. (Only orthogonal, not unit length? That's an orthogonal basis — one normalization away.) Why chase it? Because in an orthonormal basis, coordinates stop requiring equation-solving: the address of any v\mathbf{v} is just λi=v,ei\lambda_i=\langle\mathbf{v},\mathbf{e}_i\rangleread off by inner product. The canonical basis of Rn\mathbb{R}^n is one example; rotate it and you get infinitely many others.

The cleaning algorithm is the shadow machine on repeat: take your messy basis in order; normalize the first vector; from each later vector, subtract its shadows on everything already cleaned, then normalize what's left. Step through the companion's example in 3D:

Gram–Schmidt, step by step — in 3Ddrag to orbit · x₁ = (1,1,0), x₂ = (1,2,2)
Step 0/4

The final frame shows the receipt: e1,e2=16+16+0=0\langle\mathbf{e}_1,\mathbf{e}_2\rangle=-\tfrac16+\tfrac16+0=0 and both lengths exactly 1 — a perfect grid for the plane the two vectors span, built from a lopsided pair that started 4545^\circ apart.

The slides' power tool: Gram–Schmidt by Gaussian elimination (and why it works)

The course adds a beautiful industrial route. Stack the messy basis as columns of AA, then run plain Gaussian elimination on the augmented matrix [ATAAT][A^TA \mid A^T] — when the left half reaches echelon form, the rows on the right are an orthogonal basis. The slides' example: v1=(3,1)\mathbf{v}_1=(3,1), v2=(2,2)\mathbf{v}_2=(2,2) give ATA=(10888)A^TA=\begin{pmatrix}10&8\\8&8\end{pmatrix}, and elimination lands on

(10.80.30.1010.250.75)\left(\begin{array}{cc|cc}1&0.8&0.3&0.1\\0&1&-0.25&0.75\end{array}\right)

Check the right-hand rows: (0.3,0.1)(0.25,0.75)=0.075+0.075=0(0.3,0.1)\cdot(-0.25,0.75)=-0.075+0.075=0 — orthogonal (the first is v1\mathbf{v}_1 rescaled; the second is what's left of v2\mathbf{v}_2 after removing its v1\mathbf{v}_1-shadow). Normalize and you have an orthonormal basis. Why it works, in three moves: ① full column rank makes ATAA^TA positive-definite (Ax=0    ATAx=0A\mathbf{x}=\mathbf{0}\iff A^TA\mathbf{x}=\mathbf{0}, so the blind spots match and are trivial) — elimination needs no row swaps; ② elimination is multiplication by elementary matrices, i.e. ATA=LUA^TA=LU with LL lower-triangular (Unit 1's recorded elimination!), and the right half becomes QT=L1ATQ^T=L^{-1}A^T; ③ then QTQ=L1(ATA)LT=ULTQ^TQ=L^{-1}(A^TA)L^{-T}=UL^{-T} is upper-triangular and symmetric — hence diagonal — which is precisely the statement that QQ's columns are orthogonal. Elimination doesn't just solve systems; run on ATAA^TA, it quietly performs Gram–Schmidt.

Pause & predict

In Gram–Schmidt, why does subtracting the shadow x2,e1e1\langle\mathbf{x}_2,\mathbf{e}_1\rangle\mathbf{e}_1 from x2\mathbf{x}_2 do the job?

12

The whole story, in five lines

① A norm is any length that scales, obeys the triangle inequality, and vanishes only at 0\mathbf{0} — the taxi's ℓ₁ and the crow's ℓ₂ both qualify. ② An inner product is the dot product's blueprint made general — bilinear, symmetric, positive-definite — and every SPD matrix AA builds one: xTAy\mathbf{x}^TA\mathbf{y}. ③ Each inner product induces a norm, a distance, and — thanks to Cauchy–Schwarz — an angle: cosω=x,y/xy\cos\omega=\langle\mathbf{x},\mathbf{y}\rangle/\|\mathbf{x}\|\|\mathbf{y}\|, ML's similarity dial. ④ Orthogonality is relative to the ruler, orthonormal matrices are the motions that preserve every measurement, and random high-dimensional vectors are nearly orthogonal by default. ⑤ Gram–Schmidt (shadow-subtraction, or elimination on [ATAAT][A^TA|A^T]) turns any basis into an orthonormal one — the cleanest grid a geometry can have.

The one thing to remember

Geometry is a choice. The inner product is the lens: pick the matrix, and you have picked what "long," "near," and "perpendicular" mean. Machine learning's obsession with similarity is exactly the craft of choosing that lens well — and Gram–Schmidt guarantees that whatever lens you choose, a perfect coordinate grid exists for it.

Next up

Unit 4 · Determinants, Eigenvalues & the Spectral Theorem

You can now measure everything. Next: the directions a matrix cannot bend — eigenvectors, the numbers that live on them, and the theorem that makes symmetric matrices the nicest citizens in linear algebra. (Being built next.)