Skip to content
AriadneTechnology

The Middle Ring · Chamber 5 of 9

Solving Systems: Elimination and Inverses

Gaussian elimination, pivots, inverses and determinants: when can you undo a matrix?

40 min 60 XP + 11 questions + 1 challengeMathVideoPapersProofsCodeLab

In this chamber you will

  • Solve Ax = b by Gaussian elimination
  • Decide when A⁻¹ exists and compute it
  • Read the determinant as a volume scale factor
  • See why normalising flows care about triangular Jacobians
DiscoverLearnRead beyondPapers & lecturesYour turn

Discover: a puzzle from two thousand years ago

About two thousand years ago, Chinese scholars compiled The Nine Chapters on the Mathematical Art, a handbook of practical problems. Its eighth chapter opens with one about grain. In modern words:

Three bundles of top-grade grain, two of medium grade and one of low grade yield 39 dou of grain. Two top, three medium and one low yield 34 dou. One top, two medium and three low yield 26 dou. How much does one bundle of each grade yield?

Call the yields of one bundle x1x_1 (top), x2x_2 (medium) and x3x_3 (low). The problem becomes three equations in three unknowns:

3x1+2x2+x3=392x1+3x2+x3=34x1+2x2+3x3=26\begin{aligned} 3x_1 + 2x_2 + x_3 &= 39 \\ 2x_1 + 3x_2 + x_3 &= 34 \\ x_1 + 2x_2 + 3x_3 &= 26 \end{aligned}

The scribes laid the numbers out on a counting board, one equation per column of rods, and called the grid a fangcheng, a “rectangular array”. Then they combined columns to wipe out one unknown after another. It is, step for step, the method of this chamber, written down more than fifteen centuries before Gauss was born.

You can try the key move right now.

Quick check +20 XP

In the grain problem, subtract the second equation from the first. Which equation do you get?

One subtraction and x3x_3 has vanished. Do that systematically and you can solve any system. This chamber turns the trick into an algorithm, then asks the deeper questions behind it: when does a system have exactly one answer, when can a matrix be undone, and how much does it stretch space? That last question turns out to be the engine of a family of generative models, the normalising flows, which you'll meet at the end.

DiscoverLearnRead beyondPapers & lecturesYour turn

Learn: two pictures of Ax = b

Stack the coefficients into a matrix and the grain problem becomes one equation, Ax=bA\mathbf{x} = \mathbf{b}, read “A x equals b”:

[321231123]⏟A[x1x2x3]⏟x=[393426]⏟b\underbrace{\begin{bmatrix} 3 & 2 & 1 \\ 2 & 3 & 1 \\ 1 & 2 & 3 \end{bmatrix}}_{A} \underbrace{\begin{bmatrix} x_1 \\ x_2 \\ x_3 \end{bmatrix}}_{\mathbf{x}} = \underbrace{\begin{bmatrix} 39 \\ 34 \\ 26 \end{bmatrix}}_{\mathbf{b}}

There are two ways to see what this equation asks, and you need both.

The row picture. Each equation on its own is a flat shape. In two unknowns, 2x1−x2=12x_1 - x_2 = 1 is a line in the plane; in three unknowns, each equation is a plane in space. A solution must lie on all of them at once, so solving means finding where they meet. The lines 2x1−x2=12x_1 - x_2 = 1 and x1+x2=5x_1 + x_2 = 5 cross at the single point (2,3)(2, 3).

The column picture. In Chamber 4 you read AxA\mathbf{x} as a combination of the columns of AA, weighted by the entries of x\mathbf{x}. So the same system asks: which combination of the columns makes b\mathbf{b}?

2[21]+3[−11]=[15]2\begin{bmatrix} 2 \\ 1 \end{bmatrix} + 3\begin{bmatrix} -1 \\ 1 \end{bmatrix} = \begin{bmatrix} 1 \\ 5 \end{bmatrix}

The weights 22 and 33 are the solution again, seen from the other side. The column picture also tells you when a solution exists at all: exactly when b\mathbf{b} lies in the span of the columns (Chamber 3). Chamber 6 gives that span a name.

In the row picture you can see every possible outcome with two lines. They cross once (one solution), they are parallel (no solution), or they are the same line (infinitely many). Three planes in space behave the same way: there are never exactly two solutions, or exactly five.

Quick check +20 XP

A system Ax=bA\mathbf{x} = \mathbf{b} has two different solutions, u\mathbf{u} and v\mathbf{v}. What can you conclude?

Learn: Gaussian elimination

Elimination works on the augmented matrix [A∣b][A \mid \mathbf{b}] (“A augmented with b”): the coefficients with the right-hand side attached as an extra column. It never touches the unknowns directly. It only changes rows, and it uses just three row operations:

