TAI BUI
Back to curriculum
BuildPython, Julia

Matrix Transformations

A matrix isn't a grid of numbers sitting still. It's a set of instructions for reshaping space: rotate, stretch, tilt, flip. Learn what it does to one point and you know what it does to the whole plane.

TIME: ~75 minutesPREREQUISITES: Phase 1, Lessons 01-02 (Linear Algebra Intuition, Vectors & Matrices Operations)

Learning Objectives

  • Construct rotation, scaling, shearing, and reflection matrices and apply them to 2D and 3D points
  • Compose multiple transformations by matrix multiplication and verify that order matters
  • Compute eigenvalues and eigenvectors of 2x2 matrices from the characteristic equation
  • Explain why eigenvalues determine PCA directions, RNN stability, and spectral clustering behavior

The Problem

You read about PCA and see "find the eigenvectors of the covariance matrix." You read about model stability and see "check whether every eigenvalue has magnitude under 1." You read about data augmentation and see "apply a random rotation." None of that means anything until you can picture what a matrix actually does to space.

A matrix isn't a static grid of numbers. It's a machine: feed it a point, and it hands back a new point somewhere else in space. A rotation matrix spins points around the origin. A scaling matrix stretches or squashes them. A shearing matrix tilts them sideways. Every transformation a neural network applies to its data is one of these, or several of them stacked together. This lesson makes them concrete enough that the phrases above stop sounding like magic.

Warm-up0/3
1. A 2x2 matrix multiplies a vector with two entries. What do you get back?Warm-up
A 2 by 2 grid of letters a, b, c, d multiplied by a 2-entry list of letters x, y, with a question mark for the result, asking how many numbers come back out.
2. What are cos(90 degrees) and sin(90 degrees)?Warm-up
A unit circle with a point marked straight up at the 90 degree position, with its coordinates left as cos 90 degrees comma sin 90 degrees equals question mark.
3. What happens to a vector when you multiply it by the identity matrix?Warm-up
The identity matrix [[1, 0], [0, 1]] multiplied by a vector [4, -2], with a question mark for the result.

The Concept

Transformations as matrices

Every linear transformation in 2D reduces to a 2x2 matrix. That matrix tells you exactly where the basis vectors [1, 0] and [0, 1] land, and once you know that, everything else follows.

Mermaid diagram

Rotation

A 2D rotation by angle theta preserves distances and angles: every point just slides along a circular arc.

Mermaid diagram

Analogy: picture a clock's hands sweeping around the pivot. Every point on the hand stays exactly as far from the center as it started, it just sweeps through an angle. A 2D rotation matrix does the same thing to every point in the plane at once, which is exactly what shows up in camera and robot pose estimation, 3D rendering, and rotation-based data augmentation for vision models.

In 3D, rotation happens around an axis, and each axis gets its own rotation matrix:

Rz(theta) = | cos  -sin  0 |     Rotate around z-axis
            | sin   cos  0 |     (x-y plane spins, z stays)
            |  0     0   1 |

Rx(theta) = | 1   0     0    |   Rotate around x-axis
            | 0  cos  -sin   |   (y-z plane spins, x stays)
            | 0  sin   cos   |

Ry(theta) = |  cos  0  sin |     Rotate around y-axis
            |   0   1   0  |     (x-z plane spins, y stays)
            | -sin  0  cos |

Scaling

Scaling stretches or compresses space along each axis, independently of the others.

Mermaid diagram

Analogy: think of stretching a photo by different amounts along its width and height. Pull the horizontal handle and the image gets wider without getting taller; pull both and each direction stretches or squashes on its own. Feature normalization and the per-channel scaling inside batch norm both work exactly this way, one axis at a time.

Shearing

Shearing tilts one axis while the other stays fixed, turning rectangles into parallelograms.

Mermaid diagram

Analogy: think of a deck of cards fanned out sideways on a table. Each card's top edge slides further than its bottom edge, so a neat stack of rectangles turns into a slanted stack of parallelograms. Image augmentation pipelines and perspective or deskew correction in document scanning both apply exactly this kind of tilt.

