forked from sPredictorX1708/Ultimate-Java-Resources
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGaussJordanAlgo.java
More file actions
114 lines (104 loc) · 3.69 KB
/
GaussJordanAlgo.java
File metadata and controls
114 lines (104 loc) · 3.69 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
import java.util.Scanner;
public class mainclass {
//got this convert to fraction from stack overflow
static private String convertDecimalToFraction(double x){
if (x < 0){
return "-" + convertDecimalToFraction(-x);
}
double tolerance = 1.0E-6;
double h1=1; double h2=0;
double k1=0; double k2=1;
double b = x;
do {
double a = Math.floor(b);
double aux = h1; h1 = a*h1+h2; h2 = aux;
aux = k1; k1 = a*k1+k2; k2 = aux;
b = 1/(b-a);
} while (Math.abs(x-h1/k1) > x*tolerance);
if (k1==1)
return ""+h1;
else
return h1+"/"+k1;
}
private static void displayMat(float[][] matrixdisp)
{
for (int i=0;i<3;i++)
{
for (int j=0;j<4;j++)
{
System.out.print(convertDecimalToFraction(matrixdisp[i][j])+ " ");
}
System.out.println();
}
System.out.println("***********************************************************");
}
private static void pivot(float[][] piviotmat, int x, int y)
{
float value= piviotmat[x][y];
float z=1/value;// value to piviot
System.out.println("piviting row "+ x+ "by multiplying "+convertDecimalToFraction(z));
for (int k=0;k<4;k++) {
piviotmat[x][k] = piviotmat[x][k] * z;//here we multiply the pivit
}
displayMat(piviotmat);
float multiplier;
for (int l=0;l<3;l++)
{
if (l!=y) {
multiplier = piviotmat[l][y];//value to multiply to piviot
System.out.println("multiplying row "+ x+ "by "+convertDecimalToFraction(multiplier));
for (int k = 0; k < 4; k++) {
piviotmat[x][k] = piviotmat[x][k] * multiplier;//multiply pivit with row multiplier
}
displayMat(piviotmat);
System.out.println("substracting row "+ l+ "from row "+x);
for (int k = 0; k < 4; k++) {
piviotmat[l][k] = piviotmat[l][k] - piviotmat[x][k];
}
displayMat(piviotmat);
System.out.println("restoring pivit row "+ x+ "by dividing "+convertDecimalToFraction(multiplier));
for (int k = 0; k < 4; k++) {
piviotmat[x][k] = piviotmat[x][k] /multiplier;//restore the multipied pivit matrix
}
displayMat(piviotmat);
}
}
}
public static void main(String[] args) {
float[][] matrix;
matrix = new float[3][4];
for (int i=0;i<3;i++)
{
for (int j=0;j<4;j++)
{
System.out.println("enter the values : ");
Scanner sc= new Scanner(System.in);
float value= sc.nextFloat();
matrix[i][j]=value;// checking matrix
}
}
displayMat(matrix);
for (int i=0;i<3;i++)
{
for (int j=0;j<4;j++)
{
if (i==j&&matrix[i][j]!=1)
{
pivot(matrix, i, j);
}
}
}
for (int m=0;m<3;m++)
{
switch (m)
{
case 0: System.out.println("value of x is "+ convertDecimalToFraction(matrix[0][3]));
break;
case 1: System.out.println("value of y is "+ convertDecimalToFraction(matrix[1][3]));
break;
case 2: System.out.println("value of z is "+ convertDecimalToFraction(matrix[2][3]));
break;
}
}
}
}