-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGaussianElimination.java
More file actions
127 lines (97 loc) · 3.48 KB
/
GaussianElimination.java
File metadata and controls
127 lines (97 loc) · 3.48 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
* Class: Gaussian Elimination
* Author: Thomas Tejedor
*
* Purpose: Create a program that solves a set of linear equations
* using Gaussian elimination
*
*/
import java.lang.Math;
public class GaussianElimination {
int n;
double[][] coeffArr;
int[] indexArr;
double[] scaleVectorArr;
public GaussianElimination(int n, double[][] coeffArr) {
this.n = n;
this.coeffArr = coeffArr;
indexArr = new int[n];
scaleVectorArr = new double[n];
getScaleVectors();
}
private void getScaleVectors() {
for(int i = 0; i < n; i++) {
indexArr[i] = i;
double maxCoeff = coeffArr[i][0];
for(int j = 0; j < n; j++) {
if( maxCoeff < Math.abs(coeffArr[i][j]))
maxCoeff = coeffArr[i][j];
}
scaleVectorArr[i] = (maxCoeff);
}
}
private String getCurrentMatrix() {
String output = "\nCurrent Matrix = \n[";
for(int i = 0; i < n; i++) {
for(int j = 0; j < (n+1); j++) {
if(i == (n-1) && j == (n)) {
output += coeffArr[i][j] + "]\n";
}
else if(j == (n)) {
output += coeffArr[i][j] + ",\n";
}
else {
output += coeffArr[i][j] + ", ";
}
}
}
return output;
}
public String getSolvedMatrix() {
String output = "";
output += getCurrentMatrix();
for (int i = 0; i < (n-1); i++) {
output += "\nScale Ratios = [";
int pivotRowIndex = i;
double maxRatio = 0;
for(int k = i; k < n; k++) {
int currRow = k;
double currRatio = Math.abs(coeffArr[currRow][k] / (double)scaleVectorArr[currRow]);
if(k != n-1)
output += currRatio + ", ";
else
output += currRatio + "]\n";
if(currRatio > maxRatio) {
maxRatio = currRatio;
pivotRowIndex = k;
}
}
output += "The pivot row = " + (indexArr[pivotRowIndex] + 1) + "\n";
int pivotRow = indexArr[pivotRowIndex];
indexArr[pivotRowIndex] = indexArr[i];
indexArr[i] = pivotRow;
for(int k = i + 1; k < n; k++) {
int currRow = indexArr[k];
double xMult = (coeffArr[currRow][i]/coeffArr[pivotRow][i]);
coeffArr[currRow][i] = 0;
for(int j = i + 1; j < (n+1); j++) {
coeffArr[currRow][j] = coeffArr[currRow][j] - xMult*(coeffArr[pivotRow][j]);
}
}
output += getCurrentMatrix();
}
return output;
}
public double[] getSolution(){
double[] solution = new double[n];
solution[n-1] = (coeffArr[indexArr[n-1]][n])/(coeffArr[indexArr[n-1]][n-1]);
for (int i = n-2; i >= 0; i--){
double sum = coeffArr[indexArr[i]][n];
for (int j = i+1; j < n ; j++) {
sum -= (coeffArr[indexArr[i]][j]*solution[j]);
}
solution[i] = sum/coeffArr[indexArr[i]][i];
}
return solution;
}
}