forked from AllAlgorithms/java
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixMultiply.java
More file actions
Latest commit
39 lines (33 loc) · 1.07 KB
/
Copy pathMatrixMultiply.java
File metadata and controls
39 lines (33 loc) · 1.07 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
publicclassMatrixMultiply {
/**
* Multiples 2 2-dimensional matrices together given their dimensions are
* compatible
*
* @param mat_a A 2-Dimensional matrix to be multiplied
* @param mat_b A 2-Dimensional matrix to be multiplied
* @return The product of the two matrices if compatible dimensions, null
* otherwise
*/
publicstaticdouble[][] matrixMultiply2D(double[][] mat_a, double[][] mat_b) {
intaRows = mat_a.length;
intaColumns = mat_a[0].length;
intbRows = mat_b.length;
intbColumns = mat_b[0].length;
if (aColumns != bRows)
thrownewIllegalArgumentException("A:Rows: " + aColumns + " did not match B:Columns " + bRows + ".");
double[][] mat_result = newdouble[aRows][bColumns];
for (inti = 0; i < aRows; i++) {
for (intj = 0; j < bColumns; j++) {
mat_result[i][j] = 0.00000;
}
}
for (inti = 0; i < aRows; i++) { // aRow
for (intj = 0; j < bColumns; j++) { // bColumn
for (intk = 0; k < aColumns; k++) { // aColumn
mat_result[i][j] += mat_a[i][k] * mat_b[k][j];
}
}
}
returnmat_result;
}
}