Elimination notation
  • [A∣b][A \mid \mathbf{b}]“A augmented with b”
    The augmented matrix: the coefficients of AA with the right-hand side b\mathbf{b} attached as an extra column. The bar marks where the equals signs were.
    [2−11115]\left[\begin{array}{cc|c} 2 & -1 & 1 \\ 1 & 1 & 5 \end{array}\right]
  • RiR_i“row i”
    The ii-th row of the matrix you're working on. Row operations are written in terms of these.
    R2R_2
  • Ri↔RjR_i \leftrightarrow R_j“swap rows i and j”
    Exchange two rows. You need it when a pivot position holds a zero.
    R1↔R3R_1 \leftrightarrow R_3
  • Ri←c RiR_i \leftarrow c\,R_i“row i gets c times row i”
    Scale a row by a non-zero number cc, usually to make a pivot equal to 1.
    R3←112R3R_3 \leftarrow \tfrac{1}{12} R_3
  • Ri←Ri+c RjR_i \leftarrow R_i + c\,R_j“row i gets row i plus c times row j”
    Add a multiple of another row. The workhorse of elimination: it creates the zeros below (and above) each pivot, and it never changes the determinant.
    R2←R2−2R1R_2 \leftarrow R_2 - 2R_1
  • rref⁡(A)\operatorname{rref}(A)“r r e f of A”
    The reduced row echelon form of AA: every pivot is 1, with zeros above and below it. Every matrix has exactly one.
    rref⁡(I)=I\operatorname{rref}(I) = I

Why is this allowed? Because every row operation can be undone by another one: a swap by the same swap, Ri←c RiR_i \leftarrow c\,R_i by Ri←1cRiR_i \leftarrow \tfrac{1}{c}R_i (which is why c=0c = 0 is banned), and Ri←Ri+c RjR_i \leftarrow R_i + c\,R_j by Ri←Ri−c RjR_i \leftarrow R_i - c\,R_j. Any solution of the old system also solves the new one, and since the step can be reversed, any solution of the new one solves the old. The set of solutions never changes; only the equations get simpler.

The plan: go column by column, left to right. In each column choose a non-zero pivot, then subtract multiples of the pivot's row to create zeros below it. Here is the grain problem.

  1. 1

    Put a convenient pivot on top

    The first pivot could be the 33, but a 11 keeps the arithmetic clean. Swap rows 1 and 3:

    [321392313412326]→R1↔R3[123262313432139]\left[\begin{array}{ccc|c} 3 & 2 & 1 & 39 \\ 2 & 3 & 1 & 34 \\ 1 & 2 & 3 & 26 \end{array}\right] \xrightarrow{R_1 \leftrightarrow R_3} \left[\begin{array}{ccc|c} 1 & 2 & 3 & 26 \\ 2 & 3 & 1 & 34 \\ 3 & 2 & 1 & 39 \end{array}\right]
  2. 2

    Clear the first column below the pivot

    The multipliers are the entry to be cleared divided by the pivot: 2/12/1 and 3/13/1.

    →R3←R3−3R1R2←R2−2R1[123260−1−5−180−4−8−39]\xrightarrow[R_3 \leftarrow R_3 - 3R_1]{R_2 \leftarrow R_2 - 2R_1} \left[\begin{array}{ccc|c} 1 & 2 & 3 & 26 \\ 0 & -1 & -5 & -18 \\ 0 & -4 & -8 & -39 \end{array}\right]
  3. 3

    Move to the second column

    The second pivot is the −1-1. The multiplier for row 3 is (−4)/(−1)=4(-4)/(-1) = 4:

    →R3←R3−4R2[123260−1−5−18001233]\xrightarrow{R_3 \leftarrow R_3 - 4R_2} \left[\begin{array}{ccc|c} 1 & 2 & 3 & 26 \\ 0 & -1 & -5 & -18 \\ 0 & 0 & 12 & 33 \end{array}\right]

    The matrix is now upper triangular: zeros below the diagonal. The pivots are 11, −1-1 and 1212.

  4. 4

    Back substitution, from the bottom up

    The last row says 12x3=3312x_3 = 33, so x3=114x_3 = \tfrac{11}{4}. The middle row says −x2−5x3=−18-x_2 - 5x_3 = -18, so x2=18−554=174x_2 = 18 - \tfrac{55}{4} = \tfrac{17}{4}. The top row says x1+2x2+3x3=26x_1 + 2x_2 + 3x_3 = 26, so x1=26−344−334=374x_1 = 26 - \tfrac{34}{4} - \tfrac{33}{4} = \tfrac{37}{4}.

One bundle yields 9149\tfrac14, 4144\tfrac14 and 2342\tfrac34 dou: the answer printed in the Nine Chapters. Check it in the first equation: 3⋅374+2⋅174+114=1564=393 \cdot \tfrac{37}{4} + 2 \cdot \tfrac{17}{4} + \tfrac{11}{4} = \tfrac{156}{4} = 39.

A pivot must be non-zero, since you divide by it. When the pivot position holds a zero, swap in a row from below that has a non-zero entry there. When there is no such row, that column simply gets no pivot, and as you'll see next, that is where infinitely many solutions (or none) come from.

Learn: echelon forms and the three outcomes

Elimination aims for a staircase shape called row echelon form. A matrix is in row echelon form when

  1. any all-zero rows sit at the bottom, and
  2. each row's first non-zero entry, its pivot, is strictly to the right of the pivot in the row above.

