Skip to content

Instantly share code, notes, and snippets.

@AndreaNicola
Created September 21, 2019 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndreaNicola/5056bf5a117123efb92793385274481e to your computer and use it in GitHub Desktop.
Save AndreaNicola/5056bf5a117123efb92793385274481e to your computer and use it in GitHub Desktop.
public class MatrixMultiply {
static void printMatrix(double[][] a) {
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[0].length; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}
System.out.println();
}
static double[][] multiply(double[][] a, double[][] b) {
if (a[0].length != b.length) {
throw new RuntimeException("Matrix are not compatible");
}
double[][] c = new double[a.length][b[0].length];
for (int i = 0; i < c.length; i++) {
for (int j = 0; j < c[0].length; j++) {
for (int k =0 ; k < c[0].length; k++) {
c[i][j] = a[i][k] * b[k][j] + c[i][j];
}
}
}
return c;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment