Repository navigation
Expand file tree
/
Copy pathMatrix.java
More file actions
269 lines (230 loc) · 9.24 KB
/
Copy pathMatrix.java
File metadata and controls
269 lines (230 loc) · 9.24 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
import java.util.Scanner;
public class Matrix {
/** data memebers */
int rows;
int cols;
Object[][] element;
/** Constructor for matrix with different number of rows and cols */
public Matrix(int rows, int cols) {
this.rows = rows;
this.cols = cols;
element = new Object[rows][cols];
}
/** Constructor for square matrix */
public Matrix(int rows) {
this.rows = rows;
this.cols = rows;
element = new Object[rows][cols];
}
/**
* Reads the matrix from the standard input.
*/
public void read() {
Scanner sc = new Scanner(System.in);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print("Enter the element at index (" + i + ", " + j + "): ");
element[i][j] = sc.nextDouble();
}
}
}
/**
* Prints the matrix.
*/
public void print() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print(element[i][j] + " ");
}
System.out.println();
}
System.out.println();
}
/**
* Adds two matrices.
* @return Matrix
* @throws IllegalArgumentException if the matrices are not of the same size.
*/
public Matrix add(Matrix other) {
// Check if the matrices are of the same size.
if (rows != other.rows || cols != other.cols) {
throw new IllegalArgumentException("Matrices are not of the same size.");
}
Matrix result = new Matrix(rows, cols);
for(int i = 0; i < rows; i++) {
for(int j = 0; j < rows; j++) {
result.element[i][j] = (double)element[i][j] + (double)other.element[i][j];
}
}
return result;
}
/**
* Multiplies Two Matrices
* @throws IllegalArgumentException if the matrices are not of the same size.
* @return Matrix
*/
public Matrix multiply(Matrix other) {
// Check if the matrices are of the same size.
if (cols != other.rows) {
throw new IllegalArgumentException("Matrices are not of the same size.");
}
Matrix result = new Matrix(rows, other.cols);
for(int i = 0; i < rows; i++) {
for(int j = 0; j < other.cols; j++) {
result.element[i][j] = 0.0;
for(int k = 0; k < cols; k++) {
// print values of i, j, k
System.out.println("i = " + i + ", j = " + j + ", k = " + k);
System.out.println("result.element[i][j] = "+result.element[i][j]);
System.out.println("element[i][k] = "+element[i][k]);
System.out.println("other.element[k][j] = "+other.element[k][j]);
// Print entire multplication process to console for better understanding
System.out.println("result.element[" + i + "][" + j + "] = " + result.element[i][j] + " + " + element[i][k] + " * " + other.element[k][j]);
result.element[i][j] = (double)result.element[i][j] + (double)element[i][k] * (double)other.element[k][j];
}
}
}
return result;
}
/**
* Returns the determinant of the matrix
* First converts the matrix into UTM By using the Gauss Elimination Method
* Then multiplies the diagonal elements to get the determinant
* @return determinant of the matrix
* @throws IllegalArgumentException if the matrix is not square.
*/
public double determinant() {
if(rows != cols) {
throw new IllegalArgumentException("Matrix is not square.");
}
// Create a temporary matrix
Matrix temp = new Matrix(rows);
for(int i = 0; i < rows; i++) {
for(int j = 0; j < rows; j++) {
temp.element[i][j] = (double)element[i][j];
}
}
// Convert the matrix to a Upper Triangular Matrix (forward operation)
for(int k = 0; k <= rows - 2; k++) {
double x = (double)temp.element[k][k];
for(int i = k + 1; i < rows; i++) {
double y = (double)temp.element[i][k];
for(int j = 0; j < cols; j++) {
// Print values for better understanding
System.out.println("x = " + x + ", y = " + y + ", temp.element[" + i + "][" + j + "] = " + temp.element[i][j] + ", temp.element[" + k + "][" + j + "] = " + temp.element[k][j]);
System.out.println("temp.element[" + i + "][" + j + "] = " + temp.element[i][j] + " - " + temp.element[k][j] + " * " + y + " / " + x);
temp.element[i][j] = (double)temp.element[i][j] - (double)temp.element[k][j] * y / x;
// Print the updated value
temp.print();
}
}
}
// Multiply the diagonal elements to get the determinant
double det = 1;
for(int i = 0; i < rows; i++) {
det *= (double)temp.element[i][i];
}
return det;
}
/**
* Returns the inverse of the matrix
* Create a identity matrix of the same size as the matrix
* First converts the matrix into UTM By using the Gauss Elimination Method and perform the same operations on the identity matrix
* Then converts the matrix into LTM By using the Gauss Elimination Method and perform the same operations on the identity matrix
* Once the matrix is converted to diagonal matrix, make the diagonal elements 1 and perform the same operations on the identity matrix
* The resulting matrix will be the inverse of the original matrix
*
* @return Matrix
* @throws IllegalArgumentException if the matrix is not square.
*/
public Matrix inverse() {
if(rows != cols) {
throw new IllegalArgumentException("Matrix is not square.");
}
// Create a temporary matrix
Matrix temp = new Matrix(rows);
for(int i = 0; i < rows; i++) {
for(int j = 0; j < rows; j++) {
temp.element[i][j] = (double)element[i][j];
}
}
// Create an identity matrix
Matrix identity = new Matrix(rows);
for(int i = 0; i < rows; i++) {
for(int j = 0; j < rows; j++) {
if(i == j) {
identity.element[i][j] = 1.0;
} else {
identity.element[i][j] = 0.0;
}
}
}
// Convert the matrix to a Upper Triangular Matrix
for(int k = 0; k <= rows - 2; k++) {
double x = (double) temp.element[k][k];
for(int i = k + 1; i < rows; i++) {
double y = (double) temp.element[i][k];
for(int j = 0; j < cols; j++) {
temp.element[i][j] = (double)temp.element[i][j] - (double)temp.element[k][j] * y / x;
identity.element[i][j] = (double)identity.element[i][j] - (double)identity.element[k][j] * y / x;
}
}
}
// Convert the matrix to a Lower Triangular Matrix
for(int k = rows - 1; k >= 1; k--) {
double x = (double)temp.element[k][k];
for(int i = 0; i <= k - 1; i++) {
double y = (double)temp.element[i][k];
for(int j = 0; j < cols; j++) {
temp.element[i][j] = (double)temp.element[i][j] - (double)temp.element[k][j] * y / x;
identity.element[i][j] = (double)identity.element[i][j] - (double)identity.element[k][j] * y / x;
}
}
}
// Make Diagonal elements 1
for(int i = 0; i < rows; i++) {
double x = (double)temp.element[i][i];
for(int j = 0; j < cols; j++) {
temp.element[i][j] = (double)temp.element[i][j] / x;
identity.element[i][j] = (double)identity.element[i][j] / x;
}
}
return identity;
}
/**
* Returns the transpose of the matrix
* @return Matrix
*/
public Matrix transpose() {
Matrix transpose = new Matrix(cols, rows);
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
transpose.element[j][i] = element[i][j];
}
}
return transpose;
}
/** Main Method */
public static void main(String[] args) {
Matrix m1 = new Matrix(3);
m1.read();
m1.print();
// System.out.println("Transpose: ");
// m1.transpose().print();
// System.out.println("Inverse: ");
// m1.inverse().print();
System.out.print("Determinant: "+m1.determinant()+"\n");
// Matrix m2 = new Matrix(3);
// m2.read();
// m2.print();
// System.out.println("Inverse: ");
// m2.inverse().print();
// System.out.print("Determinant: "+m2.determinant()+"\n");
// Matrix m3 = m1.add(m2);
// System.out.println("Sum of the two matrices is: ");
// m3.print();
// Matrix m4 = m1.multiply(m2);
// System.out.println("Product of the two matrices is: ");
// m4.print();
}
}