It follows that every entry below a pivot is zero. Stopping here and back-substituting is Gaussian elimination. Keep going and you reach the reduced row echelon form, rref⁡\operatorname{rref}: in addition, every pivot equals 11 and is the only non-zero entry in its column. Clearing upwards as well as downwards like this is called Gauss–Jordan elimination. Every matrix has exactly one reduced row echelon form, however you get there, so it's a fingerprint of the system.

Once [A∣b][A \mid \mathbf{b}] is in reduced form, you can read the outcome straight off it:

[1002010−10014][130200150000][102001−100001]\left[\begin{array}{ccc|c} 1 & 0 & 0 & 2 \\ 0 & 1 & 0 & -1 \\ 0 & 0 & 1 & 4 \end{array}\right] \qquad \left[\begin{array}{ccc|c} 1 & 3 & 0 & 2 \\ 0 & 0 & 1 & 5 \\ 0 & 0 & 0 & 0 \end{array}\right] \qquad \left[\begin{array}{ccc|c} 1 & 0 & 2 & 0 \\ 0 & 1 & -1 & 0 \\ 0 & 0 & 0 & 1 \end{array}\right]
  • Left: a pivot in every column of AA. Exactly one solution, and the last column is it: x=(2,−1,4)\mathbf{x} = (2, -1, 4).
  • Middle: a column without a pivot. Column 2 has no pivot, so x2x_2 is a free variable: choose it freely, say x2=tx_2 = t, and the pivot rows fix the rest, x1=2−3tx_1 = 2 - 3t and x3=5x_3 = 5. Infinitely many solutions, x=(2,0,5)+t (−3,1,0)\mathbf{x} = (2, 0, 5) + t\,(-3, 1, 0): a line. The zero row, 0=00 = 0, is harmless.
  • Right: a pivot in the last column. The bottom row says 0x1+0x2+0x3=10x_1 + 0x_2 + 0x_3 = 1, that is, 0=10 = 1. No solution: the system is inconsistent.

That's the whole story, for any number of equations and unknowns. A pivot in the b\mathbf{b} column means none; otherwise, every free column means infinitely many; no free columns means exactly one.

Now do it yourself. The workbench records each operation in the notation above, and it recognises both echelon forms. One of the three systems has a single solution, one has infinitely many and one has none. Reduce all three to find out which is which. If you're stuck, the Hint button loads the next Gauss–Jordan step.

Interactive lab

Row reduction workbench

Each system is an augmented matrix [A | b]. Transform it with the three row operations until it reaches reduced row echelon form, then read off the answer. One system has a single solution, one has infinitely many and one has none. Which is which?
0127111621−13

x₂ + 2x₃ = 7

x₁ + x₂ + x₃ = 6

2x₁ + x₂ − x₃ = 3

Not in echelon form yet

Work column by column, left to right: get a non-zero pivot at the top, then zeros below it.

Operations so far (0)

None yet. Every operation you apply is recorded here in row notation.

Add a multiple of another row: Rᵢ ← Rᵢ + c·Rⱼ. Use a negative c to subtract.

Change row
using row

Whole numbers, fractions like 1/2 or −3/4, or decimals.

R₂ ← R₂ − 2R₁

Systems reduced: 0 / 3

Challenge: Row reducerReduce three systems to reduced row echelon form.+50 XP
Quick check +20 XP

A system in three unknowns reduces to [102301−110000].\left[\begin{array}{ccc|c} 1 & 0 & 2 & 3 \\ 0 & 1 & -1 & 1 \\ 0 & 0 & 0 & 0 \end{array}\right]. What are its solutions?

Learn: undoing a matrix

In Chamber 4, a matrix was a transformation of space. Some transformations can be reversed: rotate by 30°30° and you can rotate back. Others can't: a projection squashes the plane onto a line, and once two different points land in the same place, no transformation can tell them apart again.

A square matrix AA is invertible if there is a matrix A−1A^{-1}, read “A inverse”, with

A−1A=AA−1=I.A^{-1}A = AA^{-1} = I.

