Home

Awesome

ONumerical

OCaml library featuring:

Sample Application: Balancing Chemical Equations

There is a typical problem in high-school chemistry: balance (add coefficients to) a reaction equation. For example we start with a simple equation

H2S + O2 -> H2O + SO2

And we wish to obtain the balanced version

2 H2S + 3 O2 -> 2 H2O + 2 SO2

While for the simple reactions the problem is trivial enough to be given as a homework, the solution in the general case requires the application of a simplex method (minimize the sum of all constraints subject to balancing atoms on left and right hand side and subject to the strict positivity constraint).

Moreover, since we want to minimize the quantity with no negative coefficients the problem is dual-feasible. The solution is implemented as a sample application of ONumerical, and can be launched as a standalone executable:

<pre> echo "H2S + O2 -> H2O + SO2" | ./chem_balancer.native 2H2S + 3O2 -> 2H2O + 2SO2 </pre>

Compiling and Installation

Compiling:

$ make

Installation:

$ make install

To remove:

$ make remove

Running tests:

$ make run_test

Compiling the balancer:

$ make chem_balancer
$ ./chem_balancer # to run

Dependencies

NOTE

Dual Simplex uses a very inefficient dense representation and is not optimized for performance. So are the linear algebra primitives, as they re-create the data-structure after each operation.