Case Study 1 — Balancing Chemical Equations: When Counting Atoms Becomes Row Reduction

Every chemistry student learns to balance equations "by inspection" — squint at $\mathrm{C_3H_8 + O_2 \to CO_2 + H_2O}$, juggle the coefficients in your head, and eventually land on the magic numbers that make the atoms tally. It works for simple reactions and fails miserably for messy ones, dissolving into trial-and-error frustration. What almost no chemistry course mentions is that balancing a chemical equation is exactly a linear-algebra problem — a homogeneous system $A\mathbf{x} = \mathbf{0}$ — and that Gaussian elimination solves it every time, no inspiration required. This case study shows the translation, works a clean example by hand, and then turns the algorithm loose on a reaction that would defeat any reasonable amount of squinting.

Conservation of atoms is a system of linear equations

A balanced equation must obey one law: atoms are neither created nor destroyed. Whatever number of carbon atoms enters on the left must leave on the right; likewise for every element. That is a counting constraint, and counting constraints are linear.

Take the combustion of propane:

$$a\,\mathrm{C_3H_8} + b\,\mathrm{O_2} \;\longrightarrow\; c\,\mathrm{CO_2} + d\,\mathrm{H_2O}.$$

The unknowns are the four coefficients $a, b, c, d$ — how many molecules of each species participate. Now write one equation per element, balancing the atom count on each side:

  • Carbon: each $\mathrm{C_3H_8}$ has $3$ carbons, each $\mathrm{CO_2}$ has $1$. So $3a = c$, i.e. $3a - c = 0$.
  • Hydrogen: each $\mathrm{C_3H_8}$ has $8$ hydrogens, each $\mathrm{H_2O}$ has $2$. So $8a = 2d$, i.e. $8a - 2d = 0$.
  • Oxygen: each $\mathrm{O_2}$ has $2$, each $\mathrm{CO_2}$ has $2$, each $\mathrm{H_2O}$ has $1$. So $2b = 2c + d$, i.e. $2b - 2c - d = 0$.

Three equations, four unknowns, and every right-hand side is zero — a homogeneous system. In matrix form, with the unknown vector $\mathbf{x} = (a, b, c, d)$:

$$\begin{bmatrix} 3 & 0 & -1 & 0 \\ 8 & 0 & 0 & -2 \\ 0 & 2 & -2 & -1 \end{bmatrix} \begin{bmatrix} a \\ b \\ c \\ d \end{bmatrix} = \begin{bmatrix} 0 \\ 0 \\ 0 \end{bmatrix}.$$

Notice immediately what the shape tells us, using the lessons of this chapter. There are more unknowns ($4$) than equations ($3$), so the system is underdetermined — we expect at least one free variable, hence infinitely many solutions. That is not a defect; it is chemically correct. If $(a,b,c,d)$ balances the equation, so does $(2a, 2b, 2c, 2d)$ — you can run the reaction at any scale. The free variable is exactly that overall scaling freedom. The chemist's job is to pick the scaling that makes all four coefficients the smallest positive integers.

Solving it by row reduction

A homogeneous system always has the trivial solution $\mathbf{x} = \mathbf{0}$ (no molecules at all — useless), so the interesting content is the nonzero solutions, which live along the free-variable direction. We row-reduce the coefficient matrix; because the right-hand side is all zeros and stays all zeros under every row operation, we can drop the augmented column and just reduce $A$. Driving it to reduced row echelon form (the Gauss–Jordan process of §4.5) yields

$$\begin{bmatrix} 1 & 0 & 0 & -\tfrac14 \\[2pt] 0 & 1 & 0 & -\tfrac54 \\[2pt] 0 & 0 & 1 & -\tfrac34 \end{bmatrix}.$$

Pivots sit in columns $1, 2, 3$ (the $a$, $b$, $c$ columns), so $d$ is the free variable — the scaling freedom we predicted. Reading the rows:

$$a = \tfrac14 d, \qquad b = \tfrac54 d, \qquad c = \tfrac34 d, \qquad d \text{ free}.$$

To clear the fractions, choose the free variable $d = 4$. Then $a = 1$, $b = 5$, $c = 3$, $d = 4$. The balanced equation is

$$\mathrm{C_3H_8} + 5\,\mathrm{O_2} \;\longrightarrow\; 3\,\mathrm{CO_2} + 4\,\mathrm{H_2O}.$$

Let's confirm the coefficient vector really lies in the null space — that $A\mathbf{x} = \mathbf{0}$ — and check the atom counts mechanically.

# A balanced equation is a null-space vector: A x = 0.
import numpy as np
A = np.array([[3, 0, -1,  0],    # carbon
              [8, 0,  0, -2],    # hydrogen
              [0, 2, -2, -1]],   # oxygen
             dtype=float)
x = np.array([1, 5, 3, 4], dtype=float)   # (a, b, c, d) from row reduction
print(A @ x)        # [0. 0. 0.]  -> every element balances