Shear matrices:

  • Shx = [[1, k], [0, 1]] shifts x by k * y
  • Shy = [[1, 0], [k, 1]] shifts y by k * x

Reflection

Reflection mirrors points across an axis or line, the way a mirror flips left and right.

Mermaid diagram

Analogy: hold a page up to a mirror. The text is still the same size and shape, but it's flipped left to right. A reflection matrix does exactly that to every point in the plane at once, which is why horizontal-flip is one of the most common augmentations for vision models.

Reflection matrices:

  • Reflect across y-axis: [[-1, 0], [0, 1]]
  • Reflect across x-axis: [[1, 0], [0, -1]]

Composition: chaining transformations

Chaining transformation A and then B is the same as multiplying their matrices: result = B @ A @ point. Order matters here, and it's easy to get backwards: rotating first and then scaling is not the same transformation as scaling first and then rotating.

Mermaid diagram

Composed: S @ R = [[0, -2], [0.5, 0]]

Mermaid diagram

Composed: R @ S = [[0, -0.5], [2, 0]]

The results differ because matrix multiplication doesn't commute: S @ R and R @ S are, in general, two different matrices.

Analogy: think of two filters stacked on a camera lens. Light passes through the first, then the second. Swap the order and the final image changes, because the second filter now reacts to whatever the first one already did to the light, not to the original scene. Every multi-stage pipeline, from a robot's chain of coordinate-frame transforms to a stack of augmentations applied to one training image, has to get this order right.

Eigenvalues and eigenvectors

An eigenvector of a matrix is a direction the matrix only stretches or shrinks, never rotates: whatever comes out lies on the same line as the input, just longer, shorter, or flipped to the opposite direction if the eigenvalue is negative. That scale factor is the eigenvalue. Every other vector isn't so lucky; the matrix drags it off toward some new direction.

A @ v = lambda * v

v is the eigenvector (direction that survives)
lambda is the eigenvalue (how much it stretches)

Example: A = | 2  1 |
             | 1  2 |

Eigenvector [1, 1] with eigenvalue 3:
  A @ [1,1] = [3, 3] = 3 * [1, 1]     (same direction, scaled by 3)

Eigenvector [1, -1] with eigenvalue 1:
  A @ [1,-1] = [1, -1] = 1 * [1, -1]  (same direction, unchanged)

This particular matrix stretches space 3x along [1, 1] and leaves [1, -1] exactly as it was. Every other direction is just some blend of those two.

Analogy: picture pulling on two opposite corners of a stretchy rubber sheet lying flat on a table. Most drawn lines on the sheet twist as it stretches, but the line running straight between the two corners you're pulling just gets longer along that same line. It never twists.

Eigendecomposition

If a matrix has n linearly independent eigenvectors, you can decompose it entirely in terms of them:

A = V @ D @ V^(-1)

V = matrix whose columns are eigenvectors
D = diagonal matrix of eigenvalues
V^(-1) = inverse of V

This says: change into eigenvector coordinates, scale along each axis, then change back.

Why eigenvalues matter

PCA. The eigenvectors of the covariance matrix are the principal components. The eigenvalues tell you how much variance each one captures. Sort by eigenvalue, keep the top k, and that's dimensionality reduction.

Stability. In recurrent networks and other dynamical systems, eigenvalues with magnitude greater than 1 make outputs explode over time. Magnitude less than 1 makes them vanish. That's the vanishing/exploding gradient problem, stated in one sentence.

Spectral methods. Graph neural networks use eigenvalues of the adjacency matrix. Spectral clustering uses eigenvalues of the Laplacian. Either way, the eigenvectors are what expose the graph's structure.

Determinant as volume scaling factor

The determinant of a transformation matrix is a single number that tells you how much it scales area in 2D, or volume in 3D.

det = 1:   area preserved (rotation)
det = 2:   area doubled
det = 0:   space crushed to lower dimension (singular)
det = -1:  area preserved but orientation flipped (reflection)

| det(Rotation) | = 1        (always)
| det(Scale sx, sy) | = sx * sy
| det(Shear) | = 1           (area preserved)
det(Reflection) = -1        (orientation flipped)

