-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRowToEchelon.java
More file actions
50 lines (50 loc) · 1.81 KB
/
RowToEchelon.java
File metadata and controls
50 lines (50 loc) · 1.81 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
public class RowToEchelon {
public static double[][] convertToEchelon(double[][] augmented_matrix){
int rows = augmented_matrix.length;
int cols = augmented_matrix[0].length;
int pivot = 0;
for (int i = 0; i < cols-1; i++) {
boolean pivotExists = false;
for (int j = 0; j <rows ; j++) {
if(augmented_matrix[j][i]!=0){
pivotExists = true;
if(i !=pivot){
double[] temp = augmented_matrix[j];
augmented_matrix[j] = augmented_matrix[pivot];
augmented_matrix[pivot] = temp;
}
pivotExists = true;
break;
}
}
if(pivotExists){
double pivotValue = augmented_matrix[pivot][i];
for (int j = pivot+1; j <rows ; j++) {
double multiplier = augmented_matrix[j][i]/pivotValue;
for (int k = i; k <cols ; k++) {
augmented_matrix[j][k] -= multiplier*augmented_matrix[pivot][k];
}
}
pivot++;
}
}
return augmented_matrix;
}
public static void printMatrix(double[][] matrix) {
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
System.out.printf("%.2f\t", matrix[i][j]);
}
System.out.println();
}
}
public static void main(String[] args) {
double[][] augmented_matrix = {
{7, 0, 4, 5},
{88, 4, 4, 72},
{9, 8, 22, 7}
};
double[][] echelonForm = convertToEchelon(augmented_matrix);
printMatrix(echelonForm);
}
}