-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGaussJordan.java
98 lines (77 loc) · 2.47 KB
/
GaussJordan.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
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
package linear_equation;
public class GaussJordan {
//static int m=10;
static void printMatrix(double a[][], int n){
for(int i=0;i<n;i++){
System.out.println("");
for(int j=0;j<=n;j++){
System.out.print(a[i][j]+ " ");
}
}
}
static int operation(double a[][], int n){
int i,j,k=0,c=0,flag=0,m=0;
// double pro=0;
for(i=0;i<n;i++){
if(a[i][i]==0){
c=1;
while((i+c)<n && a[i+c][k]==0)
c++;
if((i+c)==n){
flag=1;
break;
}
for(j=i,k=0;k<=n;k++){
double temp= a[j][k];
a[j][k]=a[j+c][k];
a[j+c][k]=temp;
}
}
for(j=0;j<n;j++){
if(i !=j){
double p=a[j][i]/a[i][i];
for(k=0;k<=n;k++)
a[j][k]=a[j][k]-(a[i][k]*p);
}
}
}
return flag;
}
static void printResult(double a[][], int n,int flag){
System.out.println("\nResult is: ");
if(flag==2){
System.out.println("Infinite solution exists!!!");
}
else{
for(int i=0;i<n;i++){
System.out.println("x"+(i+1)+" = "+a[i][n]/a[i][i]+" ");
}
}
}
static int checkFlag(double a[][], int n,int flag){
flag=3;
for(int i=0;i<n;i++){
double sum=0;
for(int j=0;j<n;j++){
sum= sum+a[i][j];
if(sum==a[i][j]){
flag=2;
}
}
}
return flag;
}
public static void main(String[] args) {
// double a[][]={{0,2,1,4},{1,1,2,6},{2,1,1,7}};
// double a[][]={{2,1,3,1},{4,4,7,1},{2,5,9,3}};
double a[][]={{3,1,5},{1,-3,5}};
int n=2, flag=0;
flag= operation(a,n);
if(flag==1)
flag= checkFlag(a,n,flag);
System.out.println("Augmented Matrix :");
printMatrix(a,n);
System.out.println("");
printResult(a,n,flag);
}
}