Skip to content

Instantly share code, notes, and snippets.

@arlyon
Last active September 25, 2017 16:55
Show Gist options
  • Save arlyon/e882f2486da476914b0c24dcc5131631 to your computer and use it in GitHub Desktop.
Save arlyon/e882f2486da476914b0c24dcc5131631 to your computer and use it in GitHub Desktop.
def matrix_multiplication(matrix1, matrix2):
# m * n x n * p => m * p
m = matrix1.width
n1 = matrix1.height
n2 = matrix2.width
p = matrix2.height
# if the n's of the matrices don't match then fail
if (n1 != n2):
fail("Invalid matrix dimensions! Expected {n1} columns in second argument, but only received {n2}.")
# create 2d array
output = new int[m][p]
for column x in output:
for row y in output:
output[x][y] = product(matrix1.getColumn(x)) + product(matrix2.getRow(y))
return output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment