-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdotproduct.groovy
41 lines (35 loc) · 1.13 KB
/
dotproduct.groovy
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
public class dotproduct {
public static void main(String[] args) {
int[][] matrix1 = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int[][] matrix2 = {
{10, 11, 12},
{13, 14, 15},
{16, 17, 18}
};
int dotProduct = calculateDotProduct(matrix1, matrix2);
System.out.println("Dot Product: " + dotProduct);
}
public static int calculateDotProduct(int[][] matrix1, int[][] matrix2) {
int rows1 = matrix1.length;
int cols1 = matrix1[0].length;
int cols2 = matrix2[0].length;
if (cols1 != matrix2.length) {
throw new IllegalArgumentException("Number of columns in matrix1 must match the number of rows in matrix2");
}
int dotProduct = 0;
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
int sum = 0;
for (int k = 0; k < cols1; k++) {
sum += matrix1[i][k] * matrix2[k][j];
}
dotProduct += sum;
}
}
return dotProduct;
}
}