function gauss(a){
let arr = a.map(r => r.slice());
let [n, m] = [arr.length, arr[0].length];
let [h, k] = [0, 0];
while(h < n && k < m){
let idx = h;
while(idx < n && arr[idx][k] == 0) idx++;
if(arr[idx][k] == 0){
k++;
} else {
[arr[idx], arr[h]] = [arr[h], arr[idx]];
for(let i = h + 1; i < n; i++){
const f = arr[i][k] / arr[h][k];
arr[i][k] = 0;
for(let j = k + 1; j < m; j++){
arr[i][j] -= arr[h][j] * f;
}
}
h++;
k++;
}
}
return arr;
}
function solve(a){
let [n, m] = [a.length, a[0].length];
const arr = gauss(a);
let sol = Array(n).fill(null);
sol[n - 1] = arr[n - 1][m - 1] / arr[n - 1][m - 2];
for(let i = n - 2; i > -1; i--){
let sum = 0;
for(let j = i + 1; j < n; j++){
sum += arr[i][j] * sol[j];
}
sol[i] = (arr[i][m - 1] - sum) / arr[i][i];
}
return sol;
}
function gaussSolve(a, b){
let arr = a.map(r => r.slice());
for (let i = 0; i < arr.length; i++){
arr[i].push(b[i]);
}
const sol = solve(arr);
return sol
}
let a = [[1, 1, 1, 0], [1, 1, 0, 1], [1, 0, 1, 1], [0, 1, 1, 1]]
let b = [1, -1, 2, 3]
let x = gaussSolve(a, b)
console.log("x: ", x)
To embed this program on your website, copy the following code and paste it into your website's HTML: