Chapter 04

Introduction to Matrices

Arrays of vectors, transposition, multiplication rules, and the link to linear transformations.

In this chapter

  1. What is a Matrix?
  2. Square Matrices & the Identity
  3. Row vs Column Vectors
  4. Transposition
  5. Matrix Multiplication
  6. Matrix Rules
  7. Matrices as Linear Transformations
  8. Exercises
1

What is a Matrix?

A matrix is a rectangular array of vectors stored in row × column format. Denoted with a bold capital letter.

┌ 1 2 3 4 5 ┐ │ 1 2 3 4 5 │ M = │ 1 2 3 4 5 │ (a 4×5 matrix) └ 1 2 3 4 5 ┘

An r × c matrix has r rows and c columns. The element at row i, column j is written Mᵢⱼ.


2

Square Matrices & the Identity

A square matrix has the same number of rows and columns. Most game-math matrices are 2×2, 3×3, or 4×4.

A diagonal matrix has non-zero values only on the main diagonal (top-left to bottom-right).

The identity matrix I is a square diagonal matrix where every diagonal value is 1. Multiplying any matrix by the identity leaves it unchanged (M·I = I·M = M):

┌ 1 0 0 ┐ I₃ = │ 0 1 0 │ └ 0 0 1 ┘

3

Row vs Column Vectors

A vector can be stored as a row vector (1×n matrix) or a column vector (n×1 matrix):

Row vector: [ 1 2 3 ] ┌ 1 ┐ Column vector: │ 2 │ └ 3 ┘
The choice of row vs column vector affects the order of matrix multiplication and whether transformation matrices are stored transposed. OpenGL and GLSL use column vectors (multiply on the right: M·v). DirectX and HLSL historically use row vectors (multiply on the left: v·M). Always know which convention a codebase uses.

4

Transposition

The transpose of matrix M (written Mᵀ) is formed by swapping its rows and columns. An r×c matrix becomes a c×r matrix.

┌ 1 2 3 ┐ ┌ 1 4 7 10 ┐ M = │ 4 5 6 │ → Mᵀ = │ 2 5 8 11 │ │ 7 8 9 │ └ 3 6 9 12 ┘ └ 10 11 12 ┘ (4×3) (3×4)
For an orthonormal matrix (pure rotation), the transpose equals the inverse: Mᵀ = M⁻¹. This is a critical optimization in shaders — transposing is free (just swap row/column access), while full matrix inversion is expensive. The normal matrix used for transforming surface normals is the inverse-transpose of the model matrix.

5

Matrix Multiplication

Scalar × Matrix

Multiply every element by the scalar:

┌ M₁₁ M₁₂ ┐ ┌ k·M₁₁ k·M₁₂ ┐ k · │ M₂₁ M₂₂ │ = │ k·M₂₁ k·M₂₂ │ └ M₃₁ M₃₂ ┘ └ k·M₃₁ k·M₃₂ ┘

Matrix × Matrix

The number of columns in the first matrix must equal the rows in the second. An (r×n) matrix times an (n×c) matrix produces an (r×c) result.

For C = A·B, element Cᵢⱼ = Aᵢ · Bⱼ (dot product of row i of A with column j of B)

Example — (3×1) × (1×3) → (3×3):

┌ 1 ┐ ┌ 1 2 3 ┐ A = │ 2 │ B = [ 1 2 3 ] C = │ 2 4 6 │ └ 3 ┘ └ 3 6 9 ┘

6

Matrix Rules

Because matrix multiplication is not commutative, the order of transformations matters. "Rotate then translate" is different from "translate then rotate." This is one of the most common sources of bugs in game engine transform code.

7

Matrices as Linear Transformations

Every square matrix has a unique relationship with the basis vectors of a coordinate space. A vector v multiplied by matrix M can be interpreted as expressing v in the new coordinate system defined by the rows of M:

v = vx·i + vy·j + vz·k (standard basis) vM = vx·[M row 1] + vy·[M row 2] + vz·[M row 3]

Each row of M is a basis vector of the output space. This is why a rotation matrix's rows (or columns, depending on convention) are always the transformed X, Y, and Z axes.

M = ┌ 2 1 ┐ → p = [2, 1] (new "right" axis) └ -1 2 ┘ q = [-1, 2] (new "up" axis) Multiplying any vector by M maps it into the space defined by p and q. This particular M rotates ~26.5° CCW and scales by √5.
Visualizing a matrix as "where do the basis vectors go?" is the most powerful way to understand what a transformation does. Identity sends them to themselves. A rotation matrix sends them to rotated versions. A scale matrix stretches them along each axis.

8

Exercises

1. Matrix Multiplication

Compute AB for:

A = ┌ 1 2 ┐ B = ┌ 5 6 ┐ └ 3 4 ┘ └ 7 8 ┘
C₁₁ = 1·5 + 2·7 = 19. C₁₂ = 1·6 + 2·8 = 22. C₂₁ = 3·5 + 4·7 = 43. C₂₂ = 3·6 + 4·8 = 50. Result: [[19, 22], [43, 50]].

2. Transpose

Find Mᵀ for the matrix from exercise 1 (matrix A). Verify that (AB)ᵀ = BᵀAᵀ.

Aᵀ = [[1, 3], [2, 4]]. Computing BᵀAᵀ should give the same result as (AB)ᵀ = [[19, 43], [22, 50]].

3. Identity Check

Verify that A · I₂ = A for matrix A above.

A·I₂ = [[1·1+2·0, 1·0+2·1], [3·1+4·0, 3·0+4·1]] = [[1, 2], [3, 4]] = A. ✓

Interview Question

Why is matrix multiplication not commutative? Give a game-dev example where order matters.

Matrices encode transformations, and the geometric meaning of composing two transformations depends on order. Example: rotating an object 90° around Y then translating it 5 units along X results in a different final position than translating first and then rotating. The translation rotates with the object in the second case.

Interview Question

What does the transpose of a rotation matrix equal, and why is this useful in shaders?

For a pure rotation matrix (orthonormal), Mᵀ = M⁻¹. This means you can invert a rotation for free by just swapping rows and columns. In a vertex shader, transforming normals requires the inverse-transpose of the model matrix. If the model matrix is pure rotation+translation (no non-uniform scale), you can skip the costly inverse and just transpose the 3×3 upper-left part.
← Chapter 3 ↑ Index Chapter 5 →