1- """
2- 9. WAPP to compute following computation on matrix:
3-
4- a) Addition of two matrices
5- b) Subtraction of two matrices
6- c) Multiplication of two matrices
7- d) Transpose of a matrix
8- """
9-
10- import numpy
11- x = numpy .array ([[1 , 2 ], [4 , 5 ]])
12- y = numpy .array ([[7 , 8 ], [9 , 10 ]])
13- print ("Addition of matrices is: " )
14- print (numpy .add (x ,y ))
15-
16- print ("Subtraction of the two matrices: " )
17- print (numpy .subtract (x ,y ))
18- print ("The product of the two matrices is: " )
19- print (numpy .dot (x ,y ))
20- print ("The transpose of given matrix is: " )
21- print (x .T )
1+ class Matrices :
2+ def __init__ (self ):
3+ self .rows = 0
4+ self .columns = 0
5+ self .rowsOther = 0
6+ self .columnsOther = 0
7+ self .matrix = []
8+ self .matrix2 = []
9+ self .matrix3 = []
10+ self .matrix4 = []
11+ self .matrix5 = []
12+
13+ def inputM (self ):
14+ self .rows = int (input ("Enter number of rows: " ))
15+ self .columns = int (input ("Enter number of columns: " ))
16+ print (f"You will enter { self .rows * self .columns } elements individually in Row by Row fashion" )
17+ for i in range (self .rows ):
18+ row = []
19+ for j in range (self .columns ):
20+ m = int (input (f"Enter element ({ i } , { j } ): " ))
21+ row .append (m )
22+ self .matrix .append (row )
23+
24+ def displayM (self ):
25+ print ("==========" )
26+ for i in range (self .rows ):
27+ for j in range (self .columns ):
28+ print (self .matrix [i ][j ], end = " " )
29+ print ()
30+
31+ def transpose (self ):
32+ print ("==========" )
33+ print ("Tranpose: " )
34+ for i in range (self .columns ):
35+ for j in range (self .rows ):
36+ print (self .matrix [j ][i ], end = " " )
37+ print ()
38+ print ("==========" )
39+
40+ def inputM2 (self ):
41+ self .rowsOther = int (input ("Enter number of rows: " ))
42+ self .columnsOther = int (input ("Enter number of columns: " ))
43+ print (f"You will enter { self .rowsOther * self .columnsOther } elements individually in Row by Row fashion" )
44+ self .matrix2 = []
45+ for i in range (self .rowsOther ):
46+ rowOther = []
47+ for j in range (self .columnsOther ):
48+ m = int (input (f"Enter element ({ i } , { j } ): " ))
49+ rowOther .append (m )
50+ self .matrix2 .append (rowOther )
51+
52+ def displayM2 (self ):
53+ print ("==========" )
54+ print ("Second Matrix:" )
55+ for i in range (self .rowsOther ):
56+ for j in range (self .columnsOther ):
57+ print (self .matrix2 [i ][j ], end = " " )
58+ print ()
59+
60+ def addition (self ):
61+ if self .rows != self .rowsOther or self .columns != self .columnsOther :
62+ print ("Matrices cannot be added." )
63+ else :
64+ for i in range (self .rows ):
65+ M3 = []
66+ for j in range (self .columns ):
67+ c = self .matrix [i ][j ] + self .matrix2 [i ][j ]
68+ M3 .append (c )
69+ self .matrix3 .append (M3 )
70+ print ("==========" )
71+ print ("Addition is: " )
72+ for i in range (self .rowsOther ):
73+ for j in range (self .columnsOther ):
74+ print (self .matrix3 [i ][j ], end = " " )
75+ print ()
76+ print ("==========" )
77+
78+ def subtraction (self ):
79+ if self .rows != self .rowsOther or self .columns != self .columnsOther :
80+ print ("Matrices cannot be subtracted." )
81+ else :
82+ for i in range (self .rows ):
83+ M3 = []
84+ for j in range (self .columns ):
85+ c = self .matrix [i ][j ] - self .matrix2 [i ][j ]
86+ M3 .append (c )
87+ self .matrix3 .append (M3 )
88+ print ("==========" )
89+ print ("Subtraction is: " )
90+ for i in range (self .rowsOther ):
91+ for j in range (self .columnsOther ):
92+ print (self .matrix3 [i ][j ], end = " " )
93+ print ()
94+ print ("==========" )
95+
96+ def multiplication (self ):
97+ if self .rowsOther != self .columns :
98+ print ("Matrices cannot be multiplied." )
99+ return
100+ else :
101+ for i in range (self .rows ):
102+ row = []
103+ for i in range (self .columnsOther ):
104+ row .append (0 )
105+ self .matrix5 .append (row )
106+ # Or self.matrix5 = [[0] * self.columnsOther for _ in range(self.rows)]
107+ # self.matrix5 = [[0 for _ in range(self.columnsOther)] for _ in range(self.rows)]
108+ for i in range (self .rows ):
109+ for j in range (self .columnsOther ):
110+ for k in range (self .rowsOther ):
111+ # Or can iterate over self.columns
112+ self .matrix5 [i ][j ] += self .matrix [i ][k ]* self .matrix2 [k ][j ]
113+ print ("==========" )
114+ print ("Multiplication is:" )
115+ for i in range (self .rows ):
116+ for j in range (self .columnsOther ):
117+ print (self .matrix5 [i ][j ], end = " " )
118+ print ()
119+ """
120+ for row in self.matrix5:
121+ for value in row:
122+ print(value, end=" ")
123+ print()
124+ """
125+ print ("==========" )
126+
127+ def main ():
128+ while True :
129+ ops = Matrices ()
130+ print ("Make your choice!\n 1. Addition\n 2. Subtraction\n 3. Tranpose\n 4. Multiplication\n 5. Exit" )
131+ try :
132+ choice = int (input ("Enter your choice: " ))
133+ except ValueError :
134+ print ("Invalid input! Please enter a number" )
135+ continue
136+
137+ if choice == 1 :
138+ print ("Enter first matrix" )
139+ ops .inputM ()
140+ ops .displayM ()
141+ print ("Now enter second matrix" )
142+ ops .inputM2 ()
143+ ops .displayM2 ()
144+ ops .addition ()
145+ elif choice == 2 :
146+ print ("Enter first matrix" )
147+ ops .inputM ()
148+ ops .displayM ()
149+ print ("Now enter second matrix" )
150+ ops .inputM2 ()
151+ ops .displayM2 ()
152+ ops .subtraction ()
153+ elif choice == 3 :
154+ ops .inputM ()
155+ ops .displayM ()
156+ ops .transpose ()
157+ elif choice == 4 :
158+ ops .inputM ()
159+ ops .displayM ()
160+ ops .inputM2 ()
161+ ops .displayM2 ()
162+ ops .multiplication ()
163+ elif choice == 5 :
164+ print ("The program ended!" )
165+ break
166+ else :
167+ print ("Invalid choice! Select a valid option." )
168+
169+ main ()
170+
0 commit comments