-
Notifications
You must be signed in to change notification settings - Fork 1
/
matrix multiplication.py
69 lines (32 loc) · 1.19 KB
/
matrix multiplication.py
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
p = int(input("enter the column number for matrix1:"))
q = int(input("enter the column number for matrix2:"))
n = int (input("enter the column number for matrix 1/ row number matrix2:"))
# accepting matrix1 values and displaying matrix 1
print ("enter the elements for matrix 1:")
matrix1 = [[int (input ()) for i in range (n)] for j in range (p)]
print ("matrix 1:")
for i in range (p):
for j in range (n):
print(format (matrix1[i][j],"<5"), end="")
print ()
# accepting matrix2 values and displaying matrix2
print ("enter the elements for matrix2:")
matrix2 = [[int (input()) for i in range (q)] for j in range (n)]
print ("matrix2:")
for i in range (n):
for j in range (q):
print (format (matrix2[i][j],"<5"), end="")
print ()
# final matrix
result = [[0 for i in range(q)] for j in range (p)]
for i in range (p) :
for j in range (q):
for k in range (n):
result[i][j] = result[i][j] + matrix1[i][k] * matrix2[k][j]
# displaying the result
print ("the result of matrix multiplication is:")
for i in range (p):
for j in range (q):
print (format (result [i][j], "<5"), end="")
print()
####time complexity is O(n^3)######