Balance chemical equations using linear algebra. Written in JS.
Since this is a test application it's not a library, but does accept a simple command line argument: a valid chemical equation to balance.
node index.js "H2 + O2 => H2O"
Result
2H2 + O2 => 2H2O
node index.js "K2Cr2O7 + KI + H2SO4 => Cr2(SO4)3 + K2SO4 + I2 + H2O"
Result
K2Cr2O7 + 6KI + 7H2SO4 => Cr2(SO4)3 + 4K2SO4 + 3I2 + 7H2O
Chemistry balancer uses a simple linear algebra approach to balance chemical equations.
- Parse the equation into a tree of subcompounds.
- For each element in the equation, create a new row in a matrix.
- For each compound, push the number of times the element is present in the compound into the row.
- Because this is a set of linear equations we already subtracted everything except the last cell, so for all the product compounds in the matrix except for the last one we'll multiply by -1.
- Separate the matrix into two matrices, one for the left side of the equation and a vector for the right side.
- Solve the system of linear equations and push one to the result vector because we are solving for a relationship instead of numbers.
- We now have the coefficients, just add them to each compound.
- Compare the right and left side to verify
- Return the result.
The code isn't well documented, so don't expect to learn much from this.
Copyright 2022 bsnk-dev
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.