Day 51 ยท Machines that move space

Matrices & Transformations

You will be able to
  • Interpret a matrix as a transformation of space and read its columns as where the basis vectors land
  • Apply rotation, scaling, and shear matrices to point clouds and predict the result before running
  • Explain why composing transformations is matrix multiplication and why order matters
  • Describe identity and inverse as "do nothing" and "undo," and name when no inverse exists
  • Time vectorized matmul against Python loops and explain why GPUs are matmul machines
Today's ~125 minutes
Spaced-rep warm-up: due flashcards (Day 50 + Week 7)10 min
ELI5 + tech read + 3B1B ch. 3โ€“5, matrix-transform visualizer25 min
Guided: point-cloud machines + layer-and-timing lab45 min
Practice: design the machine20 min
Project: gallery + notes15 min
Quiz + flashcards10 min

Builds on: Day 50 โ€” Vectors & dot products ยท Day 22 โ€” Big O โ€” counting the multiplies

The analogy

A matrix is not a spreadsheet of numbers โ€” it is a machine that moves space. Feed it any arrow (vector), and it outputs a moved arrow. One machine rotates everything 90ยฐ; another stretches everything horizontally; another squashes the whole plane onto a line. And here is the beautiful secret that makes them readable: to know everything a machine does, you only need to watch what it does to two test arrows โ€” "one step right" and "one step up." The machine's columns ARE those answers. Columns [0,1] and [โˆ’1,0]? Right went up, up went left: it is a rotation. You can read the machine's soul off its columns.

Chaining machines โ€” rotate, THEN stretch โ€” is itself a single machine, and computing it is what matrix multiplication *is*. That is why order matters: rotating then stretching is a different trip than stretching then rotating, just like putting on socks then shoes differs from shoes then socks. The identity matrix is the machine that touches nothing; an inverse is the machine that exactly undoes another โ€” and machines that flatten space (squash 2D onto a line) cannot be undone, because flattening destroys information. Hold that thought: it returns tomorrow as "rank."

Why this matters on the job

A neural network layer is literally a matrix machine: output = W @ x + b, where W is learned (Day 85). A transformer forward pass is a long chain of these machines โ€” and attention (Day 94) computes its scores as one big matmul of queries against keys, exactly the "every row shadow-tests every column" picture you build today. Matmul is such an overwhelming fraction of AI compute that an entire hardware industry (GPUs, TPUs) exists to do it fast โ€” which is why "can you vectorize this?" is a real performance question you will face in every pipeline you ship, and why today's loop-vs-matmul timing is a number worth remembering.

Watch it happen

Machines that move space โ€” read a matrix by where it sends the unit vectors

step 1 / 6
Aยทe1Aยทe2
x20
y01

The secret to reading any matrix: its COLUMNS are the destinations of the unit vectors. Matrix A sends e1=(1,0) to (2,0) and e2=(0,1) to (0,1) โ€” a 2ร— horizontal stretch.A = [[2, 0], [0, 1]] โ€” columns = where the axes land

Guided practice

guided 1

Machines that move a point cloud

25 min
  1. Run part 1: a unit square (four corners + center) is pushed through four machines: rotation(45ยฐ), scale(2, 0.5), shear, and a "flattener." For each, the corners are printed before/after. BEFORE running each, read the matrix columns aloud and predict where [1,0] and [0,1] land โ€” then check.
  2. Part 2 composes: R @ S versus S @ R applied to the same square. Confirm the corner coordinates differ โ€” order matters. Write one sentence: which corner proves it?
  3. Part 3: undo. Apply np.linalg.inv(R @ S) to the transformed square and confirm you recover the original corners (to ~1e-15). Then try to invert the flattener and watch NumPy raise LinAlgError โ€” flattening is irreversible: two corners already landed on the same line.
  4. In your notes: the columns-tell-all rule in your own words, plus why the flattener's columns give it away (they are parallel โ€” both point along one line).
๐Ÿ python โ€” editable, runs in your browser
Ctrl/โŒ˜+Enter runs ยท Tab indents ยท numpy/pandas/sklearn auto-load on import (torch and network calls need a local run)
guided 2

A layer is a matrix; a GPU is a matmul furnace

20 min
  1. Part 1 builds a tiny "sentiment layer" by hand: W has two rows โ€” a "positive direction" and a "negative direction" in the 4-dim doc space from Day 50. Push the whole 5-doc matrix through it with ONE matmul and read the two scores per doc. Each output is a dot-product alignment with one learned-direction-to-be โ€” this is a dense layer before training.
  2. Confirm the shapes narrate the story: (5,4) @ (4,2) โ†’ (5,2). Five docs, two features out.
  3. Part 2 times a (500ร—500)@(500ร—500) matmul as triple Python loop vs np.matmul. Record the ratio (expect 3โ€“4 orders of magnitude).
  4. Count the multiplies: 500ยณ = 1.25ร—10โธ โ€” and note that NumPy did them in milliseconds on a CPU. A modern GPU does tens of TERAFLOPs: this operation is what AI hardware is for. Write the measured ratio in your notes; you will quote it on Day 64.
