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