I am currently working on a program which takes a chemical equation as input and returns the balanced chemical equation instead. It finds the appropiate chemical coefficients with matrix operations, using the formula $x = A^{-1}b$. I am using JAMA for calculations involving matrices. According to their docs, it calculates $A$'s inverse using
Eigenvalue Decomposition of both symmetric and nonsymmetric square matrices
Let's take an example to better visualize this. Suppose the following chemical equation with denoted coefficients for every compound:
$aCa(OH)_2 + bH_3PO_4 \rightarrow cCa_3(PO_4)_2 + dH_2O$
The computer creates an equation for every element:
$P: b - 2c = 0$
$H: 2a + 3b - 2d = 0$
$Ca: a - 3c = 0$
The last equation needs to be $a = det(A)$ for three reasons. We need a square matrix (and that's why oxygen's equation is omitted) to compute it's inverse and determinant. Secondly, we need to give a numerical value to one of the variables to be able to get a numerical result, and thirdly (as far as I understood because I learned nothing about matrices in school yet) $adj(A)$ is multiplied by $1/det(A)$ to get $A^{-1}$ which causes the computed coefficients to sometimes be non-integer numbers. I am not sure of how or why setting $a$ to $det(A)$ works, neither this is the most efficient way of doing it.
$A = \left[\begin{array}{cccc}0&1&-2&0\\2&3&0&-2\\1&0&-3&0\\1&0&0&0\end{array}\right]$ $b = \left[\begin{array}{c}0\\0\\0\\det(A)\end{array}\right]$
After dividing every number to the GCD the computed vector $x$ will be:
$x = A^{-1}b = \left[\begin{array}{c}6\\4\\2\\12\end{array}\right]$ = $\left[\begin{array}{c}3\\2\\1\\6\end{array}\right]$
And of course, the balanced chemical equation is
$3Ca(OH)_2 + 2H_3PO_4 \rightarrow Ca_3(PO_4)_2 + 6H_2O$
This program works perfectly for even more complex equations:
$299H_2SO_4 + 10K_4Fe(CN)_6 + 122KMnO_4 \rightarrow 60CO_2 + 5Fe_2(SO_4)_3 + 188H_2O + 60HNO_3 + 162KHSO_4 + 122MnSO_4$
$9Fe_{36}Si_5 + 836H_3PO_4 + 192K_2Cr_2O_7 \rightarrow 324FePO_4 + 45SiO_2 + 128K_3PO_4 + 384CrPO_4 + 1254H_2O$
However, in the process of testing, I found a chemical equation that produces the following error: java.lang.RuntimeException: Matrix is singular.
Let's write everything down:
$aB_{10}H_{12}CNH_3 + bNiCl_2 + cNaOH \rightarrow dNa_4(B_{10}H_{10}CNH_2)_2Ni + eNaCl + fH_2O$
$B: 10a - 20d = 0$
$C: a - 2d = 0$
$Na: c - 4d - e = 0$
$H: 15a + c - 24d - 2f = 0$
$Cl: 2b - e = 0$
This time, the code omits the equation for three elements ($N, Ni, O$) because it would result in an overdetermined system and a non-square matrix.
$A = \left[\begin{array}{cccccc}10&0&0&-20&0&0\\1&0&0&-2&0&0\\0&0&1&-4&-1&0\\15&0&1&-24&0&-2\\0&2&0&0&-1&0\\1&0&0&0&0&0\end{array}\right]$
This is completely unexpected, both for me and the script. Using a matrix calculator, I found out that $A$ has no inverse: its determinant is zero. I analyzed the matrix and found that $10a - 20d = 0$ and $a - 2d = 0$ are really the same equation. I'm not sure if this has something to do with the matrix being singular.
What should I do to make this work? Should I use a different method for finding solutions and representing my system of equations, or do I have to do something specific when $det(A) = 0$ ? I've read a little bit about Gaussian Elimination and LU Decomposition, but I don't seem to understand them very well. In case I have to use another method for solving the system, which one is the most suitable for this script? Also, I would be happy to get some details about it if possible. Keep in mind I'm a beginner in this matrix field. Any help is appreciated!
The determinant is zero, because the first two lines are linearly dependent, indeed, we can consider $B_{10}C$ as one "atomic element", or complex, if you like, which provides just one equation,
$B_{10}C:\quad a-2d=0$,
so you need to remove the first line and replace it with the line with $Ni$ instead,
$Ni:\quad b-d=0$.
Then $\det A=4$ and the solution is
$a=4$, $b=2$, $c=12$, $d=2$, $e=4$, $f=12$,
or, equivalently,
$a=2$, $b=1$, $c=6$, $d=1$, $e=2$, $f=6$.
And indeed,
\begin{align} 2B_{10}H_{12}CNH_3 + NiCl_2 + 6NaOH &\rightarrow Na_4(B_{10}H_{10}CNH_2)_2Ni + 2NaCl + 6H_2O . \end{align}