JS Gauss-Jordan Elimination
Gauss-Jordan Elimination by JavaScript.
by Tinytsunami
HTML
<div id="demo">
<button>Create Matrix</button>
<button>Run Gauss Elimination</button>
<button>Run Gauss-Jordan Elimination</button>
<br/>
<br/>
<canvas></canvas>
</div>
CSS
body {
color: #ffffff;
background: #20262e;
font-family: monospace, sans-serif;
}
canvas {
border: solid 1px #ffffff;
}
#demo button {
cursor: pointer;
color: #ffffff;
background: #20262e;
border: 1px solid #ffffff;
outline: none;
}
#demo button:hover {
color: #20262e;
background: #ffffff;
}
JavaScript
let root = document.getElementById("demo");
let canvas = root.getElementsByTagName("canvas")[0];
let context = canvas.getContext("2d");
let createMatrixButton = root.getElementsByTagName("button")[0];
let runGaussEliminationButton = root.getElementsByTagName("button")[1];
let runGaussJordanEliminationButton = root.getElementsByTagName("button")[2];
const FLOAT = 2;
const MARGIN = 20;
const PADDING = 10;
const FONT_STYLE = "20px Arial";
const DELAY = 200;
let animation = null;
let matrix = null;
let matrices = [];
let refresh = function(A, i = -1, j = -1) {
matrices.push({
value: A.slice(),
r: i,
c: j
});
};
let clear = function() {
matrix = null
matrices = [];
if (animation != null) {
clearInterval(animation);
animation = null;
}
};
let initialize = function() {
clear();
let m = Math.floor(Math.random() * 3) + 4;
let n = Math.floor(Math.random() * 3) + 4;
let M = Array.from({
length: m
}, function() {
return Array.from({
length: n
}, function() {
return Math.floor(Math.random() * 100);
});
});
let b = Array.from({
length: m
}, function() {
return Array.from({
length: 1
}, function() {
return Math.floor(Math.random() * 100);
});
});
matrix = augmented(M, b);
if (Math.random() > 0.5) {
scalar(matrix, Math.floor(Math.random() * m), 0);
}
if (Math.random() > 0.5) {
let z = Math.floor(Math.random() * m);
scalar(matrix, z, 0);
addition(matrix, Math.floor(Math.random() * m), z, 1);
}
refresh(matrix);
show();
};
createMatrixButton.onclick = initialize;
runGaussEliminationButton.onclick = function() {
if (animation == null && matrix != null) {
Gauss(matrix);
show();
}
};
runGaussJordanEliminationButton.onclick = function() {
if (animation == null && matrix != null) {
Gauss(matrix);
Jordan(matrix);
show();
}
};
let show = function() {
animation = setInterval(function() {
let matrix =...