forked from Sukhdev841/Numerical-Methods
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths_matrix.java
More file actions
303 lines (279 loc) · 7.58 KB
/
Copy paths_matrix.java
File metadata and controls
303 lines (279 loc) · 7.58 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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
public class s_matrix
{
private int rows,columns; // can be set only during construction
private Float[][] elements;
public s_matrix()
{
this.rows = this.columns = 0;
}
public s_matrix (int rows,int columns)
{
this.rows = rows;
this.columns = columns;
if(this.rows < 0)
this.rows = 0;
if(this.columns < 0)
this.columns = 0;
elements = new Float[rows][columns];
}
public void copy(s_matrix arr)
{
this.elements = new Float[arr.get_rows()][arr.get_columns()];
this.rows = arr.get_rows();
this.columns = arr.get_columns();
for(int i=0;i<arr.get_rows();i++)
{
for(int j=0;j<arr.get_columns();j++)
this.elements[i][j] = arr.get(i,j);
}
}
public void copy(float[][] arr,int rows,int columns)
{
if(this.rows != rows || this.columns != columns)
{
System.out.println("Number of rows and columns are not matching for copying.");
return;
}
/*
now copying
*/
for(int i=0;i<rows;i++)
{
for(int j=0;j<columns;j++)
this.elements[i][j] = arr[i][j];
}
}
public void set(int pos_x,int pos_y,float val)
{
if( pos_x >= 0 && pos_x < rows && pos_y >=0 && pos_y < columns)
{
this.elements[pos_x][pos_y] = val;
}
else
{
System.out.print("Invalid position." + pos_x);
System.out.println(" "+pos_y);
}
}
public float get(int pos_x,int pos_y)
{
if( pos_x >= 0 && pos_x < rows && pos_y >=0 && pos_y < columns)
{
return this.elements[pos_x][pos_y];
}
else
System.out.println("Invalid position.");
return 0f;
}
public int get_rows()
{
return this.rows;
}
public int get_columns()
{
return this.columns;
}
public void print()
{
System.out.println("\n------------------------------------------------------");
for(int i=0;i<rows;i++)
{
for(int j=0;j<columns;j++)
System.out.printf("%.2f ",elements[i][j]);
System.out.println();
}
System.out.println("-------------------------------------------------------");
}
public s_matrix transpose()
{
s_matrix temp = new s_matrix (this.columns,this.rows);
for(int i=0;i<this.rows;i++)
{
for(int j=0;j<this.columns;j++)
temp.set(j,i,this.elements[i][j]);
}
return temp;
}
public s_matrix adjoint()
{
s_matrix ad_mat = new s_matrix(this.rows,this.columns);
int count = 0;
for(int i=0;i<this.rows;i++)
{
count = i%2;
for(int j=0;j<this.columns;j++)
{
s_matrix temp = new s_matrix();
temp.copy(this);
temp.remove_row(i);
temp.remove_column(j);
temp.print();
float ans = s_matrix.determinant(temp);
if(count %2 !=0)
ans *= -1f;
ad_mat.set(i,j,ans);
count++;
}
}
ad_mat.print();
ad_mat.copy(ad_mat.transpose());
return ad_mat;
}
public void multiply(float val)
{
for(int i=0;i<get_rows();i++)
{
for(int j=0;j<get_columns();j++)
{
set(i,j,this.get(i,j)*val);
}
}
}
public void add(float val)
{
for(int i=0;i<get_rows();i++)
{
for(int j=0;j<get_columns();j++)
{
set(i,j,this.get(i,j)+val);
}
}
}
public void remove_row(int row_index)
{
s_matrix upper_part = this.get_sub_matrix(0,row_index-1,0,this.columns-1);
s_matrix lower_part = this.get_sub_matrix(row_index+1,this.rows-1,0,this.columns-1);
this.concat_vertical(upper_part,lower_part);
}
public void remove_column(int column_index)
{
s_matrix left_part = this.get_sub_matrix(0,this.get_rows()-1,0,column_index-1);
s_matrix right_part = this.get_sub_matrix(0,this.get_rows()-1,column_index+1,this.get_columns()-1);
this.concat_horizontal(left_part,right_part);
}
public s_matrix get_sub_matrix(int start_row,int end_row,int start_col,int end_col)
{
if(!(start_row >=0 && start_row < this.rows && end_row >=0 && end_row < this.rows && start_col >=0 && start_col < this.columns
&& end_col >=0 && end_col <this.columns && start_row <= end_row && start_col <= end_col))
{
//System.out.print("Invalid sub matrix boundaries.");
return new s_matrix (0,0);
}
// System.out.println("Getting matrix from rows "+Integer.toString(start_row)+" "+Integer.toString(end_row));
// System.out.println("Getting matrix from cols "+Integer.toString(start_col)+" "+Integer.toString(end_col));
s_matrix temp = new s_matrix (end_row-start_row+1,end_col-start_col+1);
for(int i=start_row;i<=end_row;i++)
{
for(int j=start_col;j<=end_col;j++)
temp.set(i-start_row,j-start_col,this.elements[i][j]);
}
return temp;
}
public void concat_horizontal(s_matrix mat1,s_matrix mat2)
{
if( mat1.get_rows() != mat2.get_rows())
{
// System.out.println("Rows should be same for horizontal concatination.");
if(mat1.get_rows() <=0 )
{
this.copy(mat2);
}
else
this.copy(mat1);
return;
}
s_matrix temp = new s_matrix (mat1.get_rows(),mat1.get_columns()+mat2.get_columns());
for(int i=0;i<mat1.get_rows();i++)
{
for(int j=0;j<mat1.get_columns();j++)
temp.set(i,j,mat1.get(i,j));
for(int j=0;j<mat2.get_columns();j++)
temp.set(i,j+mat1.get_columns(),mat2.get(i,j));
}
this.copy(temp);
}
public void concat_vertical(s_matrix mat1,s_matrix mat2)
{
if( mat1.get_columns() != mat2.get_columns())
{
// System.out.println("Columns should be same for vertical concatination.");
if(mat1.get_columns() <= 0)
{
this.copy(mat2);
}
else
this.copy(mat1);
return;
}
s_matrix temp = new s_matrix (mat1.get_rows()+mat2.get_rows(),mat2.get_columns());
for(int i=0;i<mat1.get_columns();i++)
{
for(int j=0;j<mat1.get_rows();j++)
temp.set(j,i,mat1.get(j,i));
for(int j=0;j<mat2.get_rows();j++)
temp.set(j+mat1.get_rows(),i,mat2.get(j,i));
}
this.copy(temp);
}
public static Float determinant(s_matrix m)
{
if(m.get_rows()==1)
return m.get(0,0);
if(m.get_rows() == 2)
{
return m.get(0,0) * m.get(1,1) - m.get(1,0)*m.get(0,1);
}
s_matrix sub_mat = new s_matrix();
sub_mat.copy(m);
sub_mat.remove_row(0);
Float det= 0f;
for(int i=0;i<m.get_columns();i++)
{
s_matrix temp = new s_matrix();
temp.copy(sub_mat);
temp.remove_column(i);
Float ans = m.get(0,i) * s_matrix.determinant(temp);
if(i%2!=0)
ans *= -1f;
det += ans;
}
return det;
}
public static s_matrix inverse(s_matrix m)
{
Float det = s_matrix.determinant(m);
if(det == 0)
{
System.out.println("determinant is zero inverse not possible.");
return new s_matrix();
}
System.out.printf("determinant is %.2f\n",det);
s_matrix transposed = m.adjoint();
System.out.println("\nTransposed matrix is ");
transposed.print();
transposed.multiply(1/det);
return transposed;
}
public static s_matrix multiply(s_matrix mat1,s_matrix mat2)
{
if(mat1.get_columns() != mat2.get_rows())
{
System.out.println("Multiplication not possible.");
return new s_matrix();
}
s_matrix ans = new s_matrix(mat1.get_rows(),mat2.get_columns());
for(int i=0;i<mat1.get_rows();i++)
{
for(int j=0;j<mat2.get_columns();j++)
{
float cal = 0f;
for(int k=0;k<mat1.get_columns();k++)
{
cal += mat1.get(i,k) * mat2.get(k,j);
}
ans.set(i,j,cal);
}
}
return ans;
}
}