The product is the zero vector: carbon, hydrogen, and oxygen each balance exactly. The "magic numbers" were never magic — they are the smallest-integer point on the one-dimensional solution line of a homogeneous linear system, found by pure row reduction.

The fact that the balanced equation is a null-space vector is no accident, and it is a preview of one of the four fundamental subspaces. The null space of $A$ (Chapter 13) is the set of all $\mathbf{x}$ with $A\mathbf{x} = \mathbf{0}$ — here, all valid scalings of the reaction. A reaction that can be balanced corresponds to a null space of dimension $\geq 1$; the rare reaction that cannot be balanced (over the given species) would have only the trivial solution. The dimension of that null space — the number of free variables — even tells you when a set of reactions is independent versus when one is a combination of others.

Where inspection dies and the algorithm shines

For propane you might have guessed the answer. Now consider a genuine redox reaction from the chemistry lab, potassium permanganate oxidizing hydrochloric acid:

$$a\,\mathrm{KMnO_4} + b\,\mathrm{HCl} \;\longrightarrow\; c\,\mathrm{KCl} + d\,\mathrm{MnCl_2} + e\,\mathrm{H_2O} + f\,\mathrm{Cl_2}.$$

Six unknowns, five elements (K, Mn, O, H, Cl). Balancing this by inspection is a genuinely painful exercise — the chlorine appears in three products and couples everything together. But the linear-algebra recipe does not care how tangled it looks. One equation per element, in the unknowns $(a,b,c,d,e,f)$:

$$\begin{array}{ll} \text{K:} & a - c = 0 \\ \text{Mn:} & a - d = 0 \\ \text{O:} & 4a - e = 0 \\ \text{H:} & b - 2e = 0 \\ \text{Cl:} & b - c - 2d - 2f = 0 \end{array} \qquad\Longrightarrow\qquad A = \begin{bmatrix} 1 & 0 & -1 & 0 & 0 & 0 \\ 1 & 0 & 0 & -1 & 0 & 0 \\ 4 & 0 & 0 & 0 & -1 & 0 \\ 0 & 1 & 0 & 0 & -2 & 0 \\ 0 & 1 & -1 & -2 & 0 & -2 \end{bmatrix}.$$

Five equations, six unknowns — again one expects a single free variable (the scaling). Hand the matrix to row reduction (here we let the computer run the exact same elimination we do by hand, since the arithmetic is tedious, not difficult):

# Balance a hard redox reaction by reducing the conservation matrix.
import numpy as np
A = np.array([[1, 0, -1,  0,  0,  0],    # K
              [1, 0,  0, -1,  0,  0],    # Mn
              [4, 0,  0,  0, -1,  0],    # O
              [0, 1,  0,  0, -2,  0],    # H
              [0, 1, -1, -2,  0, -2]],   # Cl
             dtype=float)
print(np.linalg.matrix_rank(A))          # 5  -> 6 - 5 = 1 free variable
x = np.array([2, 16, 2, 2, 8, 5], dtype=float)  # smallest-integer null vector
print(A @ x)                              # [0. 0. 0. 0. 0.]  -> balanced

The rank is $5$, so $6 - 5 = 1$ free variable — exactly one scaling degree of freedom, as chemistry demands. Solving and clearing to smallest integers gives $(a,b,c,d,e,f) = (2, 16, 2, 2, 8, 5)$:

$$2\,\mathrm{KMnO_4} + 16\,\mathrm{HCl} \;\longrightarrow\; 2\,\mathrm{KCl} + 2\,\mathrm{MnCl_2} + 8\,\mathrm{H_2O} + 5\,\mathrm{Cl_2}.$$

The check A @ x returns all zeros: every one of the five elements balances. No squinting, no lucky guesses — just the algorithm of this chapter, applied to a matrix of atom counts.

What you should take away

A chemical equation looks nothing like a system of linear equations until you ask the right question. The instant you write "atoms in = atoms out," each element becomes a linear equation, the coefficients become unknowns, and balancing becomes finding a nonzero vector in the null space of an integer matrix — a job Gaussian elimination does flawlessly and at any scale. The free variable you cannot eliminate is not a flaw in the method; it is the chemistry, encoding the fact that a reaction balances at any overall multiple.

This is the pattern you saw with PageRank and recommenders in earlier chapters, now in a wet-lab setting: a problem from a completely non-mathematical field, restated in vectors and matrices, becomes an instance of something this book already knows how to solve. The same row reduction that found $(1,2,3)$ for three planes in space finds $(2, 16, 2, 2, 8, 5)$ for a flask of permanganate. When you reach the null space in Chapter 13, return here — you will recognize that "balancing a reaction" and "finding the null space of the conservation matrix" are the very same sentence.

Forward references: the null space $N(A)$ and homogeneous systems $A\mathbf{x}=\mathbf{0}$ (Chapter 13); rank and the dimension of the solution set (Chapter 14); the four fundamental subspaces as the organizing framework (Part III).