forked from williamfiset/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MatrixMultiplication.java
42 lines (36 loc) · 1.03 KB
/
MatrixMultiplication.java
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
/**
* Multiply two matrices together and get their product
*
* <p>Time Complexity: O(n^3)
*
* @author Micah Stairs
*/
package com.williamfiset.algorithms.linearalgebra;
class MatrixMultiplication {
// Returns the result of the product of the matrices 'a' and 'b'
// or null if the matrices are the wrong dimensions
static double[][] multiply(double[][] a, double[][] b) {
int aRows = a.length, aCols = a[0].length;
int bRows = b.length, bCols = b[0].length;
if (aCols != bRows) return null;
double[][] c = new double[aRows][bCols];
for (int i = 0; i < aRows; i++)
for (int j = 0; j < bCols; j++) for (int k = 0; k < aCols; k++) c[i][j] += a[i][k] * b[k][j];
return c;
}
public static void main(String[] args) {
double[][] a = {
{1, 2, 3, 4},
{4, 3, 2, 1},
{1, 2, 2, 1}
};
double[][] b = {
{1, 0},
{2, 1},
{0, 3},
{0, 0}
};
double[][] c = multiply(a, b);
for (double[] row : c) System.out.println(java.util.Arrays.toString(row));
}
}