added fixes for chemical equation balancer
This commit is contained in:
parent
34c273c2d2
commit
c99fcd3009
@ -144,11 +144,12 @@ function balanceEquation(reactant, product) {
|
|||||||
var vary = [];
|
var vary = [];
|
||||||
var matrix = [];
|
var matrix = [];
|
||||||
var freeMatrix = [];
|
var freeMatrix = [];
|
||||||
|
var finalMatrix = [];
|
||||||
|
|
||||||
// Create array from inputs.
|
// Create array from inputs.
|
||||||
formulaStr = reactant + "+" + product;
|
formulaStr = reactant + "+" + product;
|
||||||
compounds = formulaStr.replace(/\s/g,"").split("+");
|
compounds = formulaStr.replace(/\s/g,"").split("+");
|
||||||
|
|
||||||
// Create element matrix.
|
// Create element matrix.
|
||||||
for(var i = 0; i < compounds.length; i++) {
|
for(var i = 0; i < compounds.length; i++) {
|
||||||
var counter = 0;
|
var counter = 0;
|
||||||
@ -181,7 +182,7 @@ function balanceEquation(reactant, product) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for(var x = 0; x < vary.length; x++) {
|
for(var x = 0; x < vary.length; x++) {
|
||||||
matrix[x] = [];
|
matrix[x] = [];
|
||||||
for(var y = 0; y < compounds.length; y++) {
|
for(var y = 0; y < compounds.length; y++) {
|
||||||
@ -241,52 +242,76 @@ function balanceEquation(reactant, product) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
lead++;
|
lead++;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extract free matrix.
|
// Extract free matrix.
|
||||||
for(var f = 0; f < matrix.length-1; f++) {
|
var rank = matrix.length;
|
||||||
freeMatrix.push(matrix[f][matrix[f].length-1]);
|
for(var m = 0; m < rank; m++) {
|
||||||
|
freeMatrix[m] = [];
|
||||||
|
for(var n = rank; n < matrix[0].length; n++) {
|
||||||
|
freeMatrix[m].push(-1*matrix[m][n]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
freeMatrix.push(1);
|
|
||||||
|
|
||||||
|
// Adding identity matrix.
|
||||||
|
for(var e = 0; e < compounds.length-freeMatrix.length; e++) {
|
||||||
|
freeMatrix.push([]);
|
||||||
|
for(var f = 0; f < compounds.length-rank; f++) {
|
||||||
|
if((f/2) == parseInt(f/2)) {
|
||||||
|
freeMatrix[e+rank].push(1);
|
||||||
|
} else {
|
||||||
|
freeMatrix[e+rank].push(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Adding and converting into 1 column/row.
|
||||||
|
for(var o = 0; o < freeMatrix.length; o++) {
|
||||||
|
var value = 0;
|
||||||
|
for(var p = 0; p< freeMatrix[0].length; p++) {
|
||||||
|
value += freeMatrix[o][p];
|
||||||
|
}
|
||||||
|
finalMatrix.push(value);
|
||||||
|
}
|
||||||
|
|
||||||
// Multiply all to integers.
|
// Multiply all to integers.
|
||||||
var power = 1;
|
var mul = 1;
|
||||||
var testPow = 1;
|
var testMul = 1;
|
||||||
|
|
||||||
for(var g = 0; g < freeMatrix.length; g++) {
|
for(var g = 0; g < finalMatrix.length; g++) {
|
||||||
var testInt = freeMatrix[g] * Math.pow(10,testPow);
|
var testInt = finalMatrix[g] * testMul;
|
||||||
while(!(testInt == parseInt(testInt))) {
|
while(testInt != parseInt(testInt)) {
|
||||||
testPow += 1;
|
testMul += 1;
|
||||||
|
testInt = finalMatrix[g] * testMul;
|
||||||
}
|
}
|
||||||
if(testPow > power) {
|
if(testMul < mul) {
|
||||||
power = testPow;
|
mul = testMul;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for(var h = 0; h < freeMatrix.length; h++) {
|
for(var h = 0; h < finalMatrix.length; h++) {
|
||||||
freeMatrix[h] *= Math.pow(10,power);
|
finalMatrix[h] *= testMul;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get GCD.
|
// Get GCD.
|
||||||
var gcd = freeMatrix[0];
|
var gcd = finalMatrix[0];
|
||||||
|
|
||||||
for(var u = 0; u < freeMatrix.length-2; u++) {
|
for(var u = 0; u < finalMatrix.length-2; u++) {
|
||||||
gcd = getGCD(gcd, freeMatrix[u+1]);
|
gcd = getGCD(gcd, finalMatrix[u+1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get final answers.
|
// Get final answers.
|
||||||
for(var w = 0; w < freeMatrix.length; w++) {
|
for(var w = 0; w < finalMatrix.length; w++) {
|
||||||
freeMatrix[w] /= gcd;
|
finalMatrix[w] /= gcd;
|
||||||
freeMatrix[w] = Math.abs(freeMatrix[w]);
|
finalMatrix[w] = Math.abs(finalMatrix[w]);
|
||||||
if(freeMatrix[w] != 1) {
|
if(finalMatrix[w] != 1) {
|
||||||
freeMatrix[w] += compounds[w][0];
|
finalMatrix[w] += compounds[w][0];
|
||||||
} else {
|
} else {
|
||||||
freeMatrix[w] = compounds[w][0];
|
finalMatrix[w] = compounds[w][0];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return freeMatrix;
|
return finalMatrix;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getGCD(a,b) {
|
function getGCD(a,b) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user