๐Ÿ python โ€” editable, runs in your browser
Ctrl/โŒ˜+Enter runs ยท Tab indents ยท numpy/pandas/sklearn auto-load on import (torch and network calls need a local run)

On your own

Design the machine

20 min

Reverse the skill: instead of reading matrices, write them. Build each 2ร—2 machine from the columns-tell-all rule, apply it to the unit square, and verify the printed corners match your intent: (1) rotate 90ยฐ counterclockwise; (2) reflect across the y-axis; (3) project everything onto the x-axis (a deliberate flattener); (4) rotate 30ยฐ then scale ร—3 โ€” as ONE matrix built by multiplication; (5) the machine that undoes (4).

Constraints: construct every matrix by reasoning about where [1,0] and [0,1] must land โ€” no formula lookup; verify (5) with an allclose check against the original square; state which of the five machines has no inverse and how you know from its columns alone.

Hints (only if stuck): reflection sends [1,0]โ†’[โˆ’1,0] and keeps [0,1]; projection sends both basis vectors onto the x-axis โ€” parallel columns = flattened = no inverse.

Ship before you stop

Transformation gallery + layer notes

Create matrix_lab.py + matrix_notes.md in your practice repo. The script: your five practice machines plus the guided demos, each printing before/after corner tables with a one-line docstring naming the transformation. The notes file: (1) the columns-tell-all rule with your own 2ร—2 worked example; (2) the composition section โ€” R@S vs S@R corner evidence and the socks-then-shoes sentence; (3) the invertibility section โ€” which machines undo, which flatten, and how columns betray a flattener; (4) the timing table (loops vs matmul, the measured ratio) under the heading "why GPUs exist"; (5) five sentences titled "a neural layer is a matrix" mapping Y = X @ W.T + b onto today's machines โ€” to be quoted back at you on Day 85. Commit both.

Rubric โ€” check what you completed (0/6)

Common mistakes & misconceptions

  • Memorizing matmul mechanics without the transformation picture. Row-times-column is HOW; "composition of space-moving machines" is WHY. The picture is what transfers to attention and layers.
  • Assuming AB = BA. Matrix multiplication is not commutative โ€” rotate-then-scale differs from scale-then-rotate, and you proved it with corner coordinates. It IS associative, which is a different (and useful) property.
  • Reading AB as "A first, then B." Applied to a vector, (AB)x = A(Bx): B acts first. Read compositions right to left, like function application.
  • Thinking every square matrix has an inverse. Flatteners (determinant 0, parallel/dependent columns) destroy information; no machine can restore what two inputs merged into one.
  • Computing explicit inverses in numerical code. Solve the linear system instead (np.linalg.solve) โ€” cheaper and more stable. The inverse is a concept for your head, not usually a matrix for your RAM.
  • Writing loops where a matmul exists. The 1000ร—-plus ratio you measured is the argument; shaping problems AS matrix products is the core NumPy (Day 64) and PyTorch (Day 87) skill.
Knowledge check

Q1. The columns of a 2ร—2 matrix are [0, 1] and [โˆ’1, 0]. What does this machine do?

Q2. Why does a matrix with determinant 0 have no inverse?

Q3. X is a batch of 1,000 vectors (1000ร—768) and W.T is 768ร—512. What does X @ W.T compute, and why do we care that it is ONE operation?

Go deeper โ€” curated resources

video3Blue1Brown โ€” Essence of Linear Algebra, chapters 3โ€“5 (transformations, composition) โ†—35 mincourseMIT OCW 18.06 โ€” lecture 1โ€“3 (the geometry of linear equations, multiplication) โ†—30 minbookImmersive Linear Algebra โ€” ch. 6 (the matrix, interactive) โ†—20 minbookMathematics for Machine Learning โ€” ch. 2 (linear algebra) โ†—25 min
If you have a third hour
  • Determinant as area scaling โ€” det(A) is the factor by which the machine scales areas (volumes in 3D); negative means orientation flips; 0 means flattened. 3B1B chapter 6 makes it visual in ten minutes.
Done means
  • All guided machines predicted-then-verified via corner tables
  • Five designed machines pass allclose checks; undo machine works
  • Loop-vs-matmul ratio measured and recorded
  • "A neural layer is a matrix" paragraph committed
  • Quiz โ‰ฅ 2/3
How this connects

โ† Back: Every entry of a matmul is a Day 50 dot product โ€” a matrix-vector product is all rows shadow-testing one vector at once. The loop-vs-vectorized timing repeats Day 22's lesson (count the operations) at the hardware level.

Forward โ†’: Tomorrow asks what a machine does to space beyond moving it: the directions it merely stretches (eigenvectors) and its rotate-stretch-rotate anatomy (SVD) โ€” the key to LoRA on Day 128. On Day 85, Y = X@W.T + b becomes a trained layer; on Day 94, attention scores are Q @ K.T โ€” a matmul you can already read.

Unlocks: D52 Rank, Eigenvectors & SVD Intuition ยท D64 NumPy in Anger ยท D85 Neurons & Forward Pass