3
$\begingroup$

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.

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

In short

Just use matrices.

In long

Consider this equation to be balanced:

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

(source with spoiler)

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.

$\endgroup$
  • 1
    $\begingroup$ 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. $\endgroup$ – Deathbreath Oct 3 '16 at 15:21
  • 1
    $\begingroup$ Also don't pad matrices to make them square. It's unnecessary. $\endgroup$ – Deathbreath Oct 3 '16 at 15:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.