Balance a reaction equation in elements and charge
rxn_balance_equation
Finds the smallest whole-number coefficients that balance a reaction in every element and in charge.
- Runs
- On the server
- Level
- Checked against a reference: exact arithmetic in a Python program, cross-checked against chempy 0.10.2; a second implementation in Python by a separate agent of the same model family
What it is
Returns the smallest whole-number coefficients that balance the species listed in every element and in charge. Species stay on the sides they are listed on. Use when the reactants and products are known and the coefficients are wanted. Refuses, with the reason, an equation that cannot balance with every species taking part, or one with more than one balance; it never chooses among balances. Not for atom economy: use `rxn_atom_economy`; not for one formula weight: use `calc_formula_mass`.
What it does
You give
reactants(required): The reactants, each once.products(required): The products, each once.
No request is recorded for this tool.
Limits
- Bench and process arithmetic on a reaction you write down.
- Research and education software: it does not predict products or routes, check that the chemistry will work, or assess hazards.
- Coefficients that balance the species you list, on the sides you list them, in elements and charge.
- Balancing is arithmetic: it does not check that these species react, that this is the reaction that occurs, or that the products are right.
- Where more than one balance exists it refuses rather than choose one.
- Electrons are not a species, so half-reactions are not accepted.
- Only elements with a standard atomic weight are accepted, because the tool shares the suite's formula reader: technetium, promethium, polonium and the heavier elements without one (thorium, protactinium and uranium are accepted), and isotope symbols such as D and T, are refused even though balancing does not use mass.
- Every computed number a reply returns, other than an integer or an exact decimal, is rounded once to 12 significant figures: to the nearest, a change of at most 5 × 10⁻¹² of its value, except that a lower bound is rounded down and an upper bound up, a change of less than 1 × 10⁻¹¹; the method computes, and decides any pass or fail, on the unrounded value.
It will not answer when
- no balance has every species taking part on the side written: an element on one side only, only the zero solution, a single balance with a zero or mixed-sign coefficient, or two or three independent balances none of which is strictly positive.
- the species admit two or three independent balances in elements and charge and at least one has every species taking part, so no one balance is chosen.
- No element is on one side only, and the species admit four or more independent balances in elements and charge (number of species minus the rank of the element-and-charge matrix is 4 or more).
- the same formula, as written, appears more than once among the reactants and products.
Every input, with its units2 inputs
reactants(required): The reactants, each once.products(required): The products, each once.
Every refusal, with its code6 refusals
| Code | When |
|---|---|
EQUATION_IMPOSSIBLE | no balance has every species taking part on the side written: an element on one side only, only the zero solution, a single balance with a zero or mixed-sign coefficient, or two or three independent balances none of which is strictly positive. |
EQUATION_UNDERDETERMINED | the species admit two or three independent balances in elements and charge and at least one has every species taking part, so no one balance is chosen. |
BALANCES_ABOVE_RANGE | No element is on one side only, and the species admit four or more independent balances in elements and charge (number of species minus the rank of the element-and-charge matrix is 4 or more). |
DUPLICATE_SPECIES | the same formula, as written, appears more than once among the reactants and products. |
FORMULA_IS_SMILES | a formula reads as a SMILES (= # @ / \ or a lower-case letter not after an upper-case one); for a structure, molecule_descriptors. |
FORMULA_INVALID | the formula cannot be read exactly as written: it is an electron; it writes a charge of zero; the formula-mass parser refuses it (an unknown symbol or one without a standard atomic weight, an ambiguous charge such as Fe3+, an unclosed bracket, a number before the formula such as 13CH4); it holds a count of zero; a count or the charge is above 2^53 − 1; or its molar-mass sum is too large to hold exactly (the shared routine formula-reading). |
The calculation
The method's equations, constants and worked example.
Equation 1
A[e][s] = +count_e(s) for a reactant, −count_e(s) for a product, for every element e and a last row of charges
a balance is ν > 0 with A·ν = 0
| Symbol | Meaning |
|---|---|
A | element-and-charge matrix |
ν | stoichiometric coefficients |
Source 1.
Equation 2
d = number of species − rank(A), by exact Gauss–Jordan elimination over rationals
| Symbol | Meaning |
|---|---|
d | independent balances |
Equation 3
an element on one side only: refused EQUATION_IMPOSSIBLE
d = 0: refused EQUATION_IMPOSSIBLE
d = 1: the null vector, if all of one sign and none zero, scaled to the smallest whole numbers, else refused EQUATION_IMPOSSIBLE
d = 2 or 3: refused EQUATION_UNDERDETERMINED if a strictly positive null vector exists (exact Fourier–Motzkin, each verdict checked by its certificate), else EQUATION_IMPOSSIBLE
d of 4 or more: refused BALANCES_ABOVE_RANGE
| Symbol | Meaning |
|---|---|
d | independent balances |
A definition, not a model: the equation states what the quantity means. A different convention gives a different number.
Worked example
Worked example: one case of rxn-balance-equation@1.0.3, from its inputs through each step to the answer. Values are written as a person writes them; the working carries more digits, so your own figure may differ in the last digit shown.
Inputs
- reactants[0].formula = C3H8
- reactants[1].formula = O2
- products[0].formula = CO2
- products[1].formula = H2O
Steps
- d = 1, from reactants[0].formula, reactants[1].formula, products[0].formula, products[1].formula; by equation 2.
- a = 1, from d; by equation 3; gives reactants[0].coefficient.
- c = 3, from a; by equation 1; gives products[0].coefficient.
- dH2O = 4, from a; by equation 1; gives products[1].coefficient.
- b = 5, from c, dH2O; by equation 1; gives reactants[1].coefficient.
The working, as the case writes it:
C 3 = 3·1
H 8 = 2·4
O 2·5 = 2·3 + 4: (1, 5, 3, 4), gcd 1
Answer
reactants[0].coefficient = 1
products[0].coefficient = 3
products[1].coefficient = 4
reactants[1].coefficient = 5
Where these values came from: our own exact-arithmetic script and a hand-worked case; exact: d = 1, null vector ['1/4', '5/4', '3/4', '1']; cross-check chempy.balance_stoichiometry: {'C3H8': 1, 'O2': 5} -> {'CO2': 3, 'H2O': 4}.
At build the tool returned this answer within the tolerance the file states: coefficients and counts exact; statuses, codes and species exact.
Sources
IUPAC, Quantities, Units and Symbols in Physical Chemistry (the IUPAC Green Book), 4th edition, abridged version, eds. Brett CMA, Frey JG, Hinde R, Kuroda Y, Marquardt R, Pavese F, Quack M, Stohner J, Thor AJ, RSC Publishing 2023
Used for: Smallest whole-number balance of a reaction equation in elements and charge. At: p. 45, section §4.10, note 13: a general chemical equation 0 = Σ_j ν_j B_j, ν_j the stoichiometric number of species B_j, negative for reactants and positive for products; here ν is its magnitude and the sign is carried by the rows of A, and a balance in each element and in charge is the record's reading of the equation, labelled so.
Read in full, 24 November 2026
How it is checked
- Level
Checked against a reference.
- Checked against
Our own exact-arithmetic script and a hand-worked case.
- Cases
No case set is linked to this version.
- Not checked
That the species react, that the equation is the reaction that occurs, or that the products are right. Independent balances 0 to 3 are tested exactly; 4 or more are refused BALANCES_ABOVE_RANGE without a test of positivity.
What was not checked, in fullfrom the method records
- That the species react, that the equation is the reaction that occurs, or that the products are right.
- Independent balances 0 to 3 are tested exactly; 4 or more are refused BALANCES_ABOVE_RANGE without a test of positivity.
- speciesNotTakingPart is not claimed to list every species forced to zero.
- A formula starting with a digit (13C3H8) is refused FORMULA_INVALID since 1.0.3, through formula-mass 1.3.0, with no case in this method's fixtures; the refusal is covered by stored reference values and unit tests only.
Changes to its methods1 change
- 25 November 2026, rxn-balance-equation@1.0.3: In 1.0.3, through formula-mass 1.3.0, a formula starting with a digit is now refused (FORMULA_INVALID); every other output is unchanged, and every pinned output hash is the same.