Application: Balancing Chemical Reactions
A chemical equation is balanced when every element appears the same number of times on both sides. Finding the coefficients is a homogeneous linear system: one equation per element, one unknown per molecule. Row reduction turns balancing, which can be a frustrating guessing game, into a mechanical computation.
Setting up the system
Take the burning of propane () in oxygen, which produces carbon dioxide and water:
Count each element, putting the reactants on the left with a plus sign and the products with a minus sign:
Three equations, four unknowns, all right-hand sides zero. A homogeneous system with more unknowns than equations always has non-trivial solutions (Homogeneous Systems), and that is exactly what we want.
is free, and , , . Choose to clear the denominators: .
Automating it
Here is a small balancer: it builds the element matrix, asks SymPy for the null space, and scales the basis vector to the smallest whole numbers.
from math import lcm
from sympy import Matrix
def balance(reactants, products):
molecules = reactants + products
elements = sorted({e for m in molecules for e in m})
rows = []
for e in elements:
rows.append([m.get(e, 0) for m in reactants] +
[-m.get(e, 0) for m in products])
ns = Matrix(rows).nullspace()
if len(ns) != 1:
return f"{len(ns)} independent solutions"
v = ns[0]
v = v * lcm(*[term.q for term in v]) # clear denominators
if v[0] < 0:
v = -v
return list(v)
propane = {"C": 3, "H": 8}
O2, CO2, H2O = {"O": 2}, {"C": 1, "O": 2}, {"H": 2, "O": 1}
print(balance([propane, O2], [CO2, H2O]))[1, 5, 3, 4]
term.q is the denominator of a SymPy rational number. If all denominators are cleared and the vector entries share no common factor, this gives the smallest balanced equation (the null space vector here already has a 1 in the free position, so that holds).
A harder reaction
Potassium permanganate reacting with hydrochloric acid has six species, and balancing it by trial and error takes a while:
from math import lcm, gcd
from functools import reduce
from sympy import Matrix
def balance(reactants, products):
molecules = reactants + products
elements = sorted({e for m in molecules for e in m})
A = Matrix([[m.get(e, 0) for m in reactants] + [-m.get(e, 0) for m in products]
for e in elements])
(v,) = A.nullspace()
v = v * lcm(*[t.q for t in v])
g = reduce(gcd, [int(t) for t in v])
return [int(t) // g for t in v]
KMnO4 = {"K": 1, "Mn": 1, "O": 4}
HCl = {"H": 1, "Cl": 1}
KCl, MnCl2 = {"K": 1, "Cl": 1}, {"Mn": 1, "Cl": 2}
H2O, Cl2 = {"H": 2, "O": 1}, {"Cl": 2}
print(balance([KMnO4, HCl], [KCl, MnCl2, H2O, Cl2]))[2, 16, 2, 2, 8, 5]
So . Check chlorine: 16 on the left, on the right. ✓
Note: If the null space has dimension 2 or more, the "reaction" is really two independent reactions written together, and there is no single balanced equation. If it has dimension 0, the proposed reaction is impossible (only the trivial solution).
In software
The same pattern, "conservation laws become a homogeneous system, the answer is the null space", appears in many places: flow conservation in network-flow problems (what flows into a node flows out), Kirchhoff's current law in circuits (Resistor Networks), stoichiometric matrices in metabolic-network modelling, and dimensional analysis (Dimensionless Variables).
Exercises
Exercise 1: Rust
Balance by hand.
Answer
.
Solution
Exercise 2: Ethanol
Write the element matrix for and balance it.
Answer
.
Solution
Exercise 3: Photosynthesis (code)
Use the balance function from the lesson on .
Answer
.
Solution
from math import lcm, gcd
from functools import reduce
from sympy import Matrix
def balance(reactants, products):
mols = reactants + products
els = sorted({e for m in mols for e in m})
A = Matrix([[m.get(e, 0) for m in reactants] + [-m.get(e, 0) for m in products] for e in els])
(v,) = A.nullspace()
v = v * lcm(*[t.q for t in v])
g = reduce(gcd, [int(t) for t in v])
return [int(t) // g for t in v]
print(balance([{"C": 1, "O": 2}, {"H": 2, "O": 1}],
[{"C": 6, "H": 12, "O": 6}, {"O": 2}]))[6, 6, 1, 6]
Exercise 4: Ammonia oxidation
Balance , by hand or in code.
Answer
.
Solution
Test yourself
What kind of linear system do you solve to balance a chemical equation?
- An inconsistent system
- A homogeneous system
- A system with a unique solution
- A system over
The null space of a reaction's element matrix is . What does that mean?
- The reaction is already balanced
- There are infinitely many balanced forms
- The reaction cannot be balanced
- One coefficient is zero
The null-space vector is . How do you get whole-number coefficients?
- Multiply by 4, the least common multiple of the denominators
- Round each entry
- Add 1 to each entry
- Take the reciprocal