Analogy: think of a recipe: double every ingredient and the dish comes out twice the size. That scaling factor is what the determinant measures for a matrix, applied to area or volume instead of a recipe. A negative determinant means the result also comes out mirror-flipped, not just resized. Checking whether a determinant (or, in normalizing flows, a Jacobian's determinant) is zero is exactly how you catch a transformation that has silently collapsed a dimension.

Check: The Concept0/31
1. What is a rotation matrix?Define
A flowchart: point A at (2, 1) and point B at (0, 2) pass through a Rotate 45 degrees box, landing at A prime (0.71, 2.12) and B prime (-1.41, 1.41).
Reused from the lesson's own rotation diagram.
2. What is a scaling matrix?Define
A flowchart: point A at (2, 1) and point B at (0, 2) pass through a Scale sx=2, sy=0.5 box, landing at A prime (4, 0.5) and B prime (0, 1).
Reused from the lesson's own scaling diagram.
3. Which 2x2 matrix shears space by shifting the x-coordinate in proportion to y while leaving y unchanged?Define
A unit square next to the same square sheared into a parallelogram: the bottom edge is unchanged while the top edge slides sideways, asking which matrix produces this.
4. What is the determinant of a reflection matrix, and what does it mean?Define
Two boxes, rotation with determinant plus 1 and reflection with determinant minus 1, both given, asking what the extra minus sign on reflection's determinant means.
5. What does composing two transformations mean in matrix terms?Define
A flow of boxes reading point, apply P first, apply Q second, result, asking which single matrix multiplication equals this two-step chain.
6. What is an eigenvector of a matrix?Define
The equation Mv equals lambda v, next to a sketch of two typical vectors getting dragged to new directions by M, with a side panel naming M and lambda but leaving v as a question mark.
7. What is an eigenvalue?Define
The equation Mv equals lambda v with a side panel naming M as a matrix and v as the eigenvector, leaving lambda as a question mark, asking what lambda is.
8. What does eigendecomposition write a matrix as?Define
Three boxes named V (eigenvectors as columns), D (eigenvalues on the diagonal) and V inverse (inverse of V), scattered above a question mark and the label A equals question mark, asking which combination reconstructs A.
9. What does the determinant of a 2D transformation matrix tell you geometrically?Define
A 2 by 2 matrix of plain numbers with a question mark and the label det(M) equals question mark; nothing is computed or explained.
10. Why is knowing where the basis vectors [1, 0] and [0, 1] land enough to describe a 2D linear transformation?Explain
A flowchart: e1 = [1, 0] and e2 = [0, 1] enter a box labelled M, columns are new basis vectors, and come out as e1 prime (new x-basis) and e2 prime (new y-basis).
Reused from the lesson's own basis-vector diagram.
11. Why is R @ S generally a different transformation from S @ R?Explain
Two flows of boxes: point, R, S, result 1 on one row and point, S, R, result 2 on the next, asking whether the two results are generally the same point.
12. Rotation has determinant 1 and reflection has determinant -1, though both keep area the same. What does the minus sign record?Explain
Determinant +1 for rotation and -1 for reflection, with their absolute values both equal to 1, asking what extra fact the minus sign carries beyond the area number.
13. What does a negative eigenvalue do to its eigenvector?Explain
A dashed arrow v from the origin, with the equation Mv equals lambda v and lambda stated as negative, and a question mark for where Mv lands.
14. What does A = V @ D @ V^(-1) do to a vector, step by step?Explain
A flow of boxes reading x, V inverse, question mark, D, question mark, V, result, asking what happens to the vector at each question mark stage.
15. In PCA, what do the eigenvectors of the data's covariance matrix represent?Explain
A scatter of correlated data points with its covariance matrix shown as numbers, and a question mark next to the phrase two directions through the cloud, asking what those directions represent.
16. In a recurrent network, what happens when the weight matrix has eigenvalues with magnitude above 1?Explain
A chain of repeated applications of weight matrix M to a vector, x0 then x1 then x2 and so on, with M's largest eigenvalue magnitude given as 1.3, asking what happens to the outputs after many repeats.
17. Which pairing of method and matrix is correct?Explain
Two boxes, GNNs and spectral clustering, each asking which graph matrix it uses, with adjacency matrix and Laplacian matrix listed below unmatched.
18. What does a determinant of 0 tell you about a transformation?Explain
A single box for a transformation matrix M with its determinant given as 0, asking what this says about the transformation M applies.
19. The matrix A = [[2, 1], [1, 2]] has eigenvalues 3 and 1 with eigenvectors [1, 1] and [1, -1]. What does its eigendecomposition show about A's action?Explain
The matrix A = [[2, 1], [1, 2]] with its two given eigenpairs, lambda 3 with eigenvector [1, 1] and lambda 1 with eigenvector [1, -1], asking what this says about A's action.
20. Rz(90 degrees) is applied to the 3D point (1, 2, 3). Which point comes out?Apply
The matrix Rz(90 degrees) = [[0, -1, 0], [1, 0, 0], [0, 0, 1]] next to the point (1, 2, 3), with a question mark for the result.
21. You want a 2x2 matrix that doubles a point's x-coordinate and triples its y-coordinate. Which matrix does that?Apply
A plain requirement, double the x-coordinate and triple the y-coordinate, with a question mark for the matrix, no formula given.
22. You apply [[1, 0], [0, -1]] to the point (2, 5). What comes out?Apply
The matrix [[1, 0], [0, -1]] multiplied by the point (2, 5), with a question mark for the result.
23. You compose a 30 degree rotation, a scaling by [1.5, 0.8], and a shear with kx = 0.3. What is the determinant of the composed matrix?Apply
Three named steps, rotate 30 degrees, scale by [1.5, 0.8], shear kx = 0.3, with a question mark and the label det of the composed matrix equals question mark.
24. A rotate-then-scale pipeline draws the shape wrong, though each matrix works on its own. What should you suspect first?Analyze
Two shapes labelled expected and actual, wrong, after running a rotate-then-scale pipeline whose individual matrices each test fine on their own, asking what to suspect first.
25. A recurrent network's signal shrinks toward zero over long sequences. Which property of its weight matrix points to this?Analyze
A line chart of the signal's magnitude falling toward zero over 14 steps, asking which property of the weight matrix explains a decay like this.
26. A teammate says a shear has determinant 1, so it must preserve distances between points. What is wrong with that?Analyze
A teammate's claim quoted in a box: a shear has determinant 1, so it must preserve distances between points, with the fact that the determinant really is 1 given, asking if the claim is correct.
27. np.linalg.eig on a 90 degree rotation matrix returns complex eigenvalues. What does that tell you?Analyze
The matrix rotation_90 = [[0, -1], [1, 0]] with np.linalg.eig reported as returning the complex pair 0+1j and 0-1j, asking what this tells you about rotation_90's eigenvectors.
28. You need a horizontal-flip augmentation that mirrors images left to right. Which matrix acts on (x, y) coordinates?Design
A rectangle with an L mark near its left edge, an arrow labelled mirror, and a second rectangle with the L mark now near its right edge, with a question mark, asking which matrix produces this.
29. You want one matrix that shears a point with Sh, then rotates with R, then scales with S. Which product do you write?Design
A flow of boxes reading point, Sh, R, S, result, in that physical order, asking which matrix product applied once equals this chain.
30. A matrix must stretch 3x along [1, 1] and leave [1, -1] unchanged. Which construction gives it?Design
Two dashed directions from the origin, [1, 1] marked as the goal to stretch 3 times and [1, -1] marked as the goal to leave unchanged, with a question mark, asking which construction gives a matrix with this behavior.
31. You have a covariance matrix and must reduce the data to k dimensions while keeping as much variance as possible. What do you do?Design
Three bars for a covariance matrix's eigenvalues, 0.8, 3.1 and 1.5, listed in no particular order, asking which two to keep to reduce to 2 dimensions while keeping the most variance.

Build It

Step 1: Transformation matrices from scratch (Python)

python
import math
 
def rotation_2d(theta):
    c, s = math.cos(theta), math.sin(theta)
    return [[c, -s], [s, c]]
 
def scaling_2d(sx, sy):
    return [[sx, 0], [0, sy]]
 
def shearing_2d(kx, ky):
    return [[1, kx], [ky, 1]]
 
def reflection_x():
    return [[1, 0], [0, -1]]
 
def reflection_y():
    return [[-1, 0], [0, 1]]
 
def mat_vec_mul(matrix, vector):
    return [
        sum(matrix[i][j] * vector[j] for j in range(len(vector)))
        for i in range(len(matrix))
    ]
 
def mat_mul(a, b):
    rows_a, cols_b = len(a), len(b[0])
    cols_a = len(a[0])
    return [
        [sum(a[i][k] * b[k][j] for k in range(cols_a)) for j in range(cols_b)]
        for i in range(rows_a)
    ]
 
point = [1.0, 0.0]
angle = math.pi / 4
 
rotated = mat_vec_mul(rotation_2d(angle), point)
print(f"Rotate (1,0) by 45 deg: ({rotated[0]:.4f}, {rotated[1]:.4f})")
 
scaled = mat_vec_mul(scaling_2d(2, 3), [1.0, 1.0])
print(f"Scale (1,1) by (2,3): ({scaled[0]:.1f}, {scaled[1]:.1f})")
 
sheared = mat_vec_mul(shearing_2d(1, 0), [1.0, 1.0])
print(f"Shear (1,1) kx=1: ({sheared[0]:.1f}, {sheared[1]:.1f})")
 
reflected = mat_vec_mul(reflection_y(), [2.0, 1.0])
print(f"Reflect (2,1) across y: ({reflected[0]:.1f}, {reflected[1]:.1f})")

Step 2: Composition of transformations

python
R = rotation_2d(math.pi / 2)
S = scaling_2d(2, 0.5)
 
rotate_then_scale = mat_mul(S, R)
scale_then_rotate = mat_mul(R, S)
 
point = [1.0, 0.0]
result1 = mat_vec_mul(rotate_then_scale, point)
result2 = mat_vec_mul(scale_then_rotate, point)
 
print(f"Rotate 90 then scale: ({result1[0]:.2f}, {result1[1]:.2f})")
print(f"Scale then rotate 90: ({result2[0]:.2f}, {result2[1]:.2f})")
print(f"Same? {result1 == result2}")

Step 3: Eigenvalues from scratch (2x2)

A 2x2 matrix [[a, b], [c, d]] has eigenvalues that solve the characteristic equation: lambda^2 - (a+d)*lambda + (ad - bc) = 0.

python
def eigenvalues_2x2(matrix):
    a, b = matrix[0]
    c, d = matrix[1]
    trace = a + d
    det = a * d - b * c
    discriminant = trace ** 2 - 4 * det
    if discriminant < 0:
        real = trace / 2
        imag = (-discriminant) ** 0.5 / 2
        return (complex(real, imag), complex(real, -imag))
    sqrt_disc = discriminant ** 0.5
    return ((trace + sqrt_disc) / 2, (trace - sqrt_disc) / 2)
 
def eigenvector_2x2(matrix, eigenvalue):
    a, b = matrix[0]
    c, d = matrix[1]
    if abs(b) > 1e-10:
        v = [b, eigenvalue - a]
    elif abs(c) > 1e-10:
        v = [eigenvalue - d, c]
    else:
        if abs(a - eigenvalue) < 1e-10:
            v = [1, 0]
        else:
            v = [0, 1]
    mag = (v[0] ** 2 + v[1] ** 2) ** 0.5
    return [v[0] / mag, v[1] / mag]
 
A = [[2, 1], [1, 2]]
vals = eigenvalues_2x2(A)
print(f"Matrix: {A}")
print(f"Eigenvalues: {vals[0]:.4f}, {vals[1]:.4f}")
 
for val in vals:
    vec = eigenvector_2x2(A, val)
    result = mat_vec_mul(A, vec)
    scaled = [val * vec[0], val * vec[1]]
    print(f"  lambda={val:.1f}, v={[round(x,4) for x in vec]}")
    print(f"    A@v = {[round(x,4) for x in result]}")
    print(f"    l*v = {[round(x,4) for x in scaled]}")

Step 4: Determinant as volume scaling factor

python
def det_2x2(matrix):
    return matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0]
 
