## 3 Balancing Chemical Equations Using Iterations
### Balancing Chemical Equations Using Iterative Methods  
The coefficients \(x_{i}\) are integers, rational or real numbers, which should be determined on the basis of three basic principles: (1) the law of conservation of mass; (2) the law of conservation of atoms; (3) the time-independence of Equation (20), an assumption usually valid for stable/non-sensitive reactions. Let there be \(m\) distinct atoms involved in the chemical reaction Equation (20) and \(n=r+s\) distinct reactants and products. It is necessary to form an \(m\times n\) matrix \(A\), called the reaction matrix, whose columns represent the reactants and products and the rows represent the distinct atoms in the chemical reaction. More precisely, the \((i,j)\)-th element of \(A\), denoted by \(a_{i,j}\), represents the number of atoms of type \(i\) in each compound/element (reactant or product). An arbitrary element \(a_{i,j}\) is positive or negative according to whether it corresponds to a reactant or a product. Hence, the balancing chemical equation problem can be formulated as the homogeneous matrix equation:  
\[Ax=0 \tag{21}\]  
with respect to the unknown vector \(x\in\mathbb{R}^{n}\), where \(A\in\mathbb{R}^{m\times n}\) denotes the reaction matrix and \(0\) denotes the null column vector of the order \(m\). In this way, an arbitrary chemical reaction can be formulated as a matrix equation. We would like to use the symbolic and numerical possibilities of the _Mathematica_ computer algebra system in conjunction with the above-defined iterative method(s) for computing generalized inverses to automatize the chemical reactions balancing process. The general solution of the balancing problem in the matrix form Equation (21) is given by:  
\[s=\left(I-A^{\dagger}A\right)c \tag{22}\]  
where \(c\) is the arbitrarily-selected \(n\)-dimensional vector. Let us assume that the approximation of \(A^{\dagger}\) generated by an arbitrary iterative method is given by \(X:=X_{k+1}\). If the iterative method for computing \(X\) is performed in the floating point arithmetic, it is necessary to perform a transition from the solution whose coordinates are real numbers into an exact (integer and/or rational) solution. Thus, the iterative approach in balancing chemical equations assumes three general algorithmic steps, as is described in Algorithm 2. ```
1:Apply (for example) Algorithm 1 and compute the approximation \(X:=X_{k+1}\) of \(A^{\dagger}\). 2:Compute the vector \(s\) using Equation (22). 3:Transform real numbers included in \(s\) into an exact solution. ```  
**Algorithm 2** General algorithm for balancing chemical equations by an iterative solver. A clear observation about Algorithms 2 is the following:  
* Steps 1 and 2 require usage of real arithmetic (with very high precision);
* Step 3 requires usage of symbolic processing and exact arithmetic capabilities to deal with rational numbers. As a result, in order to apply iterative methods to the problem of balancing chemical equations, it is necessary to use a software that meets two diametrically-opposite criteria: the ability to carry out numerical calculations (with very high precision) and the ability to apply the exact arithmetic and symbolic calculations. The programming language _Mathematica_ possesses both of these properties. More details about this programming language can be found in [24]. The following (sample) _Mathematica_ code can be used to determine the exact solution using real values contained in the vector \(s\) (defined in Equation (22)). ```
1:Id=IdentityMatrix[n];s=(Id-X.A).ConstantArray[1,n];s=Rationalize[s,10^(-300)];c=s*LCM@@Denominator/@s; (* MultiplysbytheLeastCommonMultipleofdenominatorsins*) s=c/Min@@Numerator/@c (* DividecbytheMinimumofnumeratorsinc*)
```  
**Algorithm 3** General algorithm for balancing chemical equations by an iterative solver.