forked from Harvard-Westlake/SourceCodeTest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixExample.java
More file actions
120 lines (99 loc) · 3.66 KB
/
MatrixExample.java
File metadata and controls
120 lines (99 loc) · 3.66 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
import java.util.Random;
public class MatrixExample {
public static void main(String[] args) {
int[][] matrix = {
{ 1, 2, 3, 4, 5, 6 },
{ 4, 5, 6, 3, 7, 2 },
{ 27, 8, 9, 5, 3, 21 },
{ 73, 2, 19, 5, 1, 8 },
{ 47, 9, 9, 5, 0, 22 },
{ 78, 86, 1, 4, 1, 21 },
{ 73, 18, 2, 2, 5, 11 }
};
int numRows = 6;
int numCols = 7;
int[][] matrix2 = generateRandomMatrix(numRows, numCols);
int[][] result = multiplyMatrices(matrix, matrix2);
System.out.println("result length: " + result.length + " x " +
result[0].length);
for (int i = 0; i < result.length; i++) {
for (int j = 0; j < result[i].length; j++) {
System.out.print(result[i][j] + " ");
}
System.out.println();
}
// test Multiply
int[][] a = { { 1, 2, 3 }, { 4, 5, 6 } }, b = { { 7, 8 }, { 9, 10 }, { 11, 12 } };
int[][] c = { { 58, 64 }, { 139, 154 } };
testMultiply(a, b, c);
int[][] a1 = { { 2, 0 }, { 0, 2 } }, b1 = { { 1, 2 }, { 3, 4 } };
int[][] c1 = { { 2, 4 }, { 6, 8 } };
testMultiply(a1, b1, c1);
int[][] a2 = { { 3 } }, b2 = { { 4 } };
int[][] c2 = { { 12 } };
testMultiply(a2, b2, c2);
// test Generate
testGenerate(6, 7);
testGenerate(100, 100);
testGenerate(1, 2);
}
private static boolean check(int[][] x, int[][] y) {
if (x.length != y.length) {
return false;
}
for (int i = 0; i < x.length; i++) {
if (x[i].length != y[i].length) {
return false;
}
for (int j = 0; j < x[i].length; j++) {
if (x[i][j] != y[i][j]) {
return false;
}
}
}
return true;
}
private static void testMultiply(int[][] a, int[][] b, int[][] result) {
if (check(multiplyMatrices(a, b), result)) {
System.out.println("Passed.");
}
}
private static void testGenerate(int rows, int cols) {
int[][] m = generateRandomMatrix(rows, cols);
if (m.length == rows && m[0].length == cols) {
System.out.println("Passed.");
}
}
public static int[][] multiplyMatrices(int[][] matrix1, int[][] matrix2) {
int rows1 = matrix1.length;
int cols1 = matrix1[0].length;
int rows2 = matrix2.length;
int cols2 = matrix2[0].length;
if (cols1 != rows2) {
throw new IllegalArgumentException(
"Number of columns in the first matrix must be equal to the number of rows in the second matrix.");
}
int[][] result = new int[rows1][cols2];
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
for (int k = 0; k < cols1; k++) {
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
return result;
}
public static int[][] generateRandomMatrix(int numRows, int numCols) {
int[][] matrix = new int[numRows][numCols];
Random random = new Random();
if (numRows <= 0 || numCols <= 0) {
throw new IllegalArgumentException();
}
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numCols; j++) {
matrix[i][j] = random.nextInt(100);
}
}
return matrix;
}
}