Then every system Ax=bA\mathbf{x} = \mathbf{b} has exactly one solution, x=A−1b\mathbf{x} = A^{-1}\mathbf{b}. (You'll put the proof of that together in the last part of this chamber.) A square matrix without an inverse is called singular.

Inverse and determinant notation
  • A−1A^{-1}“A inverse”
    The matrix that undoes AA: A−1A=AA−1=IA^{-1}A = AA^{-1} = I. Only square matrices can have one, and not all do. It is not the matrix of reciprocals 1/aij1/a_{ij}.
    x=A−1b\mathbf{x} = A^{-1}\mathbf{b}
  • InI_n“the n by n identity”
    Ones on the diagonal, zeros elsewhere: the matrix that changes nothing, Inx=xI_n \mathbf{x} = \mathbf{x}. Written just II when the size is clear.
    I2=[1001]I_2 = \begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix}
  • det⁡A\det A“det A”
    The determinant of a square matrix: the signed factor by which AA scales areas (in 2D) or volumes (in 3D and beyond). It is zero exactly when AA is singular.
    det⁡[abcd]=ad−bc\det\begin{bmatrix} a & b \\ c & d \end{bmatrix} = ad - bc
  • ∣A∣|A|“the determinant of A, written with bars”
    Another way to write det⁡A\det A. Careful: in ∣det⁡J∣\lvert \det J \rvert the bars mean absolute value, applied to the number det⁡J\det J.
    ∣abcd∣=ad−bc\begin{vmatrix} a & b \\ c & d \end{vmatrix} = ad - bc
  • ∂f(x)∂xT\frac{\partial f(x)}{\partial x^T}“the Jacobian of f at x”
    The matrix of partial derivatives ∂fi/∂xj\partial f_i / \partial x_j, in the notation of the Real NVP paper. Near xx, ff behaves like this matrix, so its determinant is ff's local volume scale.
    ∂y∂xT\frac{\partial y}{\partial x^T}

Computing an inverse. The jj-th column of A−1A^{-1} is the solution of Ax=ejA\mathbf{x} = \mathbf{e}_j, where ej\mathbf{e}_j is the jj-th standard basis vector. You could solve nn systems one at a time, but they share the same AA, so do them all at once: reduce the wide augmented matrix [A∣I][A \mid I]. If AA is invertible, the left half becomes II and the right half becomes A−1A^{-1}. If elimination runs out of pivots first, AA is singular.

Let's run that on a general 2×22 \times 2 matrix and watch the famous formula appear. (Here bb is just the top-right entry of the matrix, not a right-hand side.)

  1. 1

    Set up, assuming a ≠ 0 for now

    [ab10cd01]\left[\begin{array}{cc|cc} a & b & 1 & 0 \\ c & d & 0 & 1 \end{array}\right]
  2. 2

    Clear below the first pivot

    Use R2←R2−caR1R_2 \leftarrow R_2 - \tfrac{c}{a}R_1. The new (2,2)(2,2) entry is d−bca=ad−bcad - \tfrac{bc}{a} = \tfrac{ad - bc}{a}:

    [ab100ad−bca−ca1]\left[\begin{array}{cc|cc} a & b & 1 & 0 \\ 0 & \frac{ad - bc}{a} & -\frac{c}{a} & 1 \end{array}\right]

    The second pivot is ad−bca\tfrac{ad - bc}{a}. If ad−bc=0ad - bc = 0 there is no second pivot, and AA has no inverse. From here on, assume ad−bc≠0ad - bc \neq 0.

  3. 3

    Make the second pivot 1

    Scale by its reciprocal, R2←aad−bcR2R_2 \leftarrow \tfrac{a}{ad - bc}R_2:

    [ab1001−cad−bcaad−bc]\left[\begin{array}{cc|cc} a & b & 1 & 0 \\ 0 & 1 & \frac{-c}{ad - bc} & \frac{a}{ad - bc} \end{array}\right]
  4. 4

    Clear above the second pivot

    R1←R1−bR2R_1 \leftarrow R_1 - bR_2. The new top-left entry on the right is 1+bcad−bc=adad−bc1 + \tfrac{bc}{ad - bc} = \tfrac{ad}{ad - bc}:

    [a0adad−bc−abad−bc01−cad−bcaad−bc]\left[\begin{array}{cc|cc} a & 0 & \frac{ad}{ad - bc} & \frac{-ab}{ad - bc} \\ 0 & 1 & \frac{-c}{ad - bc} & \frac{a}{ad - bc} \end{array}\right]
  5. 5

    Make the first pivot 1 and read off the inverse

    R1←1aR1R_1 \leftarrow \tfrac{1}{a}R_1 turns the left half into II, and the right half is the inverse:

    [abcd]−1=1ad−bc[d−b−ca]\begin{bmatrix} a & b \\ c & d \end{bmatrix}^{-1} = \frac{1}{ad - bc}\begin{bmatrix} d & -b \\ -c & a \end{bmatrix}

Swap the diagonal, negate the off-diagonal, divide by ad−bcad - bc. (If a=0a = 0, swap the rows first; the formula comes out the same.) Always check by multiplying:

[abcd][d−b−ca]=[ad−bc00ad−bc]=(ad−bc) I.\begin{bmatrix} a & b \\ c & d \end{bmatrix}\begin{bmatrix} d & -b \\ -c & a \end{bmatrix} = \begin{bmatrix} ad - bc & 0 \\ 0 & ad - bc \end{bmatrix} = (ad - bc)\,I.

That number ad−bcad - bc decides everything: non-zero, the inverse exists; zero, it doesn't. It has a name, and a meaning, which is the subject of the next section.

Inverting a product. Here is a complete proof of a rule you'll use constantly.

Claim. If AA and BB are invertible n×nn \times n matrices, then ABAB is invertible and (AB)−1=B−1A−1(AB)^{-1} = B^{-1}A^{-1}.