print(f"det(rotation 45) = {det_2x2(rotation_2d(math.pi/4)):.4f}")
print(f"det(scale 2,3)   = {det_2x2(scaling_2d(2, 3)):.1f}")
print(f"det(shear kx=1)  = {det_2x2(shearing_2d(1, 0)):.1f}")
print(f"det(reflect y)   = {det_2x2(reflection_y()):.1f}")
 
singular = [[1, 2], [2, 4]]
print(f"det(singular)     = {det_2x2(singular):.1f}")
print("Singular: columns are proportional, space collapses to a line.")
Check: Build It0/6
1. For a 2x2 matrix [[a, b], [c, d]], which equation gives its eigenvalues?Define
A symbolic matrix with entries a, b, c, d, and the equation det of A minus lambda I equals 0, asking which expansion in terms of a, b, c, d is correct.
2. In the characteristic equation for a 2x2 matrix, what does a negative discriminant tell you?Explain
The formula discriminant equals trace squared minus 4 times determinant, with the discriminant given as negative, asking what this tells you about the matrix's eigenvalues.
3. What does the Step 1 code print for rotating the point (1, 0) by 45 degrees?Apply
The rotation_2d function's source code, with a note giving theta as 45 degrees and the point as (1.0, 0.0), asking what the rotated point prints as.
4. In Step 1, shearing_2d(1, 0) is applied to the point (1, 1). What point results?Apply
The matrix from shearing_2d(1, 0), which is [[1, 1], [0, 1]], multiplied by the point (1, 1), with a question mark for the result.
5. In Step 2, with R a 90 degree rotation and S scaling by (2, 0.5), what does mat_mul(S, R) give for the point (1, 0)?Apply
The matrices R, a 90 degree rotation, and S, a scale by (2, 0.5), with a question asking what mat_mul(S, R) applied to the point (1, 0) gives.
6. A matrix has columns [1, 2] and [2, 4], and someone wants to undo its transformation. Why can they not?Analyze
The matrix M = [[1, 2], [2, 4]], asking why a transformation using this matrix cannot be undone.

