-
Notifications
You must be signed in to change notification settings - Fork 0
/
A_9_completed.py
127 lines (116 loc) · 3.53 KB
/
A_9_completed.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
def take_matrix(m,n):
out=[]
for i in range(m):
row=[]
for j in range(n):
element=int(input(f"Enter Element [{i}][{j}] :"))
row.append(element)
out.append(row)
return out
#function to add two matrices
def addition():
#take no. of rows and columns
m=int(input("Enter Rows Of Matrix : "))
n=int(input("Enter Columns Of Matrix : "))
#take matrices A and B
print("\n\tMATRIX A")
A = take_matrix(m,n)
print("\n\tMATRIX B")
B = take_matrix(m,n)
#calculate sum
result=[]
for i in range(m):
row =[]
for j in range(n):
row.append(A[i][j]+B[i][j])
result.append(row)
return result
#function to subtract two matrices
def subtraction():
#take no. of rows and columns
m=int(input("Enter Rows Of Matrix : "))
n=int(input("Enter Columns Of Matrix : "))
#take matrices A and B
print("\n\tMATRIX A")
A = take_matrix(m,n)
print("\n\tMATRIX B")
B = take_matrix(m,n)
#calculate difference
result=[]
for i in range(m):
row =[]
for j in range(n):
row.append(A[i][j]-B[i][j])
result.append(row)
return result
#function to multiply matrices A and B
def multiplication():
#take rows and columns of matrix A
m=int(input("Enter Rows Of Matrix : "))
n=int(input("Enter Columns Of Matrix : "))
print("\n\tMATRIX A")
A = take_matrix(m,n)
#Take no. of columns and keep rows = columns of previous matrix
print("\nNo. of rows are : ",n)
x=int(input("Enter Columns : "))
print("\n\tMATRIX B")
B = take_matrix(n,x)
#calculate product
result = []
for i in range(len(A)):
collector =[]
for j in range(len(B[0])):
temp = []
for k in range(len(B)):
temp.append(A[i][k]*B[k][j])
row = sum(temp)
collector.append(row)
result.append(collector)
return result
#function for transpose
def transpose():
#take no. of rows and columns
m=int(input("Enter Rows and Columns Of Matrix : "))
n=m #as transpose is possible on square matrix only
#take matrix A
print("\n\tMATRIX A")
A = take_matrix(m,n)
#find transpose
result=[]
for i in range(m):
row =[]
for j in range(n):
row.append(A[j][i])
result.append(row)
return result
# Main Code
while (1):
print("\n\t\t***** MATRIX OPERATIONS *****\n")
print("1.Addition\n2.Subtraction\n3.Multiplication\n4.Transpose\n5.Exit\n")
ch = int(input("Enter Your Choice : "))
print("")
if ch == 1:
W = addition()
print("\nSum of Matrices A and B is : ")
for row in W:
print(row)
elif ch == 2:
X = subtraction()
print("\nDifference of Matrices A and B is : ")
for row in X:
print(row)
elif ch == 3:
Y = multiplication()
print("\nProduct of Matrices A and B is : ")
for row in Y:
print(row)
elif ch == 4:
Z = transpose()
print("\nTranspose of Matrix A is : ")
for row in Z:
print(row)
elif ch == 5:
print("\t\t*****TERMINATED SUCCESSFULLY*****\n")
break
else:
print("Invalid Choice\n")