Proof. To show that a matrix CC is the inverse of ABAB, it is enough to check that (AB)C=I(AB)C = I and C(AB)=IC(AB) = I: that's the definition. Take C=B−1A−1C = B^{-1}A^{-1}. Using associativity (Chamber 4) to regroup,

(AB)(B−1A−1)=A(BB−1)A−1=AIA−1=AA−1=I,(AB)(B^{-1}A^{-1}) = A(BB^{-1})A^{-1} = AIA^{-1} = AA^{-1} = I, (B−1A−1)(AB)=B−1(A−1A)B=B−1IB=B−1B=I.(B^{-1}A^{-1})(AB) = B^{-1}(A^{-1}A)B = B^{-1}IB = B^{-1}B = I.

Both products are II, so B−1A−1B^{-1}A^{-1} is the inverse of ABAB. ■\blacksquare

The order reverses for the reason your shoes come off before your socks: ABAB means “first BB, then AA”, so undoing it means “first undo AA, then undo BB”. Notice the proof's shape. It didn't derive the answer. It guessed a candidate and verified the definition, a pattern you'll see again and again.

Learn: the determinant as a volume scale

A 2×22 \times 2 matrix sends the unit square to the parallelogram spanned by its columns (Chamber 4's picture: the columns are where e1\mathbf{e}_1 and e2\mathbf{e}_2 land). The determinant, det⁡A\det A (read “det A”), is the area of that parallelogram, with a sign:

det⁡[abcd]=ad−bc.\det\begin{bmatrix} a & b \\ c & d \end{bmatrix} = ad - bc.

Since the transformation is linear, every region is scaled by the same factor, so ∣det⁡A∣|\det A| is the factor by which AA scales all areas. For example, [3112]\begin{bmatrix} 3 & 1 \\ 1 & 2 \end{bmatrix} sends the unit square to a parallelogram of area 3⋅2−1⋅1=53 \cdot 2 - 1 \cdot 1 = 5, and a circle of area π\pi to an ellipse of area 5π5\pi. A negative determinant means the transformation also flips orientation, like a mirror. A zero determinant means the parallelogram is flat: the plane has been squashed onto a line, which is exactly the singular case you met in the 2×22 \times 2 inverse.

For a 3×33 \times 3 matrix the determinant is the signed volume of the box (a parallelepiped) spanned by its three columns. One formula expands along the first row, each entry times the 2×22 \times 2 determinant left when you delete its row and column, with alternating signs:

det⁡A=a11(a22a33−a23a32)−a12(a21a33−a23a31)+a13(a21a32−a22a31).\det A = a_{11}(a_{22}a_{33} - a_{23}a_{32}) - a_{12}(a_{21}a_{33} - a_{23}a_{31}) + a_{13}(a_{21}a_{32} - a_{22}a_{31}).

For the grain matrix: 3(9−2)−2(6−1)+1(4−3)=21−10+1=123(9 - 2) - 2(6 - 1) + 1(4 - 3) = 21 - 10 + 1 = 12.

Expanding like this takes roughly n!n! multiplications for an n×nn \times n matrix, which is hopeless beyond tiny sizes. Four rules give a far better way:

  • Products multiply: det⁡(AB)=det⁡A⋅det⁡B\det(AB) = \det A \cdot \det B. Doing one transformation after another multiplies their scale factors.
  • Triangular matrices are easy: the determinant is the product of the diagonal entries.
  • Row operations have simple effects: a swap flips the sign, scaling a row by cc scales the determinant by cc, and adding a multiple of one row to another changes nothing.
  • The identity has determinant 11.

Put together, elimination computes determinants. It turns AA into a triangular matrix using only swaps and replacements, so

det⁡A=(−1)number of swaps×(product of the pivots).\det A = (-1)^{\text{number of swaps}} \times (\text{product of the pivots}).

For the grain problem: one swap and pivots 11, −1-1, 1212 give det⁡A=−(1⋅(−1)⋅12)=12\det A = -(1 \cdot (-1) \cdot 12) = 12, just as the expansion said. It also explains the central fact of this chamber. det⁡A=0\det A = 0 exactly when a pivot is missing, which is exactly when elimination can't reach II, which is exactly when there is no inverse:

A is singular  ⟺  det⁡A=0  ⟺  fewer than n pivots  ⟺  the columns are linearly dependent.A \text{ is singular} \iff \det A = 0 \iff \text{fewer than } n \text{ pivots} \iff \text{the columns are linearly dependent}.
Quick check +20 XP

A 2×22 \times 2 matrix AA has det⁡A=−3\det A = -3. It transforms a triangle of area 22. What is the area of the transformed triangle?

Why machine learning cares. A normalising flow is a generative model that turns simple noise into data, such as images, through an invertible function. Training it needs the exact probability of each training image, and when an invertible function ff maps data xx to noise z=f(x)z = f(x), probabilities get rescaled by exactly how much ff stretches volume near xx:

Spotted in the wild

pX(x)=pZ(f(x))∣det⁡(∂f(x)∂xT)∣p_{X}(x) = p_{Z}\big(f(x)\big) \left|\det\left(\cfrac{\partial f(x)}{\partial x^T} \right)\right|
Dinh, Sohl-Dickstein & Bengio (2017), “Density estimation using Real NVP”, Eq. 2

The matrix inside the determinant is the Jacobian of ff, its matrix of partial derivatives (Mathematics for Machine Learning, Chamber 7). Near xx, ff behaves like that matrix, so its determinant is ff's local volume scale. The problem is cost. A small colour image has 32×32×3=307232 \times 32 \times 3 = 3072 numbers, so the Jacobian is 3072×30723072 \times 3072, and a general determinant of that size costs around twenty billion arithmetic operations, even by elimination. For every image. At every training step.

Real NVP's answer is to design each layer so that its Jacobian is triangular. A coupling layer copies the first dd inputs unchanged and scales and shifts the rest, using amounts computed from the first dd. The paper writes out the Jacobian:

Spotted in the wild

∂y∂xT=[Id0∂yd+1:D∂x1:dTdiag⁡(exp⁡[s(x1:d)])]\frac{\partial y}{\partial x^T} = \left[\begin{array}{cc} \mathbb{I}_d & 0 \\ \frac{\partial y_{d+1:D}}{\partial x_{1:d}^T} & \operatorname{diag}\big(\exp\left[s\left(x_{1:d}\right)\right]\big) \end{array} \right]
Dinh, Sohl-Dickstein & Bengio (2017), “Density estimation using Real NVP”, Eq. 6

Zeros above the diagonal, the identity and a diagonal matrix on it. The complicated bottom-left block, which depends on neural networks, doesn't matter at all: the determinant is the product of the diagonal, exp⁡(∑js(x1:d)j)\exp\big(\sum_j s(x_{1:d})_j\big), and its logarithm is a plain sum. And because det⁡(AB)=det⁡Adet⁡B\det(AB) = \det A \det B (the paper's Equation 11), a whole stack of layers just adds up their log-determinants. A few facts from this section turn an impossible computation into a cheap one.

DiscoverLearnRead beyondPapers & lecturesYour turn

Read beyond the course

Each of these covers this chamber's ideas from a different angle. Pick at least one, and mark it read when you're done.

Book · free online · ~45 min

Mathematics for Machine Learning

Marc Peter Deisenroth, A. Aldo Faisal & Cheng Soon Ong · Section 2.3, Solving Systems of Linear Equations; Section 4.1, Determinant and Trace

The same material with more formality and more examples, including the “minus-1 trick” for writing down every solution of a system at once. Section 4.1 lists the determinant's properties with the notation machine learning papers use.

Interactive · free online · ~40 min

Immersive Linear Algebra

Jacob Ström, Kalle Åström & Tomas Akenine-Möller · Chapter 5, Gaussian Elimination; then Chapter 7, Determinants

A free textbook with interactive figures. In Chapter 7 you can drag the columns of a matrix and watch the area of the parallelogram, and the determinant, change sign as it flips.

Article · free online · ~20 min

Matrices and determinants

J J O'Connor & E F Robertson, MacTutor History of Mathematics

The story of this chamber's ideas, from the grain problem on a Chinese counting board, through Seki and Leibniz inventing determinants independently, to Gauss's asteroid and Cayley's first abstract definition of a matrix. It's a reminder that notation evolves: the matrices came long after the methods.

Article · free online · ~25 min

Flow-based Deep Generative Models

Lilian Weng · Sections “Linear Algebra Basics Recap” and “RealNVP”

A clear tour of normalising flows that starts from Jacobians and determinants and builds up to Real NVP's coupling layers. Read it right after this chamber, while triangular determinants are fresh.

DiscoverLearnRead beyondPapers & lecturesYour turn

Papers and lectures

