Vectors through eigenvalues, each shown with diagrams and a running 2D game example, plus a cheat sheet and practice problems.
Core ideaLinear algebra is the math of moving lots of points at once, and a game does that every frame.
Part 1: Move one thing and aim it① Vectors ② Dot product ③ Span & independence
Part 2: Move everything at once④ Matrix ⑤ Matrix multiplication ⑥ Determinant
Part 3: Undo a move⑦ Linear systems ⑧ Inverse ⑨ Rank
Part 4: Repeat a move⑩ Eigenvectors & eigenvalues
Part 1: Moving one thing and aiming it
1Vectors
Amy wants the ship to fly in any direction and to shoot.
In plain wordsA vector is a list of numbers. On the map it is an arrow, written as (steps across, steps up). Positions, velocities and directions are all vectors.
Formulasadd: (2,1) + (3,4) = (5,5) ← part by part, tip to tail length: |v| = √(x² + y²) |(3,4)| = 5 scale: 2·(3,4) = (6,8) ← same direction, twice as long aim: target − start (14,6) − (2,1) = (12,5) unit vector: v / |v| (12,5)/13 ≈ (0.92, 0.38)
Adding: tip to tail
Speed = length; scaling
Aiming: target − ship
A vector is an arrow, written as (steps across, steps up). Add vectors by putting them tip to tail. Scaling changes the length but not the direction. To aim, subtract positions, then divide by the length to get a unit vector.
What went wrong in the gameBen fired along (14,6), which is an arrow from the origin, not from the ship. Fix: subtract the ship's position from the enemy's, then normalize. Bullet velocity = 26 × unit vector.
AI connectionEmbeddings, token representations and feature vectors are all vectors, often with thousands of numbers. Normalizing to unit length is the same step as building a cosine-similarity search.
2Dot product
Amy wants a cockpit light that turns on when an enemy is in front of the ship.
In plain wordsA dot product produces one number: how much two arrows agree on direction. Its sign tells you front (+), behind (−) or exactly beside (0).
Formulasa · b = a₁b₁ + a₂b₂ (0.6,0.8)·(12,5) = 7.2 + 4 = 11.2 a · b = |a| |b| cos θ if a is a unit vector: a · b = length of b's shadow (projection) on a
Dot product = shadow on the facing line
60° warning cone: light on when cos θ > 0.5
Multiply matching parts and add. When the facing arrow is a unit vector, the result is the length of the enemy arrow's shadow (projection) along it. Positive means in front, negative means behind, zero means at a right angle.
Two uses in the gameWarning cone: normalize both arrows, so the dot product equals cos θ, and turn the light on when it is above cos 60° = 0.5. Asteroid lighting: brightness = max(0, normal · sunDirection). This is the same line LearnOpenGL uses for diffuse lighting.
AI connectionAttention scores are dot products: QKᵀ measures how much each query agrees with each key. Cosine similarity in RAG and vector databases is the dot product of unit vectors.
3Linear combination, span, independence, basis
While docking, the ship has two fixed thrusters, A = (1,2) and B = (3,1). Can they reach any point?
In plain wordsLinear combination: a mix, some amount of A plus some amount of B. Span: every point the mixes can reach. Independent: each arrow adds a direction the others can't reach. Basis: an independent set that spans the whole space.
Key factsx·A + y·B (x, y can be fractions or negative) standard basis: (1,0), (0,1), so the point (7,5) = 7·(1,0) + 5·(0,1) dimension = number of arrows in any basis (2 for a flat map)
Independent: A, B span the whole plane
Dependent: B′ = 2A, so both lie on one line
Span is every point you can reach by mixing the arrows. Two independent arrows reach the whole plane, and any two such arrows form a basis. If one arrow is a multiple of the other, it adds no new direction and the span collapses to a line.
What went wrong in the gameBen rewired B to (2,4) = 2·A, so every mix lies on one line and the port at (7,5) can't be reached. A third thruster C = (4,3) = A + B adds nothing either, because in 2D no third arrow can add a new direction.
AI connectionSpan and independence explain redundant features (multicollinearity), the size of a model's representation space, and low-rank ideas like LoRA.
Part 2: Moving everything at once
4Matrix (linear transformation)
Amy doesn't want to draw 360 versions of the ship. Rotate one drawing instead.
In plain wordsA matrix is a grid of numbers, and its columns say where the basis arrows land. Because every point is a mix of those arrows, knowing where they land moves every point.
Formulas
a
b
c
d
x
y
= x
a
c
+ y
b
d
=
ax + by
cx + dy
rotate θ:
cos θ
−sin θ
sin θ
cos θ
quarter turn:
0
−1
1
0
double:
2
0
0
2
mirror:
−1
0
0
1
Before
After a quarter turn left
Every point is a mix of the across and up arrows, and the mix is preserved. So the whole move is fixed by where those two arrows land, and those landing spots become the matrix's columns.
Why “linear”These moves keep grid lines straight and evenly spaced and keep the origin fixed. Godot's 2D transform stores its columns as the X vector and Y vector, which are exactly these landing spots.
AI connectionA neural network layer is W x + b, so the weight matrix W is a linear transformation of the input vector.
5Matrix multiplication (composition)
The camera follows, turns and zooms, for every pixel, 60 times a second. Can the moves be combined so each point needs only one multiplication?
In plain wordsMultiplying two matrices builds the single matrix that does both moves. The order matters, and you read it right to left.
Formulaszoom · turn =
½
0
0
½
0
−1
1
0
=
0
−½
½
0
column j of AB = A × (column j of B) entry (i,j) = row i of A · column j of B (a dot product) AB ≠ BA in general
Asteroid (identity)
Amy: S·R (turn, then stretch)
Ben: R·S (stretch, then turn)
Matrix multiplication combines a chain of moves into a single matrix. Read right to left: the matrix nearest the point acts first. Changing the order gives a different matrix (AB ≠ BA in general).
Worked exampleEnemy at (12,7): follow (subtract the camera position (10,4)) gives (2,3), turn gives (−3,2), zoom gives (−1.5,1). One combined matrix does the turn and the zoom. Ben's bug: he multiplied the stretch in on the wrong side, so round asteroids came out tall instead of wide. Note: “follow” is a translation. It moves the origin, so no 2×2 matrix can do it. Engines use 3×3 or 4×4 matrices (homogeneous coordinates) so translation fits into a matrix too.
AI connectionA deep network is a chain of matrix multiplies with nonlinearities in between. Without the nonlinearities the whole chain would collapse into one matrix. GPUs exist largely to do matrix multiplication fast.
6Determinant
The ship squashes when it bumps an asteroid, but it shouldn't look like it shrinks.
In plain wordsThe determinant is one number: how much the matrix multiplies every area. A negative value means the picture is mirrored. Zero means it is flattened.
The determinant is the factor every area gets multiplied by. Because the grid stays evenly spaced, tracking the unit square is enough. A negative sign means the picture is mirrored, and zero means it is flattened.
AI connectionDeterminants appear in normalizing flows (the change-of-variables formula), Gaussian likelihoods (log det Σ), and as a quick test of whether a matrix can be inverted.
Part 3: Undoing a move
7Linear systems and Gaussian elimination
The autopilot must dock at (7,5). How long should each thruster fire?
In plain wordsWrite what you want as Ax = b, which asks “what mix of the columns lands on the target?” Then eliminate one unknown at a time and solve from the bottom row up.
Worked example
1
3
2
1
x
y
=
7
5
rows before: [1 3 | 7] and [2 1 | 5] step: R2 ← R2 − 2·R1 rows after: [1 3 | 7] and [0 −5 | −9] ⇒ y = 1.8, x = 7 − 3·1.8 = 1.6 product of the diagonal after elimination: 1·(−5) = −5 = det (elimination steps are shears, which keep area)
Row picture (seconds of A across, seconds of B up)
Column picture: 1.6·A + 1.8·B = port
One system, two views. Rows: each equation is a line, and the answer is where the lines cross. Columns: the answer tells you how much of each column to mix to land on the target.
AI connectionnp.linalg.solve calls LAPACK, which runs exactly this elimination (as an LU factorization). Linear regression's normal equations are a linear system.
8Inverse
The player clicks on the screen, but the ships live on the map. Which ship was clicked?
In plain wordsThe inverse is the move that takes everything back. A·A⁻¹ = I (the identity, which leaves every point where it is). To undo a chain of moves, undo them in reverse order.
Formulas
a
b
c
d
⁻¹ = (1 / (ad − bc)) ·
d
−b
−c
a
(swap a and d, negate b and c, divide by det) (AB)⁻¹ = B⁻¹A⁻¹ Ax = b ⇒ x = A⁻¹b e.g. T⁻¹(7,5) = (1.6, 1.8)
The inverse runs a transformation backward. It is how the game turns a mouse click on the screen into a position on the map.
What went wrong in the gameWhile boosting, the stretch was the camera's last step. Ben's code undid the turn first, so the click mapped back to (11,10), which is empty space. Undoing stretch, then zoom, then turn finds the enemy. Godot's code does the same thing: inverse(screen_transform) * mouse_pos.
Watch outThe formula divides by the determinant. If det = 0, the inverse doesn't exist. In practice, solve Ax = b directly instead of computing A⁻¹; it is faster and more stable numerically.
9Rank
The “old TV switch-off” level transition squashes the screen into a line. Ben clicks during it and the game crashes with a division by zero.
In plain wordsRank is how many directions survive the matrix. It equals the number of independent columns, which is the dimension of their span.
Rank 2: plane survives
Rank 1: flattened to a line
Rank 0: a single dot
Ben's thrusters: parallel lines, no solution
Rank is the number of directions that survive the matrix. Once a direction is flattened away, the information is lost and the move can't be undone.
For a 2×2 matrix, these four statements say the same thing (the plane gets flattened)det = 0 ⇔ columns are dependent ⇔ rank < 2 ⇔ no inverse switch-off matrix
1
0
0
0
has rank 1; Ben's thrusters
1
2
2
4
have rank 1, det = 4 − 4 = 0 The determinant can't tell rank 1 from rank 0 (both have zero area), but the rank can.
Fixif det == 0: ignore clicks, else: use the camera inverse. Godot refuses to invert a transform with determinant 0.
AI connectionLoRA learns a low-rank update (W + BA with small rank r). Rank also explains information bottlenecks, “rank collapse” in deep attention stacks, and why the SVD compresses matrices.
Part 4: Repeating a move
10Eigenvectors and eigenvalues
Amy wants a camera that follows the ship like it's on a spring. Will it settle or blow up?
Setting up the matrixEach frame: v ← −k·offset + 0.4·v (spring stiffness k, damping keeps 40%) then offset ← offset + v. So the state (offset, velocity) is multiplied by the same matrix every frame: M =
1−k
0.4
−k
0.4
with k = 0.1:
0.9
0.4
−0.1
0.4
In plain wordsAn eigenvector is a direction the matrix doesn't rotate, only stretches or shrinks. The eigenvalue λ is the stretch factor. Along an eigenvector, applying M n times just multiplies by λⁿ, with no mixing.
An eigenvector is a direction the matrix doesn't rotate, only stretches. Split the start state into eigenvector pieces, and each piece just shrinks by its own λ every frame.
Change of basisstart (−300, 0) = −100·(4,−1) + 100·(1,−1) (finding these amounts is a linear system again) after n frames: −100·0.8ⁿ·(4,−1) + 100·0.5ⁿ·(1,−1) after 10 frames: the fast piece is about 0.1 px, the slow piece is still about 43 px behind, so the largest |λ| controls how long settling takes
Offset vs frame for different stiffness
Eigenvalues predict what happens when one matrix is applied over and over, without simulating it.
The rule (spectral radius)All |λ| < 1 → the system settles. Any |λ| > 1 → it blows up. The |λ| closest to 1 sets the speed. Negative λ → the state flips sides every step. Ben's k = 3 gives λ ≈ −0.31 and −1.29, so the camera flips and lands 29% farther away every frame. Past k ≈ 0.135 the eigenvalues become complex, which shows up as wobble. k = 0.13 is near critical damping, the fastest setting with no overshoot. Above k = 2.8 one λ drops below −1 and the camera explodes.
AI connectionExploding and vanishing gradients in RNNs come from repeated multiplication by the same matrix: spectral radius above 1 explodes, below 1 vanishes. PCA uses the eigenvectors of the covariance matrix. The Hessian's eigenvalues set the stable learning rate. PageRank and Markov chains converge to the eigenvector with λ = 1.
Cheat sheet
#
Idea
Picture
Formula
Game feature
1
Vector
an arrow
add part by part, |v| = √(x²+y²), v/|v|
fly and shoot
2
Dot product
a shadow (projection)
a₁b₁ + a₂b₂ = |a||b|cos θ
warning light, lighting
3
Span / independence / basis
everywhere the arrows can reach
x·A + y·B
docking thrusters
4
Matrix
columns = where the basis arrows land
(ax+by, cx+dy)
rotate the ship
5
Matrix multiplication
a chain of moves as one move
row · column; read right to left
camera
6
Determinant
area scale factor (sign = flip)
ad − bc
squash without shrinking
7
Linear system
where lines cross / a mix of columns
Ax = b, elimination
autopilot
8
Inverse
run the move backward
(1/det)[d −b; −c a], (AB)⁻¹ = B⁻¹A⁻¹
click-to-select
9
Rank
directions that survive
number of independent columns
switch-off crash
10
Eigenvectors / eigenvalues
directions only stretched
det(M − λI) = 0, Mv = λv
spring camera
Practice (do these by hand, then check in NumPy)
Ship at (1,3), enemy at (7,11). Find the aim arrow, its length and the unit vector. (Answer: (6,8), 10, (0.6,0.8))
Facing (0,1). Is an enemy at arrow (−2,5) in front? Inside a 60° cone? (dot = 5 > 0, so in front; cos θ = 5/√29 ≈ 0.93 > 0.5, so inside)
Write the matrix that rotates by 90° to the right, then the one that rotates right and then doubles. Which order of multiplication is correct?
Find det, then the inverse, of
2
1
1
1
. Use it to solve Ax = (5,3). (det 1, x = (2,1))
Is
2
6
1
3
invertible? What is its rank? Describe its span.
Find the eigenvalues of
2
0
0
0.5
and of
0.5
0.5
0.5
0.5
. If each is applied 100 times, what happens?
Code it: np.linalg.eig(np.array([[0.9,0.4],[-0.1,0.4]])). Do you get λ = 0.8 and 0.5? (NumPy returns unit-length eigenvectors, so expect scaled versions of (4,−1) and (1,−1).)
What to learn next
Least squares (when no exact solution exists) → SVD (any matrix = rotate · stretch · rotate) → PCA (the directions where data spreads the most). All of them build on the eigenvectors above, and everything here carries over to 3D with 3 numbers per vector and 3 columns per matrix.