-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRank.java
232 lines (164 loc) · 3.26 KB
/
Rank.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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import java.io.*;
public class Rank
{
static InputStreamReader r;
static BufferedReader input;
Rank()throws IOException
{
r=new InputStreamReader(System.in);
input=new BufferedReader(r);
driver();
}
void driver()throws IOException
{
double a[][];
System.out.println("Enter the order of the matrix");
int m=Integer.parseInt(input.readLine());
a=takeInput(m);
double rank=computeRank(a,m);
}
int computeRank(double a[][],int m)
{
int result=-1;
//matrixDisplay(a,m);
for(int i=0;i<m;i++)
{
if(a[i][i]==0)
continue;
if(a[i][i]!=1)
a=divide(a,i,m);
//matrixDisplay(a,m);
a=subtract(i,a,m);
}
matrixDisplay(a,m);
a=columnTransforms(a,m);
matrixDisplay(a,m);
return result;
}
double[][] columnTransforms(double a[][],int m)
{
double x[][]=a;
for(int i=0;i<m;i++)
{
if(a[i][i]==0)
continue;
if(a[i][i]!=1)
a=divide_columns(a,i,m);
//matrixDisplay(a,m);
a=subtract_columns(i,a,m);
}
return x;
}
double[][] switcher(double a[][],int updated,int old,int m)
{
double x[][]=a;
double temp[]=new double[m];
for(int i=0;i<m;i++)
{
temp[i]=x[old][i];
x[old][i]=x[updated][i];
x[updated][i]=temp[i];
}
return x;
}
double[][] switcher_columns(double a[][],int updated,int old,int m)
{
double x[][]=a;
double temp[]=new double[m];
for(int i=0;i<m;i++)
{
temp[i]=x[i][old];
x[i][old]=x[i][updated];
x[i][updated]=temp[i];
}
return x;
}
double[][] subtract(int row,double temp[][],int m)
{
double x[][]=temp;
double MF;
int sign=1;
int column=row;
for(int i=0;i<m;i++)
{
if(i==row)
continue;
MF=temp[i][column];
for(int j=0;j<m;j++)
{
x[i][j]=temp[i][j]-(temp[row][j]*MF);
}
}
return x;
}
double[][] subtract_columns(int column,double temp[][],int m)
{
double x[][]=temp;
double MF;
int sign=1;
int row=column;
for(int i=0;i<m;i++)
{
if(i==row)
continue;
MF=temp[row][i];
for(int j=0;j<m;j++)
{
x[j][i]=temp[j][i]-(temp[j][row]*MF);
}
}
return x;
}
double[][] divide(double temp[][],int row,int m)
{
double x[][]=temp;
double DIVISOR=temp[row][row];
//System.out.println(DIVISOR);
for(int i=0;i<m;i++)
x[row][i]=temp[row][i]/DIVISOR;
return x;
}
double[][] divide_columns(double temp[][],int column,int m)
{
double x[][]=temp;
double DIVISOR=temp[column][column];
//System.out.println(DIVISOR);
for(int i=0;i<m;i++)
x[i][column]=temp[i][column]/DIVISOR;
return x;
}
double[][] takeInput(int m)
{
double temp[][]=new double[m][m];
System.out.println("Enter the elements of the matrix");
try
{
for(int i=0;i<m;i++)
for(int j=0;j<m;j++)
{
System.out.println("Enter the element at ["+i+","+j+"]");
temp[i][j]=Double.parseDouble(input.readLine());
}
}
catch(Exception e)
{
System.out.println("ERROR...please enter elements properly");
}
//matrixDisplay(temp,m);
return temp;
}
void matrixDisplay(double temp[][],int m)
{
for(int i=0;i<m;i++)
{
System.out.print("|");
for(int j=0;j<m;j++)
if(j<m-1)
System.out.print(temp[i][j]+"\t");
else
System.out.print(temp[i][j]);
System.out.println("|");
}
System.out.println("-------------------------------------------------------------------------");
}
}