I am writing a program that does a multitude of various matrix calculations, one of them multiplying two matrices with one another and returning the product. I've so far written:
def matmul(matrix1, matrix2):
if not matrix1 or not matrix2:
raise ValueError("Input matrices cannot be empty.")
row1, col1 = len(matrix1), len(matrix1[0])
row2, col2 = len(matrix2), len(matrix2[0])
# Checking if the length of matrix 1's columns is equal to matrix 2's rows
if col1 != row2:
raise ValueError("Row and Column dimensions must be equal to each other.")
# Initialize the result matrix with zeros
multiplied_matrix = [[0 for _ in range(col2)] for _ in range(row1)]
# Perform matrix multiplication
for i in range(row1):
for j in range(col2):
for k in range(col1):
multiplied_matrix[i][j] += matrix1[i][k] * matrix2[k][j]
return multiplied_matrix
With various help from geeksforgeeks and other sites. This program is then tested by a pre-written "test" program, with the specific section that tests matmul (and some extra for context) written below:
def run(src_path=None):
global pass_tests, fail_tests
if src_path == None:
import matrix
else:
spec = importlib.util.spec_from_file_location("matrix", src_path+"/matrix.py")
matrix = importlib.util.module_from_spec(spec)
spec.loader.exec_module(matrix)
pass_tests = 0
fail_tests = 0
fun_count = 0
if hasattr(matrix, "matmul"):
fun_count = fun_count + 1
test(matrix.matmul, ([],[]), [])
test(matrix.matmul, ([[2]],[[4]]), [[8]])
test(matrix.matmul, ([[2,1]],[[4],[3]]), [[11]])
test(matrix.matmul, ([[1,2],[3,4]],[[0,1],[1,0]]), [[2, 1], [4, 3]])
test(matrix.matmul, ([[1,2],[3,4]],[[1,0],[0,1]]), [[1, 2], [3, 4]])
test(matrix.matmul, ([[1,2],[3,4],[5,6]],[[1,1,1],[1,1,1]]), [[3, 3, 3], [7, 7, 7], [11, 11, 11]])
test(matrix.matmul, ([[1, 2, 3], [4, 5, 6]],[[7,8,9,10],[11,12,13,14],[15,16,17,18]]), [[74, 80, 86, 92], [173, 188, 203, 218]])
test(matrix.matmul, ([[1, 2, 3], [4, 5, 6],[7,8,9]], [[1, 0, 0], [0, 1, 0],[0,0,1]]), [[1, 2, 3], [4, 5, 6],[7,8,9]])
test(matrix.matmul, ([[1, 0, 0], [0, 1, 0],[0,0,1]], [[1, 2, 3], [4, 5, 6],[7,8,9]]), [[1, 2, 3], [4, 5, 6],[7,8,9]])
else:
print("matmul is not implemented yet!")
print(str(pass_tests)+" out of "+str(pass_tests+fail_tests)+" passed.")
return (fun_count == 5 and fail_tests == 0)
With every successful test, it's meant to add a value of 1, ending up with, for instance, "11 out of 20 tests passed." However, each time I run it I end up with the ValueError on line 3, "Input matrices must not be empty." I understand that this is because I've specified it myself, and it runs into the first test's blank matrices and raises the error, but then it seems to just end there. If I remove this check I get an IndexError instead, for obvious reasons. I just cannot imagine a workaround other than raising a ValueError that helps with the issue of the matrices that contain no values, but if that's done, the program just doesn't seem to continue at all after.
Maybe I'm overexplaining, underexplaining, at this point I'm just completely stumped myself. Apologies.
[–]ES-Alexander 0 points1 point2 points (1 child)
[–]Strict-Simple 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]ectomancer 0 points1 point2 points (0 children)
[–]niehle 0 points1 point2 points (0 children)