-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixUtil.java
More file actions
314 lines (255 loc) · 8.74 KB
/
Copy pathMatrixUtil.java
File metadata and controls
314 lines (255 loc) · 8.74 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
304
305
306
307
308
309
310
311
312
313
314
package com.szkingdom.util;
import java.util.Scanner;
/**
* 矩阵工具类
* <p>
* m*n -- m行n列
*
* @author Tongch
* @version 1.0
* @time 2019/2/19 14:59
*/
public class MatrixUtil {
public static double[][] staticMatrix = null;
public static void main(String[] args) throws Exception {
System.out.println("欢迎进入矩阵计算器,请选择要进行的操作:\n" +
"1.相加\n" +
"2.相减\n" +
"3.相乘\n" +
"4.反转\n"
);
System.out.println("请输入要进行操作的序号:");
Scanner sc = new Scanner(System.in);
handleMatrix(sc.nextInt());
sc.close();
}
public static void handleMatrix(int order) throws Exception {
double[][] matrix1 = null;
double[][] matrix2 = null;
switch (order) {
case 1:
matrix1 = inputMatrix(1);
matrix2 = inputMatrix(2);
showMatrix(addMatrix(matrix1, matrix2), "当前计算结果为:");
break;
case 2:
matrix1 = inputMatrix(1);
matrix2 = inputMatrix(2);
showMatrix(subMatrix(matrix1, matrix2), "当前计算结果为:");
break;
case 3:
matrix1 = inputMatrix(1);
matrix2 = inputMatrix(2);
showMatrix(multiplyMatrix(matrix1, matrix2), "当前计算结果为:");
break;
case 4:
matrix1 = inputMatrix(1);
showMatrix(revertMatrix(matrix1), "当前计算结果为:");
break;
}
}
/**
* 矩阵的输入
*
* @return
*/
public static double[][] inputMatrix(int number) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入第 " + number +
" 个矩阵的行数(按回车结束):");
int row = sc.nextInt();
System.out.println("请输入第 " + number +
" 个矩阵的列数(按回车结束):");
int column = sc.nextInt();
double[][] inputMatrix = new double[row][column];
for (int i = 0; i < row; i++) {
System.out.println("请输入第 " + (i + 1) + " 行的数据");
for (int j = 0; j < column; j++) {
System.out.println("请输入( " + (i + 1) + " , " + (j + 1) + " )的数据: ");
inputMatrix[i][j] = sc.nextInt();
}
}
showMatrix(inputMatrix, "");
return inputMatrix;
}
/**
* 对矩阵的校验
*
* @param matrix1
* @return
* @throws Exception
*/
public static boolean checkRevertMatrix(double[][] matrix1) throws Exception {
if (matrix1 == null) {
throw new Exception("矩阵为空");
}
return true;
}
/**
* 转置矩阵
*
* @param matrix1
* @return
*/
public static double[][] revertMatrix(double[][] matrix1) throws Exception {
if (checkRevertMatrix(matrix1)) {
int row = getMatrixRow(matrix1);
int column = getMatrixColumn(matrix1);
double[][] resultMatrix = new double[column][row];
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
resultMatrix[j][i] = matrix1[i][j];
}
}
return resultMatrix;
}
return staticMatrix;
}
/***
* 矩阵相乘时的校验
*
* todo:矩阵为0时没有校验
* @param matrix1
* @param matrix2
* @return
*/
public static boolean checkMultiplyMatrix(double[][] matrix1, double[][] matrix2) throws Exception {
if (matrix1 == null) {
throw new Exception("矩阵1 为空");
}
if (matrix2 == null) {
throw new Exception("矩阵2 为空");
}
if (getMatrixColumn(matrix1) != getMatrixRow(matrix2)) {
throw new Exception("矩阵1的列数与矩阵2的行数不一致,无法相乘");
}
return true;
}
/**
* 矩阵相乘
*
* @param matrix1
* @param matrix2
* @return
*/
public static double[][] multiplyMatrix(double[][] matrix1, double[][] matrix2) throws Exception {
if (checkMultiplyMatrix(matrix1, matrix2)) {
int row = getMatrixRow(matrix1);
int column = getMatrixColumn(matrix2);
double[][] resultMatrix = new double[row][column];
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
double sum = 0;
for (int k = 0; k < getMatrixColumn(matrix1); k++) {
sum += matrix1[i][k] * matrix2[k][j];
}
resultMatrix[i][j] = sum;
}
}
return resultMatrix;
}
return staticMatrix;
}
/**
* 对矩阵相加时的校验
* <p>
* todo:矩阵为0时没有校验
*
* @param matrix1
* @param matrix2
* @return
*/
public static boolean checkAddMethod(double[][] matrix1, double[][] matrix2) throws Exception {
if (matrix1 == null) {
throw new Exception("矩阵1为空");
}
if (matrix2 == null) {
throw new Exception("矩阵2为空");
}
if (getMatrixRow(matrix1) != getMatrixRow(matrix2)) {
throw new Exception("两个矩阵的行数不相同,无法相加");
}
if (getMatrixColumn(matrix1) != getMatrixColumn(matrix2)) {
throw new Exception("两个矩阵的列数不相同,无法相加");
}
return true;
}
/**
* 矩阵的相加
*
* @param matrix1
* @param matrix2
* @return
*/
public static double[][] addMatrix(double[][] matrix1, double[][] matrix2) throws Exception {
if (checkAddMethod(matrix1, matrix2)) {
int row = getMatrixRow(matrix1);
int column = getMatrixColumn(matrix1);
double[][] resultMatrix = new double[row][column];
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
resultMatrix[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
return resultMatrix;
}
return staticMatrix;
}
/**
* 矩阵的相减
*
* @param matrix1
* @param matrix2
* @return
*/
public static double[][] subMatrix(double[][] matrix1, double[][] matrix2) throws Exception {
if (checkAddMethod(matrix1, matrix2)) {
int row = getMatrixRow(matrix1);
int column = getMatrixColumn(matrix1);
double[][] resultMatrix = new double[row][column];
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
resultMatrix[i][j] = matrix1[i][j] - matrix2[i][j];
}
}
return resultMatrix;
}
return staticMatrix;
}
/**
* 获取矩阵的列数
*
* @param matrix1
* @return
*/
public static int getMatrixColumn(double[][] matrix1) {
return matrix1.length == 0 ? 0 : matrix1[0].length;
}
/**
* 获取矩阵的行数
*
* @param matrix1
* @return
*/
public static int getMatrixRow(double[][] matrix1) {
return matrix1.length == 0 ? 0 : matrix1.length;
}
/**
* 打印矩阵
*
* @param matrix1
* @param message
*/
public static void showMatrix(double[][] matrix1, String message) {
int row = getMatrixRow(matrix1);
int column = getMatrixColumn(matrix1);
message = (message == null || message == "") ? "当前矩阵为 : " : message;
System.out.println(message);
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
System.out.print(matrix1[i][j] + " ");
}
System.out.println();
}
}
}