ENع
←Matrices
Lesson 20 · Linear algebra and calculus

Matrices

18 min11 steps5 questionsAdvanced
About this lesson

A matrix is a grid of numbers, and it is also a machine that moves every point of space at once. Follow one example, A = [[2, −1], [1, 1]] times x = (2, 1): multiply it row by row, watch A move the plane, read its columns and its determinant, then chain it with a rotation and use it as a neural-network layer.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig 20 · Interactive
01 / 11

A grid of numbers

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.

>>> 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)
Loading figure…
or ← →
Read the lesson step by step
  1. A grid of numbers

    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.

    >>> 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)
  2. A vector is an arrow

    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.

    >>> x = np.array([2, 1])
    >>> x.shape
    (2,)
  3. Row 1 times x

    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.

    >>> A[0] @ x               # row 1 dotted with x
    np.int64(3)
    >>> 2*2 + (-1)*1
    3
  4. Row 2 times x

    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.

    >>> A @ x                  # @ multiplies matrices
    array([3, 3])
  5. A matrix moves points

    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.

    >>> A @ np.array([0, 0])   # the origin never moves
    array([0, 0])
  6. Columns are where axes land

    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.

    >>> A[:, 0], A[:, 1]        # the columns
    (array([2, 1]), array([-1,  1]))
    >>> A @ np.array([1, 0])   # e1 lands on column 1
    array([2, 1])
  7. A mix of columns

    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.

    >>> 2*A[:, 0] + 1*A[:, 1]
    array([3, 3])
  8. Determinant: area scale

    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.

    >>> np.linalg.det(A)       # a float: computed with LU
    np.float64(2.9999999999999996)
    >>> 2*1 - (-1)*1
    3
  9. Turning with a matrix

    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).

    >>> R = np.array([[0, -1], [1, 0]])
    >>> R @ (A @ x)
    array([-3,  3])
  10. Matrix times matrix

    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.

    >>> R @ A
    array([[-1, -1],
           [ 2, -1]])
    >>> (R @ A) @ x            # same as R @ (A @ x)
    array([-3,  3])
    >>> A @ R                  # the other order
    array([[-1, -2],
           [ 1, -1]])
  11. A neural-network layer

    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.

    >>> b = np.array([1, -4])
    >>> A @ x + b
    array([ 4, -1])
    >>> np.maximum(A @ x + b, 0)   # ReLU
    array([4, 0])

Quiz Unlocks after the last step.