Socket
Book a DemoInstallSign in
Socket

equation-balancer

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

equation-balancer

> A very simple CLI chemical equation balancer

latest
Source
npmnpm
Version
0.1.1
Version published
Weekly downloads
0
-100%
Maintainers
1
Weekly downloads
 
Created
Source

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

FAQs

Package last updated on 09 Oct 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts