# Given an unbalanced chemical equation, is it possible to determine the largest possible coefficient that could be used in the balanced equation?

I am writing a program to balance chemical equations, and would love to know if there is an algebraic way to determine the greatest possible coefficient that I would need to check.

• I think you might just want to use matrices... – DHMO Oct 1 '16 at 13:34

# In short

Just use matrices.

# In long

Consider this equation to be balanced:

$$\ce{CuSCN + KIO3 + HCl -> CuSO4 + KCl + HCN + ICl + H2O}$$

We are trying to solve this equation:

$$\ce{aCuSCN + bKIO3 + cHCl -> dCuSO4 + eKCl + fHCN + gICl + hH2O}$$

Now, we move every term to the left hand side (warning: handwavey chemistry):

$$\ce{aCuSCN + bKIO3 + cHCl - dCuSO4 - eKCl - fHCN - gICl - hH2O = 0}$$

Transform into matrix form (the element names are just for reference):

$$\begin{matrix} \ce{Cu}\\ \ce{S}\\ \ce{C}\\ \ce{N}\\ \ce{K}\\ \ce{I}\\ \ce{O}\\ \ce{H}\\ \ce{Cl} \end{matrix} \left( \begin{matrix} 1 &0 &0 &-1&0 &0 &0 &0 \\ 1 &0 &0 &-1&0 &0 &0 &0 \\ 1 &0 &0 &0 &0 &-1&0 &0 \\ 1 &0 &0 &0 &0 &-1&0 &0 \\ 0 &1 &0 &0 &-1&0 &0 &0 \\ 0 &1 &0 &0 &0 &0 &-1&0 \\ 0 &3 &0 &-4&0 &0 &0 &-1\\ 0 &0 &1 &0 &0 &-1&0 &-2\\ 0 &0 &1 &0 &-1&0 &-1&0 \end{matrix} \right) \left( \begin{matrix} a\\ b\\ c\\ d\\ e\\ f\\ g\\ h \end{matrix} \right) = \left( \begin{matrix} 0\\0\\0\\0\\0\\0\\0\\0\\0\\ \end{matrix} \right)$$

Notice that the biggest matrix is not a square. You must transform it to a square. If the elements are in excess (just like this case), add variables; otherwise, add elements. Set the extra variables/elements to zero:

$$\begin{matrix} \ce{Cu}\\ \ce{S}\\ \ce{C}\\ \ce{N}\\ \ce{K}\\ \ce{I}\\ \ce{O}\\ \ce{H}\\ \ce{Cl} \end{matrix} \left( \begin{matrix} 1 &0 &0 &-1&0 &0 &0 &0 &0 \\ 1 &0 &0 &-1&0 &0 &0 &0 &0 \\ 1 &0 &0 &0 &0 &-1&0 &0 &0 \\ 1 &0 &0 &0 &0 &-1&0 &0 &0 \\ 0 &1 &0 &0 &-1&0 &0 &0 &0 \\ 0 &1 &0 &0 &0 &0 &-1&0 &0 \\ 0 &3 &0 &-4&0 &0 &0 &-1&0 \\ 0 &0 &1 &0 &0 &-1&0 &-2&0 \\ 0 &0 &1 &0 &-1&0 &-1&0 &0 \end{matrix} \right) \left( \begin{matrix} a\\ b\\ c\\ d\\ e\\ f\\ g\\ h\\ i \end{matrix} \right) = \left( \begin{matrix} 0\\0\\0\\0\\0\\0\\0\\0\\0\\ \end{matrix} \right)$$

Note: $i$ must be zero.

Now you can use any algorithm for solving homogeneous square matrix equations to continue.

• There is no guarantee that any solver gives exact integer multiples and if the matrix is singular (as in your example) simply scaling the result vector won't necessarily work either. You will actually need to use an integer matrix solver. A Gaussian elimination with pivoting that only uses integer multipliers for instance should do this correctly. The smallest common multiplier of all matrix entries should provide the upper bound. – Deathbreath Oct 3 '16 at 15:21
• Also don't pad matrices to make them square. It's unnecessary. – Deathbreath Oct 3 '16 at 15:26