This is the same problem Statement as mentioned in Operations on Matrix using Python | Addition | Subtraction | Multiplication | Transpose but, in this solution, we used oop concepts like class and member function, etc.
Problem:- Write a Python program to compute the following computation on the matrix:
a) Addition of two matrices
b) Subtraction of two matrices
c) Multiplication of two matrices
d) transpose of a matrix
Note: Scroll Horizontally to see the full line of code.
class Matrix:
def __init__(self):
self.matrix = None
self.r = None
self.c = None
def addelem(self):
self.r = int(input("Enter the no. of rows: "))
self.c = int(input("Enter the no. of columns: "))
self.matrix = []
for i in range(self.r):
row = []
for j in range(self.c):
print("Enter the (", i, ",", j, ") th element: ", end="")
elem = int(input())
row.append(elem)
self.matrix.append(row)
def printMatrix(self):
self.r = len(self.matrix)
for i in range(self.r):
print("[ ", end="")
for j in range(len(self.matrix[i])):
print(self.matrix[i][j], end=" ")
print("]")
def addition(self, B):
C = Matrix()
if (self.r == B.r) and (self.c == B.c):
C.matrix = []
for i in range(self.r):
row = []
for j in range(self.c):
elem = self.matrix[i][j] + B.matrix[i][j]
row.append(elem)
C.matrix.append(row)
return C
else:
C.matrix = None
print("\nAddition is not Possible.")
def subtraction(self, B):
C = Matrix()
if (self.r == B.r) and (self.c == B.c):
C.matrix = []
for i in range(self.r):
row = []
for j in range(self.c):
elem = self.matrix[i][j] - B.matrix[i][j]
row.append(elem)
C.matrix.append(row)
return C
else:
C.matrix = None
print("\nSubtraction is not Possible.")
def transpose(self):
C = Matrix()
C.matrix = []
for j in range(self.c):
row = []
for i in range(self.r):
elem = self.matrix[i][j]
row.append(elem)
C.matrix.append(row)
return C
def multiplication(self, B):
C = Matrix()
if self.c == B.r:
C.matrix = []
for i in range(self.r):
row = []
for j in range(B.c):
elem = 0
for k in range(B.r):
elem += self.matrix[i][k] * B.matrix[k][j]
row.append(elem)
C.matrix.append(row)
return C
else:
C.matrix = None
print("\nMultiplication is not possible!")
cont = 'y'
while cont == 'y':
# Matrix A
A = Matrix()
print("Matrix A: ")
A.addelem()
print("Matrix A is: ")
A.printMatrix()
# Matrix B
B = Matrix()
print("Matrix B: ")
B.addelem()
print("Matrix B is: ")
B.printMatrix()
cont1 = 'y'
while cont1 == 'y':
print("<-----Menu----->")
print("1.Addition\n2.Subtraction\n3.Multiplication\n4.Transpose")
choice = int(input("Enter the Choice"))
if (choice == 1):
C = A.addition(B)
if C is not None:
print("\nMatrix A is: ")
A.printMatrix()
print("\nMatrix B is: ")
B.printMatrix()
print("\nAddition is: ")
C.printMatrix()
elif (choice == 2):
C = A.subtraction(B)
if C is not None:
print("\nMatrix A is: ")
A.printMatrix()
print("\nMatrix B is: ")
B.printMatrix()
print("\nSubtraction is: ")
C.printMatrix()
elif (choice == 3):
C = A.multiplication(B)
if C is not None:
print("\nMatrix A is: ")
A.printMatrix()
print("\nMatrix B is: ")
B.printMatrix()
print("\nMultiplication is: ")
C.printMatrix()
elif (choice == 4):
C = A.transpose()
print("\nMatrix A is: ")
A.printMatrix()
print("\nTranspose of Matrix A is: ")
C.printMatrix()
C = B.transpose()
print("\nMatrix B is: ")
B.printMatrix()
print("\nTranspose of Matrix B is: ")
C.printMatrix()
else:
print("Invalid Choice!!!\n")
cont1 = input("Do you want to do any other operation? (y/n): ")
cont = input("Do you want to continue? (y/n): ")
print("\nProgram Ended Successfully!!!")
Comments
Post a Comment