-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumericMatrixProcessor.java
More file actions
241 lines (207 loc) · 8.41 KB
/
NumericMatrixProcessor.java
File metadata and controls
241 lines (207 loc) · 8.41 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
package processor;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.Scanner;
public class Main {
private static Scanner scan;
public static void main(String[] args) {
scan = new Scanner(System.in);
boolean running = true;
while (running) {
System.out.println("1. Add matrices");
System.out.println("2. Multiply maxtrix to a constant");
System.out.println("3. Multiply matrices");
System.out.println("4. Transpose matrix");
System.out.println("5. Calculate a determinant");
System.out.println("6. Inverse Matrix");
System.out.println("0. Exit");
int mode = scan.nextInt();
double[][] resultMatrix = new double[0][0];
switch (mode) {
case 1:
resultMatrix = addMatrices();
break;
case 2:
double[][] matrix = createMatrix("first");
System.out.print("Enter a constant: ");
int mult = scan.nextInt();
resultMatrix = multMatrixByConstant(matrix, mult);
System.out.println("The multiplication result is:");
break;
case 3:
resultMatrix = multMatrices();
break;
case 4:
System.out.println("1. Main diagonal");
System.out.println("2. Side diagonal");
System.out.println("3. Vertical line");
System.out.println("4. Horizontal line");
System.out.print("Your choice: ");
int transposeType = scan.nextInt();
resultMatrix = transposeMatrix(createMatrix("first"), transposeType);
break;
case 5:
double determinant = getDeterminant(createMatrix("first"));
System.out.println("The result is:");
System.out.println(determinant);
break;
case 6:
resultMatrix = getInverse(createMatrix("first"));
break;
default:
mode = 0;
running = false;
break;
}
if ((mode > 0 && mode < 5) || mode == 6) {
printMatrix(resultMatrix);
}
System.out.println();
}
}
private static double[][] getInverse(double[][] matrix) {
double determinant = getDeterminant(matrix);
if (determinant != 0) {
double[][] adjoint = new double[matrix.length][matrix[0].length];
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix.length; j++) {
adjoint[i][j] = getCofactor(matrix, i, j);
}
}
return multMatrixByConstant(transposeMatrix(adjoint, 1), 1 / getDeterminant(matrix));
}
return matrix;
}
private static double getDeterminant(double[][] matrix) {
double determinant = 0;
int rows = matrix.length;
int cols = matrix[0].length;
if (rows == cols) {
if (rows == 1) {
return matrix[0][0];
} else if (rows == 2) {
determinant += matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0];
} else {
for (int i = 0; i < matrix.length; i++) {
determinant += matrix[0][i] * getCofactor(matrix, 0, i);
}
}
}
return determinant;
}
private static double getCofactor(double[][] matrix, int row, int col) {
double co = Math.pow(-1, (row + 1) + (col + 1)) * getDeterminant(nextMatrix(matrix, row, col));
co = co == -0 ? 0 : co;
return co;
}
private static double[][] nextMatrix(double[][] matrix, int row, int col) {
double[][] nextMatrix = new double[matrix.length - 1][matrix[0].length - 1];
int count = 0;
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
if (i != row && j != col) {
nextMatrix[count / nextMatrix.length][count % nextMatrix.length] = matrix[i][j];
count++;
}
}
}
return nextMatrix;
}
private static double[][] transposeMatrix(double[][] matrix, int transposeType) {
double[][] resultMatrix = new double[matrix.length][matrix[0].length];
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
double num = 0;
switch (transposeType) {
case 1:
num = matrix[j][i];
break;
case 2:
num = matrix[Math.abs(j + 1 - matrix[i].length)][Math.abs(i + 1 - matrix.length)];
break;
case 3:
num = matrix[i][matrix[i].length - j - 1];
break;
case 4:
num = matrix[matrix.length - i - 1][j];
break;
default:
break;
}
resultMatrix[i][j] = num;
}
}
return resultMatrix;
}
private static double[][] multMatrices() {
double[][] firstMatrix = createMatrix("first");
double[][] secondMatrix = createMatrix("second");
double[][] resultMatrix = new double[0][0];
if (firstMatrix[0].length == secondMatrix.length) {
resultMatrix = new double[firstMatrix.length][secondMatrix[0].length];
for (int i = 0; i < resultMatrix.length; i++) {
for (int j = 0; j < resultMatrix[0].length; j++) {
double result = 0;
for (int k = 0; k < firstMatrix[i].length; k++) {
result += firstMatrix[i][k] * secondMatrix[k][j];
}
resultMatrix[i][j] = result;
}
}
}
System.out.println("The resulting matrix is: ");
return resultMatrix;
}
public static double[][] multMatrixByConstant(double[][] matrix, double mult) {
double[][] resultMatrix = new double[matrix.length][matrix[0].length];
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
double num = matrix[i][j] * mult;
resultMatrix[i][j] = num == -0 ? 0 : num;
}
}
return resultMatrix;
}
public static double[][] addMatrices() {
double[][] firstMatrix = createMatrix("first");
double[][] secondMatrix = createMatrix("second");
double[][] resultMatrix = new double[0][0];
if (firstMatrix.length == secondMatrix.length && firstMatrix[0].length == secondMatrix[0].length) {
resultMatrix = new double[firstMatrix.length][firstMatrix[0].length];
for (int i = 0; i < firstMatrix.length; i++) {
for (int j = 0; j < firstMatrix[0].length; j++) {
resultMatrix[i][j] = firstMatrix[i][j] + secondMatrix[i][j];
}
}
}
return resultMatrix;
}
private static double[][] createMatrix(String num) {
System.out.print("Enter size of " + num + " matrix: ");
int dy = scan.nextInt();
int dx = scan.nextInt();
System.out.println("Enter " + num + " matrix:");
double[][] matrix = new double[dy][dx];
for (int i = 0; i < dy; i++) {
for (int j = 0; j < dx; j++) {
matrix[i][j] = scan.nextDouble();
}
}
return matrix;
}
private static void printMatrix(double[][] matrix) {
DecimalFormat df = new DecimalFormat("#0.00");
df.setRoundingMode(RoundingMode.FLOOR);
for (double[] row : matrix) {
StringBuilder str = new StringBuilder();
for (double d : row) {
if (d >= 0) {
str.append(" ");
}
str.append(df.format(d)).append(" ");
}
System.out.println(str.toString().replaceAll("\\s++$", ""));
}
System.out.println();
}
}