0.1.1 • Published 7 years ago
equation-balancer v0.1.1
Equation balancer
A very simple CLI chemical equation balancer
Install
npm install -g equation-balancer
Requires Node 8.
Usage
Call equation-solver
with no arguments to get a loop that prompts for an equation:
balance > Fe + H2SO4 -> Fe2(SO4)3 + H2
2, 3, 1, 3
or call equation-solver
with a single argument of the equation you want balanced in quotes.
equation-balancer "Fe + H2SO4 -> Fe2(SO4)3 + H2"
2, 3, 1, 3
Separate terms with +
and use ->
to separate the reactants from the products. Capitalization matters.
Library
You can also use the function programmatically,
const balance = require('equation-balancer/balance');
balance('Fe + H2SO4 -> Fe2(SO4)3 + H2') // [2, 3, 1, 3]
How does it work?
It parses the input into a set of terms and places them into a matrix.
For example, the equation
Fe + H2SO4 -> Fe2(SO4)3 + H2
Gets parsed into:
const matrix = [
[ 1, 0, -2, 0 ], // Fe
[ 0, 4, -12, 0 ], // O
[ 0, 2, 0, -2 ], // H
[ 0, 1, -3, 0 ], // S
];
And then that gets row reduced into:
const reduceMatrix = [
[ 1, 0, 0, -0.6666666666666666 ],
[ 0, 1, 0, -1 ],
[ 0, 0, 1, -0.3333333333333333 ],
[ 0, 0, 0, 0 ]
];
The last column of rows in the matrix get converted to fractions and then the LCM is applied to give the final result:
2, 3, 1, 3