Real NVP is a good first paper about generative models because its central idea is pure linear algebra. You can read the core today. Start with Section 3.1, the change of variable formula (Equations 2 and 3, which you've now seen). Then read Section 3.2 on coupling layers, which defines the layer in Equations 4 and 5, and Section 3.3, where Equation 6 is the triangular Jacobian and Equations 7–8 invert the layer. Finish with the short Section 3.5, whose Equations 11 and 12 are det⁡(AB)=det⁡Adet⁡B\det(AB) = \det A \det B and the socks-and-shoes rule. Skip the masking scheme, the multi-scale architecture, batch normalisation and the experiments for now, but do look at the generated faces and bedrooms in the figures: every one came from inverting a stack of these layers.

Density estimation using Real NVPLaurent Dinh, Jascha Sohl-Dickstein, Samy Bengio · ICLR, 2017

The paper that made normalising flows practical for images, by making every layer's Jacobian triangular. Its descendants include Glow and many modern flow-based models.

Decode the paper · Eq. (3), Section 3.1

Density estimation using Real NVP

Laurent Dinh, Jascha Sohl-Dickstein, Samy Bengio · ICLR, 2017

+30 XP
log⁡(pX(x))=log⁡(pZ(f(x)))+log⁡(∣det⁡(∂f(x)∂xT)∣)\log\left(p_{X}(x)\right) = \log\Big(p_{Z}\big(f(x)\big)\Big) + \log\left(\left|\det\left(\frac{\partial f(x)}{\partial x^T}\right)\right|\right)

Real NVP learns an invertible map ff from data xx (an image, say) to a latent code z=f(x)z = f(x) that should look like simple Gaussian noise. This equation, from the paper's section on the change of variable formula, gives the exact log-likelihood of a data point: the number the model maximises during training. Match each symbol to its meaning.

pX(x)p_{X}(x)
pZp_{Z}
ff
∂f(x)∂xT\frac{\partial f(x)}{\partial x^T}
det⁡\det
∣ ⋅ ∣\left|\,\cdot\,\right|

Options

Watch

The determinant | Chapter 6, Essence of linear algebra3Blue1Brown · 10 min

The best visual explanation of the determinant there is. Watch for the moment a region's area is scaled by the same factor everywhere, and for why a flipped grid gives a negative sign.

2. Elimination with Matrices.MIT OpenCourseWare · 48 min

Gilbert Strang's second lecture from MIT's 18.06. He runs elimination on a 3×33 \times 3 system, then shows that each step is itself a matrix multiplication, an idea that leads to the LU factorisation used inside np.linalg.solve.

DiscoverLearnRead beyondPapers & lecturesYour turn

Your turn

First, translate between notation, code and the rules of determinants.

Match · Maths ↔ NumPy

From notation to NumPy

+25 XP

Match each piece of notation to the NumPy that computes or performs it. M is an augmented matrix stored as a float array. Remember that NumPy counts rows from 0, so the paper's R1R_1 is M[0].

A−1A^{-1}
det⁡A\det A
the solution of Ax=bA\mathbf{x} = \mathbf{b}
I3I_3
R1↔R2R_1 \leftrightarrow R_2
R2←R2−2R1R_2 \leftarrow R_2 - 2R_1
R3←14R3R_3 \leftarrow \tfrac{1}{4} R_3

Options

Match · Operation ↔ Determinant

What happens to the determinant?

+25 XP

Match each operation or matrix to what it does to the determinant (or what its determinant is). All matrices are square.

Swap two rows of AA
Multiply one row of AA by cc
Add a multiple of one row to another
det⁡(cA)\det(cA) for A∈Rn×nA \in \mathbb{R}^{n \times n}
det⁡(AB)\det(AB)
det⁡(A−1)\det(A^{-1})
A triangular matrix

Options

Proofs

The first proof comes in pieces: put the lines in order and leave out the ones that don't belong. The second you write yourself, and it's short.

Proof puzzle

An invertible matrix gives exactly one solution

+25 XP

Claim

If AA is an invertible n×nn \times n matrix, then for every b∈Rn\mathbf{b} \in \mathbb{R}^n the system Ax=bA\mathbf{x} = \mathbf{b} has exactly one solution, namely x=A−1b\mathbf{x} = A^{-1}\mathbf{b}.

Tap lines in the order they should appear. Not every line belongs. Tap a line in your proof to send it back.

Your proof

  1. Pick the first line below.

Available lines

Prove it yourself

A matrix has at most one inverse

+40 XP

Claim

Let AA be an n×nn \times n matrix. If BB and CC are both inverses of AA, that is AB=BA=IAB = BA = I and AC=CA=IAC = CA = I, then B=CB = C. (This is why we may speak of the inverse A−1A^{-1}.)

Preview

Your typeset proof appears here.

Code it up

Three problems: count the singular weight matrices, beat floating point with exact elimination, and evaluate a Real NVP likelihood the way the paper does.

Problem 13·Warm-up

How many layers lose information?

+20 XP

A tiny linear layer maps R2→R2\mathbb{R}^2 \to \mathbb{R}^2 with the weight matrix

W=[abcd].W = \begin{bmatrix} a & b \\ c & d \end{bmatrix}.

If WW is singular, the layer squashes the plane onto a line (or a point), and no later layer can recover what it threw away.

Suppose every entry a,b,c,da, b, c, d is an integer from −3-3 to 33 inclusive, so there are 74=24017^4 = 2401 possible matrices. How many of them are singular, that is, have det⁡W=ad−bc=0\det W = ad - bc = 0?

An exact integer (or a fraction like 7/12)

Problem 14·Standard

Exact elimination beats floating point

+35 XP

The n×nn \times n Hilbert matrix has entries

Hij=1i+j−1,1≤i,j≤n.H_{ij} = \frac{1}{i + j - 1}, \qquad 1 \le i, j \le n.

It looks harmless, but it is famously ill-conditioned: tiny rounding errors are magnified enormously when you solve systems with it.

Take n=10n = 10 and let b=(1,1,…,1)∈R10\mathbf{b} = (1, 1, \ldots, 1) \in \mathbb{R}^{10}. The solution x\mathbf{x} of Hx=bH\mathbf{x} = \mathbf{b} has integer entries. Find it exactly, by Gaussian elimination with fractions, and give

∑i=110∣xi∣.\sum_{i=1}^{10} |x_i|.

An exact integer (or a fraction like 7/12)

Problem 15·Challenge

Apply the paper: a tiny Real NVP

+50 XP

Real NVP builds an invertible map f:RD→RDf : \mathbb{R}^D \to \mathbb{R}^D from affine coupling layers. Each layer keeps one half uu of its input unchanged and transforms the other half vv (the paper's Eqs. 4–5):

u′=u,v′=v⊙exp⁡(s(u))+t(u),u' = u, \qquad v' = v \odot \exp\big(s(u)\big) + t(u),

where ⊙\odot and exp⁡\exp act entry by entry. The model's log-density then comes from Eq. 3:

log⁡pX(x)=log⁡pZ(f(x))+log⁡∣det⁡∂f(x)∂xT∣.\log p_X(x) = \log p_Z\big(f(x)\big) + \log\left|\det \frac{\partial f(x)}{\partial x^T}\right|.

Here D=4D = 4 and x=(0.5, −1, 0.25, 1)x = (0.5,\ -1,\ 0.25,\ 1). The map ff is three coupling layers, each applied to the output of the one before:

- Layer 1: u=(x1,x2)u = (x_1, x_2) and v=(x3,x4)v = (x_3, x_4), using M1M_1. - Layer 2: u=(x3,x4)u = (x_3, x_4) and v=(x1,x2)v = (x_1, x_2), using M2M_2. - Layer 3: u=(x1,x2)u = (x_1, x_2) and v=(x3,x4)v = (x_3, x_4), using M3M_3.

Here x1,…,x4x_1, \ldots, x_4 always mean the current four numbers, and v′v' goes back into the positions vv came from. In layer kk, s(u)=tanh⁡(Mku)s(u) = \tanh(M_k u) (entry by entry) and t(u)=ut(u) = u, with

M1=[0.5−110.25],M2=[−0.30.80.60.4],M3=[11−10.5].M_1 = \begin{bmatrix} 0.5 & -1 \\ 1 & 0.25 \end{bmatrix}, \quad M_2 = \begin{bmatrix} -0.3 & 0.8 \\ 0.6 & 0.4 \end{bmatrix}, \quad M_3 = \begin{bmatrix} 1 & 1 \\ -1 & 0.5 \end{bmatrix}.

The prior pZp_Z is the standard normal density on R4\mathbb{R}^4, so log⁡pZ(z)=−2log⁡(2π)−12∥z∥2\log p_Z(z) = -2\log(2\pi) - \tfrac{1}{2}\lVert z \rVert^2, with natural logarithms.

Compute log⁡pX(x)\log p_X(x), rounded to 4 decimal places.

A number, rounded to 4 decimal places

Key takeaways

  • Two pictures of Ax=bA\mathbf{x} = \mathbf{b}: rows are lines or planes that must meet; columns are vectors whose combination must make b\mathbf{b}.
  • Elimination uses three reversible row operations on [A∣b][A \mid \mathbf{b}] to reach row echelon form, then back substitution; Gauss–Jordan continues to the unique reduced form.
  • Three outcomes: a pivot in the b\mathbf{b} column means no solution; a column of AA without a pivot means infinitely many; otherwise exactly one.
  • The inverse undoes AA, can be found by reducing [A∣I][A \mid I], exists only for nonsingular square matrices, and reverses products: (AB)−1=B−1A−1(AB)^{-1} = B^{-1}A^{-1}.
  • The determinant is the signed volume scale: det⁡(AB)=det⁡Adet⁡B\det(AB) = \det A \det B, triangular means product of the diagonal, and det⁡A=0\det A = 0 exactly when AA is singular.
  • Triangular Jacobians make a normalising flow's log-determinant a cheap sum, which is how Real NVP computes exact likelihoods.

Checkpoint

Prove it to the labyrinth

Answer every question to clear this chamber. First-try answers earn the most XP.

0/7
Question 1 of 7 +20 XP

Which row operation undoes R2←R2−3R1R_2 \leftarrow R_2 - 3R_1?

Question 2 of 7 +20 XP

What is det⁡[27−10−34005]\det \begin{bmatrix} 2 & 7 & -1 \\ 0 & -3 & 4 \\ 0 & 0 & 5 \end{bmatrix}?

Question 3 of 7 +20 XP

What is the inverse of A=[2153]A = \begin{bmatrix} 2 & 1 \\ 5 & 3 \end{bmatrix}?

Question 4 of 7 +20 XP

AA and BB are invertible n×nn \times n matrices. What is (AB)−1(AB)^{-1}?

Question 5 of 7 +20 XP

AA and BB are 3×33 \times 3 matrices with det⁡A=5\det A = 5 and det⁡B=−2\det B = -2. What is det⁡(2AB)\det(2AB)?

Question 6 of 7 +20 XP

Which of these matrices is singular?

Question 7 of 7 +20 XP

Why does Real NVP design its coupling layers so that their Jacobians are triangular?

End of the chamber

Clear this chamber

+60 XPGaussian EliminationThe InverseThe DeterminantSingular Matrix