Skip to content
Prev Previous commit
Gaussian elimination translated from C to JS
  • Loading branch information
Ariana1729 committed Oct 6, 2018
commit 069f87119802cf19cf577f4a63c9304faf4e96e7
112 changes: 112 additions & 0 deletions contents/gaussian_elimination/code/javascript/gaussian_elimination.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
function gaussian_elimination(a){
var rows = a.length
var cols = a[0].length
var row = 0;

for (let col = 0; col < cols - 1; ++col) {

let pivot = row;
for (let i = row + 1; i < rows; ++i) {
if (Math.abs(a[i][col]) > Math.abs(a[pivot][col])) {
pivot = i;
}
}

if (a[pivot][col] == 0) {
console.log("The matrix is singular.\n");
continue;
}

if (col != pivot) {
let t=a[col];
a[col]=a[pivot];
a[pivot]=t;
}

for (let i = row + 1; i < rows; ++i) {
let scale = a[i][col] / a[row][col];

for (let j = col + 1; j < cols; ++j) {
a[i][j] -= a[row][j] * scale;
}

a[i][col] = 0;
}

++row;
}
return a;
}

function back_substitution(a){
var rows = a.length;
var cols = a[0].length;
var sol=new Array(rows);

for (let i = rows - 1; i >= 0; --i) {

let sum = 0;
for (let j = cols - 2; j > i; --j) {
sum += sol[j] * a[i][j];
}

sol[i] = (a[i][cols - 1] - sum) / a[i][i];
}
return sol;
}

function gauss_jordan(a) {
var rows = a.length;
var cols = a[0].length;
var row = 0;

for (let col = 0; col < cols - 1; ++col) {
if (a[row][col] != 0) {
for (let i = cols - 1; i > col - 1; --i) {
a[row][i] /= a[row][col];
}

for (let i = 0; i < row; ++i) {
for (let j = cols - 1; j > col - 1; --j) {
a[i][j] -= a[i][col] * a[row][j];
}
}

++row;
}
}
}

var a = [[3, 2 , -4, 3 ],
[ 2, 3 , 3 , 15],
[ 5, -3, 1 , 14]];

gaussian_elimination(a);
console.log("Gaussian elimination:\n");
for(let i=0;i<a.length;++i){
let txt=""
for(let j=0;j<a[i].length;++j){
txt+=a[i][j]<0?" ":" ";
txt+=a[i][j].toPrecision(8);
}
console.log(txt);
}

gauss_jordan(a);
console.log("\nGauss-Jordan:\n");
for(let i=0;i<a.length;++i){
let txt=""
for(let j=0;j<a[i].length;++j){
txt+=a[i][j]<0?" ":" ";
txt+=a[i][j].toPrecision(8);
}
console.log(txt);
}

var sol=back_substitution(a),txt="";
console.log("\nSolutions are:\n");
for(let i=0;i<sol.length;++i){
txt+=sol[i]<0?" ":" ";
txt+=sol[i].toPrecision(8);
}
console.log(txt)
8 changes: 8 additions & 0 deletions contents/gaussian_elimination/gaussian_elimination.md
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,8 @@ In code, this looks like:
[import:10-36, lang:"haskell"](code/haskell/gaussianElimination.hs)
{% sample lang="py" %}
[import:3-28, lang:"python"](code/python/gaussian_elimination.py)
{% sample lang="js" %}
[import:1-39, lang:"javascript"](code/javascript/gaussian_elimination.js)
{% endmethod %}

Now, to be clear: this algorithm creates an upper-triangular matrix.
Expand Down Expand Up @@ -397,6 +399,8 @@ This code does not exist yet in rust, so here's Julia code (sorry for the inconv
[import:38-46, lang:"haskell"](code/haskell/gaussianElimination.hs)
{% sample lang="py" %}
[import:31-49, lang:"python"](code/python/gaussian_elimination.py)
{% sample lang="js" %}
[import:58-78, lang:"javascript"](code/javascript/gaussian_elimination.js)
{% endmethod %}

## Back-substitution
Expand Down Expand Up @@ -429,6 +433,8 @@ In code, this involves keeping a rolling sum of all the values we substitute in
[import:48-53, lang:"haskell"](code/haskell/gaussianElimination.hs)
{% sample lang="py" %}
[import:52-64, lang:"python"](code/python/gaussian_elimination.py)
{% sample lang="js" %}
[import:41-56, lang:"javascript"](code/javascript/gaussian_elimination.js)
{% endmethod %}

## Conclusions
Expand All @@ -453,6 +459,8 @@ As for what's next... Well, we are in for a treat! The above algorithm clearly h
[import, lang:"haskell"](code/haskell/gaussianElimination.hs)
{% sample lang="py" %}
[import, lang:"python"](code/python/gaussian_elimination.py)
{% sample lang="js" %}
[import, lang:"javascript"](code/javascript/gaussian_elimination.js)
{% endmethod %}


Expand Down