Use It

NumPy already does all of this, faster than any of the loops above, and it's what you'll actually reach for.

python
import numpy as np
 
theta = np.pi / 4
R = np.array([[np.cos(theta), -np.sin(theta)],
              [np.sin(theta),  np.cos(theta)]])
 
point = np.array([1.0, 0.0])
print(f"Rotate (1,0) by 45 deg: {R @ point}")
 
S = np.diag([2.0, 3.0])
composed = S @ R
print(f"Scale(2,3) after Rotate(45): {composed @ point}")
 
A = np.array([[2, 1], [1, 2]], dtype=float)
eigenvalues, eigenvectors = np.linalg.eig(A)
print(f"\nEigenvalues: {eigenvalues}")
print(f"Eigenvectors (columns):\n{eigenvectors}")
 
for i in range(len(eigenvalues)):
    v = eigenvectors[:, i]
    lam = eigenvalues[i]
    print(f"  A @ v{i} = {A @ v}, lambda * v{i} = {lam * v}")
 
print(f"\ndet(R) = {np.linalg.det(R):.4f}")
print(f"det(S) = {np.linalg.det(S):.1f}")
 
B = np.array([[3, 1], [0, 2]], dtype=float)
vals, vecs = np.linalg.eig(B)
D = np.diag(vals)
V = vecs
reconstructed = V @ D @ np.linalg.inv(V)
print(f"\nEigendecomposition A = V @ D @ V^-1:")
print(f"Original:\n{B}")
print(f"Reconstructed:\n{reconstructed}")

