Velamuvi AIAcademy

Lesson 9 of 16 · Transformations

Matrices as Transformations

See what a matrix really does: it moves every point in space. Learn basis vectors, why a matrix's columns show where they land, and the scaling, rotation, shear and reflection matrices, with an interactive picture.

  • 9 min read
  • Intermediate
  • Reviewed
In this lesson you will learn
  • What a linear transformation is
  • What the basis vectors î and ĵ are
  • Why a matrix's columns tell you everything it does
  • The matrices for scaling, rotation, shearing and reflection
  • Why combining transformations means multiplying matrices

A matrix moves space

So far, a matrix has been a table of numbers. Here is a second, more powerful way to see it: a matrix is a machine that moves every point in space. Feed in a vector, and out comes a new vector: y = A @ x.

Rotating, stretching, flipping and slanting are all linear transformations. Bending or shifting the origin is not.

The basis vectors

Here is the key insight. Because [x, y] = x·î + y·ĵ, once you know where a transformation sends î and ĵ, you know where it sends everything:

A @ [x, y] = x · (where î lands) + y · (where ĵ lands)

And where do they land? Look at the columns:

Try it

Move the sliders to change the four numbers of the matrix [[a, b], [c, d]]. The faint lines show the old grid, the coloured lines the transformed grid, and the orange square shows what happens to the 1 × 1 square. The arrow v = [2, 1] moves with everything else.

îĵv
[[1, 0], [0, 1]]î lands on (1, 0), ĵ lands on (0, 1)determinant: 1
Start with the identity, then try a = 2, then b = 1, then a = 0, b = -1, c = 1, d = 0.

Things to try:

  • Set a = 2: everything stretches sideways. î now lands on (2, 0).
  • Set b = 1 (from the identity): the grid slants. ĵ lands on (1, 1). This is a shear.
  • Set a = 0, b = -1, c = 1, d = 0: the whole plane turns 90° anticlockwise.
  • Set a = 1, b = 2, c = 0.5, d = 1: the grid collapses onto a line, and the square has no area left.

A catalogue of transformations

TransformationMatrixWhere î landsWhere ĵ lands
Do nothing (identity)[[1, 0], [0, 1]](1, 0)(0, 1)
Stretch x by 2[[2, 0], [0, 1]](2, 0)(0, 1)
Scale everything by 3[[3, 0], [0, 3]](3, 0)(0, 3)
Rotate 90° anticlockwise[[0, -1], [1, 0]](0, 1)(-1, 0)
Shear sideways[[1, 1], [0, 1]](1, 0)(1, 1)
Reflect in the y-axis[[-1, 0], [0, 1]](-1, 0)(0, 1)
Try it yourselfPython
import numpy as np

v = np.array([2, 1])

rotate_90 = np.array([[0, -1],
                      [1,  0]])
shear = np.array([[1, 1],
                  [0, 1]])
reflect = np.array([[-1, 0],
                    [ 0, 1]])

print("rotated:  ", rotate_90 @ v)
print("sheared:  ", shear @ v)
print("reflected:", reflect @ v)

theta = np.radians(45)
rotate_45 = np.array([[np.cos(theta), -np.sin(theta)],
                      [np.sin(theta),  np.cos(theta)]])
print("rotated 45°:", np.round(rotate_45 @ v, 3))

Transforming many points at once

Put points in the columns of a matrix, and one multiplication moves them all:

Try it yourselfPython
import numpy as np

# The corners of a unit square, one point per column
square = np.array([[0, 1, 1, 0],
                   [0, 0, 1, 1]])

stretch = np.array([[2, 0],
                    [0, 3]])

print(stretch @ square)

The square becomes a 2 × 3 rectangle. Computer graphics move millions of points this way every frame.

Combining transformations

Doing B first and then A is a single transformation: the matrix A @ B. Read right to left: the matrix nearest the vector acts first.

Try it yourselfPython
import numpy as np

rotate_90 = np.array([[0, -1], [1, 0]])
stretch_x = np.array([[2, 0], [0, 1]])
v = np.array([1, 1])

print("stretch, then rotate:", rotate_90 @ (stretch_x @ v))
print("as one matrix:       ", (rotate_90 @ stretch_x) @ v)
print("rotate, then stretch:", stretch_x @ (rotate_90 @ v))

The first two lines agree. The third, in the other order, does not: this is the picture behind "matrix multiplication is not commutative".

Common mistakes

Practice exercise

Transform a triangle in two steps, then find the single matrix that does both.

Your solutionPython
import numpy as np

# The three corners of a triangle, one point per column
triangle = np.array([[1, 3, 2],
                     [1, 1, 3]])

# 1. Rotate it 90° anticlockwise with the matrix [[0, -1], [1, 0]] and print the result.
# 2. Then reflect the rotated triangle in the y-axis with [[-1, 0], [0, 1]] and print it.
# 3. Print the single matrix that does both steps (rotation first, then reflection).

# Output must be exactly:
# [[-1 -1 -3]
#  [ 1  3  2]]
# [[1 1 3]
#  [1 3 2]]
# [[0 1]
#  [1 0]]
Knowledge check

A matrix is [[3, 1], [0, 2]]. Where does it send î = [1, 0]?

Summary

  • A matrix is a linear transformation: it moves every point while keeping grid lines straight and the origin fixed.
  • The basis vectors î and ĵ build every vector, so where they land decides everything.
  • The columns of a matrix are where the basis vectors land.
  • Scaling, rotation, shear and reflection each have a simple matrix.
  • Doing B then A is the single matrix A @ B, and order matters.

Next: using matrices to solve systems of equations.

Finished reading? Tick it off to track your progress.