-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathA9.py
executable file
·164 lines (119 loc) · 3.87 KB
/
A9.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# DSL – ASSIGNMENT 3 – A9
class Matrix:
def __init__(self, row, col):
self.row = row
self.col = col
self.matrix_A = [i[:] for i in [[0]*self.col]*self.row]
self.matrix_B = [i[:] for i in [[0]*self.col]*self.row]
self.matrix_C = [i[:] for i in [[0]*self.col]*self.row]
def getinp(self, mat):
for i in range(self.row):
for j in range(self.col):
mat[i][j] = int(input(f'Enter the element of row {i+1} and column {j+1}: '))
def printmat(self, mat):
for i in mat:
print('\t'.join(map(str, i)))
def getmatrix(self):
print('\n\nEnter Matrix A data: ')
self.getinp(self.matrix_A)
print('\nEnter Matrix B data: ')
self.getinp(self.matrix_B)
print('\n\nMatrix A')
self.printmat(self.matrix_A)
print('\nMatrix B')
self.printmat(self.matrix_B)
def addition(self):
for i in range(self.row):
for j in range(self.col):
self.matrix_C[i][j] = self.matrix_A[i][j] + self.matrix_B[i][j]
print("\n\nAddition matrix: ")
self.printmat(self.matrix_C)
def subtraction(self):
for i in range(self.row):
for j in range(self.col):
self.matrix_C[i][j] = self.matrix_A[i][j] - self.matrix_B[i][j]
print("\n\nSubtraction matrix: ")
self.printmat(self.matrix_C)
def multiply(self):
for i in range(self.row):
for j in range(self.col):
self.matrix_C[i][j] = 0
for k in range(self.row):
self.matrix_C[i][j] += self.matrix_A[i][k] * self.matrix_B[k][j]
print("\n\nMultiplication matrix: ")
self.printmat(self.matrix_C)
def transpose(self):
for i in range(self.row):
for j in range(self.col):
self.matrix_C[i][j] = self.matrix_A[j][i]
print("\n\nTranspose of Matrix A: ")
self.printmat(self.matrix_C)
for i in range(self.row):
for j in range(self.col):
self.matrix_C[i][j] = self.matrix_B[j][i]
print("\n\nTranspose of Matrix B: ")
self.printmat(self.matrix_C)
def main():
row = int(input('\nEnter number of rows: '))
col = int(input('Enter number of columns: '))
mat = Matrix(row, col)
mat.getmatrix()
mat.addition()
mat.subtraction()
mat.multiply()
mat.transpose()
if __name__ == "__main__":
main()
"""
---------------- OUTPUT -----------------
Enter number of rows: 3
Enter number of columns: 3
Enter Matrix A data:
Enter the element of row 1 and column 1: 1
Enter the element of row 1 and column 2: 5
Enter the element of row 1 and column 3: 2
Enter the element of row 2 and column 1: 4
Enter the element of row 2 and column 2: 3
Enter the element of row 2 and column 3: 2
Enter the element of row 3 and column 1: 7
Enter the element of row 3 and column 2: 4
Enter the element of row 3 and column 3: 5
Enter Matrix B data:
Enter the element of row 1 and column 1: 5
Enter the element of row 1 and column 2: 4
Enter the element of row 1 and column 3: 1
Enter the element of row 2 and column 1: 2
Enter the element of row 2 and column 2: 7
Enter the element of row 2 and column 3: 4
Enter the element of row 3 and column 1: 3
Enter the element of row 3 and column 2: 6
Enter the element of row 3 and column 3: 1
Matrix A
1 5 2
4 3 2
7 4 5
Matrix B
5 4 1
2 7 4
3 6 1
Addition matrix:
6 9 3
6 10 6
10 10 6
Subtraction matrix:
-4 1 1
2 -4 -2
4 -2 4
Multiplication matrix:
21 51 23
32 49 18
58 86 28
Transpose of Matrix A:
1 4 7
5 3 4
2 2 5
Transpose of Matrix B:
5 2 3
4 7 6
1 4 1
"""