Skip to content

Fixed matrix multiplication, added input checks #879

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 77 additions & 12 deletions matrix/matrix_multiplication_addition.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
def add(matrix_a, matrix_b):
rows = len(matrix_a)
columns = len(matrix_a[0])
if not is_matrix(matrix_a) or not is_matrix(matrix_b):
raise ValueError('One or more of the arguments of multiply is not a matrix!')
size_a = matrix_size(matrix_a)
size_b = matrix_size(matrix_b)
if size_a[0] != size_b[0] or size_a[1] != size_b[1]:
raise ValueError('Matrices are incompatible in size')
rows = size_a[0]
columns = size_a[1]
matrix_c = []
for i in range(rows):
list_1 = []
Expand All @@ -14,30 +20,46 @@ def scalarMultiply(matrix , n):
return [[x * n for x in row] for row in matrix]

def multiply(matrix_a, matrix_b):
'''Returns the matrix product of two matrices, expressed as a list of lists'''

if not is_matrix(matrix_a) or not is_matrix(matrix_b):
raise ValueError('One or more of the arguments of multiply is not a matrix!')
size_a = matrix_size(matrix_a)
size_b = matrix_size(matrix_b)
if(not size_a[0] == size_b[1]):
raise ValueError('Matrices are incompatible in size')
matrix_c = []
n = len(matrix_a)
for i in range(n):
list_1 = []
for j in range(n):
val = 0
for k in range(n):
val = val + matrix_a[i][k] * matrix_b[k][j]
list_1.append(val)
matrix_c.append(list_1)
iteration_num = 0

for i in range(size_a[0]):
row_c = []
for j in range(size_b[1]):
element_c = 0
#We previously checked that the number of columns in a is the number of rows in b
for k in range(size_a[1]):
element_c += matrix_a[i][k] * matrix_b[k][j]
row_c.append(element_c)
matrix_c.append(row_c)
return matrix_c

def identity(n):
return [[int(row == column) for column in range(n)] for row in range(n)]

def transpose(matrix):
if not is_matrix(matrix):
raise ValueError('Argument is not a matrix!')
return map(list , zip(*matrix))

def minor(matrix, row, column):
if not is_matrix(matrix):
raise ValueError('Argument is not a matrix!')
minor = matrix[:row] + matrix[row + 1:]
minor = [row[:column] + row[column + 1:] for row in minor]
return minor

def determinant(matrix):
if not is_matrix(matrix):
raise ValueError('Argument is not a matrix!')
if len(matrix) == 1: return matrix[0][0]

res = 0
Expand All @@ -46,6 +68,8 @@ def determinant(matrix):
return res

def inverse(matrix):
if not is_matrix(matrix):
raise ValueError('Argument is not a matrix!')
det = determinant(matrix)
if det == 0: return None

Expand All @@ -57,15 +81,56 @@ def inverse(matrix):
cofactors = [[x * (-1) ** (row + col) for col, x in enumerate(matrixMinor[row])] for row in range(len(matrix))]
adjugate = transpose(cofactors)
return scalarMultiply(adjugate , 1/det)

def check_matrix_size_consistency(matrix):
'''Checks that each row in a matrix has the same number of elements'''
if(is_list(matrix)):
for element in matrix:
if(not is_list(element)):
return False
if(len(matrix[0]) != len(element)):
return False
return True;

def is_list(element):
'''Checks is a variable is a list'''
return isinstance(element, list)

def is_int_or_float(element):
'''Checks is a variable is an int or a float'''
isinstance(element, (int, float))

def is_matrix(matrix):
'''Checks is a variable is a matrix'''
#A variable is a matrix if and only if it is a list of lists of numbers (ints or floats)
if(is_list(matrix)):
for row in matrix:
if(is_list(row)):
for element in row:
if(not is_int_or_float(element)):
return False
else:
return False
else:
return False
return True

def matrix_size(matrix):
'''Returns the size of a matrix as a list [rows, columns]'''
if(not is_matrix(matrix)):
raise ValueError('argument is not a matrix!')
return [len(matrix), len(matrix[0])]

def main():
matrix_a = [[12, 10], [3, 9]]
matrix_b = [[3, 4], [7, 4]]
matrix_c = [[11, 12, 13, 14], [21, 22, 23, 24], [31, 32, 33, 34], [41, 42, 43, 44]]
matrix_d = [[3, 0, 2], [2, 0, -2], [0, 1, 1]]
matrix_e = [ [1, 2], [2, 1] , [1, 1]]
matrix_f = [ [1, 2, 5], [2, 1, 1] ]

print(add(matrix_a, matrix_b))
print(multiply(matrix_a, matrix_b))
print(multiply(matrix_e, matrix_f))
print(identity(5))
print(minor(matrix_c , 1 , 2))
print(determinant(matrix_b))
Expand Down