3D rotations with NumPy

python
def rotation_3d_z(theta):
    c, s = np.cos(theta), np.sin(theta)
    return np.array([[c, -s, 0], [s, c, 0], [0, 0, 1]])
 
def rotation_3d_x(theta):
    c, s = np.cos(theta), np.sin(theta)
    return np.array([[1, 0, 0], [0, c, -s], [0, s, c]])
 
point_3d = np.array([1.0, 0.0, 0.0])
rotated_z = rotation_3d_z(np.pi / 2) @ point_3d
rotated_x = rotation_3d_x(np.pi / 2) @ point_3d
 
print(f"\n3D point: {point_3d}")
print(f"Rotate 90 around z: {np.round(rotated_z, 4)}")
print(f"Rotate 90 around x: {np.round(rotated_x, 4)}")
Check: Use It0/2
1. What are the eigenvalues of B = [[3, 1], [0, 2]] from the Use It code?Apply
The matrix B = [[3, 1], [0, 2]] with a question mark and the label eigenvalues of B equals question mark, nothing computed.
2. In the 3D example, what does rotating (1, 0, 0) by 90 degrees around the x-axis give?Apply
The general Rx(theta) formula for rotating around the x-axis, with theta given as 90 degrees and the point given as (1, 0, 0), and a question mark for the result.

Ship It

This lesson lays the geometric groundwork for PCA (Phase 2) and for reading what a neural network's weights actually do to its inputs. The eigenvalue and eigenvector code you just built isn't a simplified stand-in for the real thing: it's the same algorithm running underneath dimensionality reduction, spectral clustering, and stability analysis in production systems.

Author's Note

The bug I've seen bite more people here than any other is composition order. You write rotate(scale(point)), feel good about it, and then wonder why the picture doesn't match what you expected. Function composition and matrix composition read in the same direction, right to left, innermost first, but it is easy to flip that around in your head the first dozen times you do it. When a transformed shape looks wrong, don't start by second-guessing the matrix entries. Print the point after each individual transform, one step at a time, and compare each step against what that single operation should do on its own. Nearly every "my eigendecomposition is broken" bug I've actually chased down turned out to be a composition-order mistake two steps earlier, not a problem with the eigenvectors at all.

Exercises

  1. Apply rotation, scaling, and shearing to a unit square (corners at [0,0], [1,0], [1,1], [0,1]). Print the transformed corners for each. Verify that rotation preserves distances between corners.

  2. Find the eigenvalues of the matrix [[4, 2], [1, 3]] by hand using the characteristic equation. Then verify with your from-scratch function and with NumPy.

  3. Create a composition of three transformations (rotate 30 degrees, scale by [1.5, 0.8], shear with kx=0.3) and apply it to 8 points arranged in a circle. Print before and after coordinates. Compute the determinant of the composed matrix and verify it equals the product of the individual determinants.

Key Terms

TermWhat people sayWhat it actually means
Rotation matrix"Spins things"An orthogonal matrix that moves points along circular arcs while preserving distances and angles. Determinant is always 1.
Scaling matrix"Makes things bigger"A diagonal matrix that stretches or compresses independently along each axis. Determinant is the product of scale factors.
Shearing matrix"Slants things"A matrix that shifts one coordinate proportionally to another, turning rectangles into parallelograms. Determinant is 1.
Reflection"Mirrors things"A matrix that flips space across an axis or plane. Determinant is -1.
Composition"Do two things"Multiplying transformation matrices to chain operations. Order matters: B @ A means apply A first, then B.
Eigenvector"Special direction"A direction that the matrix only scales, never rotates. The transformation's fingerprint.
Eigenvalue"How much it stretches"The scalar factor by which the matrix scales its eigenvector. Can be negative (flip) or complex (rotation).
Eigendecomposition"Break the matrix apart"Writing a matrix as V @ D @ V^(-1), separating it into its fundamental scaling directions and magnitudes.
Determinant"A single number from a matrix"The factor by which the transformation scales area (2D) or volume (3D). Zero means the transformation is irreversible.
Characteristic equation"Where eigenvalues come from"det(A - lambda * I) = 0. The polynomial whose roots are the eigenvalues.

Further Reading

Code

Ships With