This repository has been archived by the owner on Sep 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguasselim.java
49 lines (40 loc) · 1.46 KB
/
guasselim.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import java.util.*;
public class guasselim {
public static void main(String[] args) {
// Test input; should give <1, 2, 3>
double[][] mat = { { 1, 1, 2, 9 }, { 2, 4, -3, 1 }, { 3, 6, -5, 0 } };
double[] ans = GaussianElimination(mat);
System.out.println(Arrays.toString(ans));
}
// Aug is an n x n + 1 augmented matrix
static double[] GaussianElimination(double[][] aug) {
int n = aug.length;
double[] X = new double[n]; // represents a column vector
// Convert matrix to row-echelon form
for (int j = 0; j < n - 1; j++) {
int l = j;
for (int i = j + 1; i < n; i++)
// Find the row with the largest column value
if (Math.abs(aug[i][j]) > Math.abs(aug[l][j]))
l = i;
// Swap rows
for (int k = j; k <= n; k++) {
double tmp = aug[j][k];
aug[j][k] = aug[l][k];
aug[l][k] = tmp;
}
// Forward elimination
for (int i = j + 1; i < n; i++)
for (int k = n; k >= j; k--)
aug[i][k] -= aug[j][k] * aug[i][j] / aug[j][j];
}
// Back-substitute
for (int j = n - 1; j >= 0; j--) {
double t = 0.0;
for (int k = j + 1; k < n; k++)
t += aug[j][k] * X[k];
X[j] = (aug[j][n] - t) / aug[j][j];
}
return X;
}
}