Created
December 1, 2020 20:24
-
-
Save Alamin02/de3a07430ebc8cf6a149f284d8cee2cc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a = [[2, 3, 4], [3, 4, 5]] | |
b = [[4, -3, 12], [1, 1, 5], [1, 3, 2]] | |
def get_dimension(a): | |
if not type(a) == list: | |
raise ValueError('Input is not a matrix') | |
dim_x = len(a) | |
dim_y = len(a[0]) | |
# Let's check if every elements has the same length | |
for x in a: | |
if not type(x) == list: | |
raise ValueError('Input is not a matrix') | |
if not len(x) == dim_y: | |
raise ValueError('Input is not a matrix') | |
return { "x": dim_x, "y": dim_y } | |
def matrix_multiplication(a, b): | |
dim_a = get_dimension(a) | |
dim_b = get_dimension(b) | |
if (dim_a["y"] != dim_b["x"]): | |
raise ValueError('Invalid matrix dimensions for multiplication') | |
c = [ [0]*dim_b["y"] for i in range(dim_a["x"]) ] | |
for x in range(dim_a["x"]): | |
for y in range(dim_a["y"]): | |
for z in range(dim_a["y"]): | |
# does not do anything | |
c[x][y] = c[x][y] + (a[x][z] * b[z][y]) | |
return c | |
print(matrix_multiplication(a, b)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment