1function gaussianElimination(aug) {
2 const n = aug.length;
3 for (let col = 0; col < n; col++) {
4 let pivot = col;
5 for (let r = col + 1; r < n; r++)
6 if (Math.abs(aug[r][col]) > Math.abs(aug[pivot][col])) pivot = r;
7 if (pivot !== col) [aug[col], aug[pivot]] = [aug[pivot], aug[col]];
8 for (let r = col + 1; r < n; r++) {
9 const factor = aug[r][col] / aug[col][col];
10 for (let c = col; c <= n; c++)
11 aug[r][c] -= factor * aug[col][c];
12 }
13 }
14 const x = new Array(n).fill(0);
15 for (let i = n - 1; i >= 0; i--) {
16 let sum = aug[i][n];
17 for (let j = i + 1; j < n; j++) sum -= aug[i][j] * x[j];
18 x[i] = sum / aug[i][i];
19 }
20 return x;
21}