-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMatrixMultiplication.java
More file actions
81 lines (73 loc) · 1.66 KB
/
Copy pathMatrixMultiplication.java
File metadata and controls
81 lines (73 loc) · 1.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
package algo;
import org.junit.Assert;
import org.junit.Test;
import java.util.Arrays;
public class MatrixMultiplication
{
// A: 2 x 5 - B: 5 x 3
public int[][] matrixMultiplication(int[][] A, int[][] B)
{
if(A[0].length != B.length)
{
throw new RuntimeException("different size");
}
int[][] result = new int[A.length][B[0].length];
for(int i=0; i<A.length; i++) // 2
{
for(int j=0; j<B[0].length; j++) // 3
{
result[i][j] = 0;
for(int k=0; k<A[0].length; k++) // 5
{
result[i][j] += A[i][k]*B[k][j];
}
}
}
return result;
}
@Test
public void empty()
{
matrixMultiplication(new int[2][10],new int[10][3]);
}
@Test(expected = RuntimeException.class)
public void differentSize()
{
matrixMultiplication(new int[2][10],new int[5][3]);
}
@Test
public void test()
{
// A = 2 x 5
// [ 1 2 3 4 5 ]
// [ 1 2 3 4 5 ]
int[][] matrixA = new int[][] {
new int[]{1,2,3,4,5},
new int[]{1,2,3,4,5}
};
// B = 5 x 3
// [ 1 2 3 ]
// [ 1 2 3 ]
// [ 1 2 3 ]
// [ 1 2 3 ]
// [ 1 2 3 ]
int[][] matrixB = new int[][] {
new int[]{1,2,3},
new int[]{1,2,3},
new int[]{1,2,3},
new int[]{1,2,3},
new int[]{1,2,3}
};
// result = 2 x 3
int[][] matrixResult = matrixMultiplication(matrixA,matrixB);
for(int i=0; i<matrixResult.length; i++)
{
for(int j=0; j<matrixResult[0].length; j++)
{
System.out.print(matrixResult[i][j]+", ");
}
Assert.assertTrue(Arrays.equals(new int[]{15,30,45},matrixResult[i]));
System.out.println();
}
}
}