Matrices and vectors
A matrix is a grid of numbers in rows and columns. A has 2 rows and 2 columns, so it is a 2 × 2 matrix. The entry in row i, column j is written aᵢⱼ: here a₁₁ = 2 and a₁₂ = −1.
A vector is a list of numbers, written as a column. x = (2, 1) is also an arrow from the origin: 2 steps right and 1 step up. In graphics it can be a corner of a shape.
Matrix times vector
Multiply A by x one row at a time. Pair row 1 of A with x, multiply matching entries and add: 2 × 2 + (−1) × 1 = 3. That sum is a dot product: the first entry of y = Ax.
Row 2 gives the second entry: 1 × 2 + 1 × 1 = 3, so y = Ax = (3, 3). Each row of A must be as long as x. An m × n matrix takes n numbers in and gives m out.
So a matrix is a function: vector in, vector out. On the plane, A moves the point (2, 1) to (3, 3). It moves every point of the plane at once, so it can stretch, turn, slant or flip a whole shape.
What A does to the plane
Follow the arrows e₁ = (1, 0) and e₂ = (0, 1). A sends them to its columns, (2, 1) and (−1, 1), and drags the grid along. Grid lines stay straight and evenly spaced, and the origin stays put: that is what linear means.
Columns explain it too. Ax = 2 × column 1 + 1 × column 2 = (4, 2) + (−1, 1) = (3, 3): walk column 1 twice, then column 2 once. A matrix times a vector is always a weighted mix of its columns.
The unit square on e₁ and e₂ becomes a parallelogram. Its area is the determinant: ad − bc = 2 × 1 − (−1) × 1 = 3, so A triples every area. A determinant of 0 would squash the plane onto a line.
Products at work
Graphics programs move shapes with matrices. R turns the plane a quarter turn anticlockwise: its columns, (0, 1) and (−1, 0), are where e₁ and e₂ land. Applying R after A carries (3, 3) on to (−3, 3).
A, then R, is one matrix: the product RA. Entry (i, j) is row i of R dotted with column j of A, so row 1, column 1 is 0 × 2 + (−1) × 1 = −1. Order matters: AR is a different matrix.
A neural-network layer keeps its weights in a matrix W. It computes W @ x + b, then a simple function such as ReLU, max(0, ·). With W = A and b = (1, −4), (4, −1) becomes (4, 0). Training adjusts W and b.
Try it in Python (NumPy), in order
;; step 1/11 · A grid of numbers >>> import numpy as np >>> A = np.array([[2, -1], [1, 1]]) >>> A.shape # (rows, columns) (2, 2) >>> A[0, 1] # row 1, column 2 (Python counts from 0) np.int64(-1)
An m × n matrix has m rows and n columns. It can multiply a vector with n entries and returns a vector with m entries; two matrices P and Q can be multiplied as PQ only when P has as many columns as Q has rows. np.linalg.det works with floating-point numbers (it uses an LU factorisation), so it prints 2.9999999999999996 instead of 3. Round it, or use the exact formula ad − bc for a 2 × 2 matrix.