3

Lore:
Now that my chemistry class has gone past memorizing equations and whatnot and begun with, example, balancing chemical equations. I could sit down all day long balancing equations, but as programming is my passion I would love to get a program working for me to solve these. This is more or less a pet-project and more for fun rather than giving me an edge in chemistry class. But the more I dwelt into it the more complex it became. I don't really know how to begin this crusade and have instead worked on the parser and data set, which is in pretty good set to have my head wrapped around it right.

Question:
What I don't know is how to utilize matrices to solve equations (balance equations to conserve mass*) and convert that into whole numbers valid in chemistry.

Code/objects:

class Element {
    constructor(name,quantity) {
        this.name = name;
        this.quantity = quantity;
        if (this.quantity == 0) {
            this.quantity = 1;
        }
    }
}
class Molecule {
    constructor() {
        this.elements = [];
        this.multiplier = 1;
    }
    addElement(newEl) {
        this.elements.push(newEl);
    }
    list() {
        this.elements.forEach(el => {
            console.log(el.name,el.quantity);
        });
    }
    getMultiplier() {
        return this.multiplier;
    }
    getElements() {
        var a = [];
        this.elements.forEach(el => {
            a.push([el.name,el.quantity*this.multiplier]);
        });
        return a;
    }
}

Code/data structure:

printFormula(moleculeList);
for (var i=0;i<moleculeList[0].length;i++) {
    console.log("Mol "+(i+1))
    moleculeList[0][i].list();
}
console.log("==>");
for (var i=0;i<moleculeList[1].length;i++) {
    console.log("Mol "+(i+1))
    moleculeList[1][i].list();
}

Code/output:

'C6H14 + O2 ==> CO2 + H2O'
Mol 1
C 6
H 14
Mol 2
O 2
==>
Mol 1
C 1
O 2
Mol 2
H 2
O 1
Simon
  • 95
  • 1
  • 11
  • @FedericoklezCulloca: *how to utilize matrices to solve equations and convert that into whole numbers valid in chemistry.* – Robert Harvey Dec 14 '18 at 15:36
  • 1
    ... which is a bit broad for this site. Can you make your question more specific? – Robert Harvey Dec 14 '18 at 15:37
  • @Simon Break it down into small functional pieces as you have been doing and you'll get there. I imagine you need some values based on the type of molecule. It's been years since I took a chemistry course, so I have no idea what those values are anymore. I remember using a `mol` and the periodic table, the periodic table you may want to declare as a hash set for quick reference – Ryan Wilson Dec 14 '18 at 15:38
  • @RyanWilson I have followed your suggestion and have a plan to solving this. But when it comes to math I still have some work left to do. But I'm not really sure how to simplify this equation to get 'x' '6 + -1 * x = 0' – Simon Dec 16 '18 at 12:15
  • @Simon You're either going to have to do some string parcing or search for some kind of equation solving software that you can add to your project. My first thought if going for your own custom, would be to parse out the numerics and the operators and do the mathematical computation necessary to solve for 'x', maybe this post will get you moving (https://stackoverflow.com/questions/27431870/is-it-possible-to-convert-a-string-that-is-an-equation-with-a-variable-into-a-eq) – Ryan Wilson Dec 18 '18 at 15:46

1 Answers1

3

I am a chemist and have exactly zero familiarity with Javascript so as to explain it in detail. But the method is fairly simple to show by hand. This link nails it down in a clean manner.

The only reason why I am linking to it without going through the steps in detail is because Stackoverflow does not have LaTeX math in order to make the text easy to follow, so I think you'll benefit better from the text in the link. Also, I don't think this is a full answer, but I do not have enough reputation to put this on a comment, where it belongs.

I think the hardest bit is finding/coding the appropriate subroutine for row-echelon reduction of the matrix in step-5. But I also believe that there is a vast number of algorithms appropriate for doing that which you can find in the internet. Very handily, this very website has a question related to just that with a python code to sweeten the deal.

I hope this helps you at least wrap your head around it enough to implement the code you need.

Cheers

urquiza
  • 307
  • 2
  • 11