-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix_mult.py
More file actions
69 lines (54 loc) · 1.38 KB
/
matrix_mult.py
File metadata and controls
69 lines (54 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
def printMatrix(m):
for row in m:
print(row)
def matrixMult(A, B):
rows_A = len(A)
cols_A = len(A[0])
rows_B = len(B)
cols_B = len(B[0])
if cols_A != rows_B:
print("Invalid matrix dimensions for multiplication")
return None
elif cols_A == rows_B:
new_matrix = [[None for i in range(cols_B)] for j in range(rows_A)]
for i in range(rows_A):
for j in range(cols_B):
new_val = 0
for x in range(cols_A):
print(A[i][x], B[x][j])
new_val += A[i][x] * B[x][j]
print(f"---\n{new_val}\n---")
new_matrix[i][j] = new_val
print(new_matrix)
return new_matrix
# Testing code
# # Test1
# A = [[2, -3, 3],
# [-2, 6, 5],
# [4, 7, 8]]
# B = [[-1, 9, 1],
# [0, 6, 5],
# [3, 4, 7]]
# C = matrixMult(A, B)
# if not C == None:
# printMatrix(C)
# # Test2
# A = [[ 2, -3, 3, 0],
# [-2, 6, 5, 1],
# [ 4, 7, 8, 2]]
# B = [[-1, 9, 1],
# [ 0, 6, 5],
# [ 3, 4, 7]]
# C = matrixMult(A, B)
# if not C == None:
# printMatrix(C)
# # Test3
# A = [[ 2, -3, 3, 5],
# [-2, 6, 5, -2]]
# B = [[-1, 9, 1],
# [ 0, 6, 5],
# [ 3, 4, 7],
# [ 1, 2, 3]]
# C = matrixMult(A, B)
# if not C == None:
# printMatrix(C)