tecdanax.blogg.se

Chemical equation balancer with decimals
Chemical equation balancer with decimals













So far, all the equations form a homogeneous linear system. There is one more step in setting up the matrix. In this case, the equation is 0 a + 0 b = 0 c. Even though none of the terms in this example are electrically charged, we always add an equation for electric charge anyway. For O, the equation is 0 a + 2 b = 1 c, and we get the matrix row. In our matrix, we actually represent this equation as 2 a + 0 b − 2 c = 0, using the row of integers. For H, the equation balances only if 2 a + 0 b = 2 c. Now we make an equation for each unique element. We give a variable to represent the coefficient for each term, and we get aH 2 + bO 2 → cH 2O. In the code, this functionality is found in buildMatrix, Equation/Term/Group/ChemElem.getElements, Term/Group/untElement.įor example, the equation H 2 + O 2 → H 2O has 3 terms and 2 elements (H, O). Each different element, and also electric charge, gets an equation. Each term in the equation gets a variable. The idea is to set up a system of linear equations to represent the balancing problem. In the code, this functionality is found in parseEquation, parseTerm, parseGroup, parseElement, parseCount, and Tokenizer. The parsed result is your chemical equation in the internal object representation.

CHEMICAL EQUATION BALANCER WITH DECIMALS CODE

The parser takes a lot of code to implement and is ugly, but it is robust. Your text input is parsed by a hand-written tokenizer and a recursive descent parser with one token of look-ahead. In the code, this functionality is found in Equation, Term, Group, ChemElem. For example: H 3O +, S 2−.Īn Equation object is a list of Term objects for the left side and a list of Term objects for the right side. For example: (H 2O) 6, (C(OH) 3) 2.Ī Term object is a list of ChemElem or Group objects, with an integer electric charge for the whole term. For example: Fe, H 2.Ī Group object is a parenthesized list of ChemElem or Group objects, with a positive integer repetition count for the whole group. The exception is that e by itself represents an electron.Īn ChemElem object is a chemical element with a positive integer repetition count. For example, these are elements: H, Na, Uuq. How it works Data representationĪ raw chemical element is a string of letters that begins with an uppercase letter and is followed by any number of lowercase letters. In this case, each term that has a negative coefficient should be put on the other side of the equation, and its new coefficient should be the absolute value of the negative coefficient. Note: For simplicity of implementation, if the equation is successfully balanced but one or more terms have a negative coefficient, the program doesn’t consider this outcome to be an error condition. This error should not happen, but if it does please contact me. The author/programmer made a serious logic mistake. There is no workaround the code would need to be rewritten to use bigints. I don’t expect this error to occur for real-world chemical formulas, only deliberately contrived ones. Your equation used numbers that are too big, or a term has an element that occurs too many times, or the internal calculation used numbers that are too big. Furthermore, the equation can be separated as H → H 2 and O → O 2, each of which does have a unique solution. For example, H + O → H 2 + O 2 has no unique solution because two solutions are 2H + 4O → H 2 + 2O 2 and 6H + 2O → 3H 2 + O 2, which are not multiples of each other. Your equation can be considered as two or more independent equations added together. There exist multiple solutions to your equation that are not simply multiples of each other. For example, C → N 2 has no solution because the only solution is 0C → 0N 2. The only mathematical solution to your equation has all coefficients set to zero, which is a trivial solution for every chemical equation. Check each letter carefully, and follow the examples as a guide to the correct syntax. Your input does not describe a proper chemical equation. Syntax guide Feature & demoįoo 5+ + Bar 3− → FooBar 2 + FooBar − The source TypeScript code and compiled JavaScript code are available for viewing. This program was hand-written in JavaScript in year 2011, received minor feature updates and clarifications and refactorings throughout the years, and was ported to TypeScript in 2018. Because the program is entirely client-side JavaScript code, this web page can be saved and used offline. The algorithm used is Gauss-Jordan elimination, slightly modified to operate using only integer coefficients (not fractions). The program calculates the coefficients to balance your given chemical equation. This is an easy-to-use, no-nonsense chemical equation balancer. Chemical equation balancer (JavaScript) Program Input:













Chemical equation balancer with decimals