← All Base Form Math projects
BM-04NOT STARTEDBASE FORM · LINEAR ALGEBRAMATHPYTHON

Chemical equation balancer

Paste an unbalanced equation like C3H8 + O2 → CO2 + H2O and get the smallest whole-number coefficients, found from the null space of a matrix.

EST.5 h
REWARD+100 XP
AFTERoriginal

ML-ARENA BENCHMARKS

YOUR NUMBER VS THE INDUSTRY'S
METRICINDUSTRYMINE
50 textbook equations
GOAL · all 50 balanced, same coefficients
SymPy nullspace—
no results yet
YOUR RESULTS

Your recorded results appear here.

Brief

Balancing a chemical equation means choosing how many of each molecule to use so that every atom is accounted for. In this project you turn that puzzle into a matrix and let elimination solve it.

Write the unknown coefficients as x=(a,b,c,d)x = (a, b, c, d). Each element gives one equation. For propane burning, carbon says 3a=c3a = c, hydrogen says 8a=2d8a = 2d, oxygen says 2b=2c+d2b = 2c + d. Move everything to one side and you have a linear system Ax=0Ax = 0: one row per element, one column per molecule, with product columns negated.

You solve it with Gaussian elimination. Pick a pivot in the first column, subtract multiples of its row to clear the entries below, then move to the next column. Swap rows whenever the pivot would be zero. At the end, any column without a pivot holds a free variable. With one free variable there is a single family of answers: set it to 1, solve for the rest, and scale to whole numbers. That family is the null space of AA, which you met in BM-03 as the set of inputs a matrix squashes to zero.

VISUALFig. 1
a C3H8 + b O2 → c CO2 + d H2O one row per element, one column per species, products negated C3H8 O2 CO2 H2O C H O 30 −10 80 0−2 02 −2−1 · 15 34 x = 00 0
Propane combustion as A·x = 0. Product columns are negated; the coefficients 1, 5, 3, 4 give zero in every row. Why it's here: This is the matrix you build in objective 2. Check your own against it before you write elimination.
HINT · OBJECTIVE 1
Stuck? Hints stay hidden until you ask.
HINT · OBJECTIVE 4
Stuck? Hints stay hidden until you ask.

DATA · 50 equations you collect

Objectives

0 / 6 CLEARED

Read-only. Log in to update progress.

VICTORY CONDITIONS

  1. All 50 equations balanced with the same coefficients as SymPy nullspace, recorded with `python tools/mlarena.py record BM-04 "50/50 match"`.
  2. Every answer satisfies A·x = 0 exactly, with positive whole-number coefficients that share no common factor.
  3. Equations that cannot be balanced, or have more than one independent answer, are reported clearly instead of printed as if they worked.

Side quests

Write a float version with partial pivoting and run it on the same 50 equations. Find one where skipping pivoting gives a wrong answer.
Support ions by adding a charge row, for example MnO4⁻ + Fe²⁺ + H⁺ → Mn²⁺ + Fe³⁺ + H2O.

Terms in plain words

Linear system
A set of equations where every unknown appears only multiplied by a number, written as Ax = b. Here b is all zeros.
Gaussian elimination
Subtract multiples of one row from the others until the matrix has zeros below a staircase of leading entries.
Pivot
The leading non-zero entry in a row that you use to clear its column. Pivoting means swapping rows so the pivot is not zero (or, with floats, is the largest available).
Reduced row echelon form
The end of elimination where every pivot is 1 and is the only non-zero entry in its column, so you can read the answer straight off.
Free variable
An unknown whose column has no pivot. You may pick its value freely; every other unknown then follows from it.