Write a Python class Matrix which defines a two-by-two matrix with float entries as a Python object. The class Matrix has to be able to display the object on the screen when the print function is called, and it should include methods determinant(), trace(), inverse(), characteristic_polynomial(), and matrix_product(). Furthermore, a user should be able to multiply the matrix by a constant and be able to add and subtract two matrices using the usual symbols + and -. Use the following as a guideline for your code: Creating an instance M of the matrix object must be done like so: M = Matrix([[a1,a2],[a3,a4]]), where a1, a2, a3, a4 are floats. Displaying an instance of this object using the print function, i.e. print(M), must produce [a1, a2] [a3, a4] on the screen. Regarding the methods: Method call: M.determinant() The determinant of a two-by-two matrix is a float and it is defined as a1*a4-a2*a3 for the instance M, defined as above. Method call: M.trace() The trace of a two-by-two matrix is a float and it is defined as a1+a4 for the instance M, defined as above. Method call: M.inverse() The inverse of a two-by-two matrix is another two-by-two matrix. For the instance M, defined as above, the inverse would result in a matrix [a4/D, -a2/D] [-a3/D, a1/D] where D is the determinant of M and D is not equal to zero! You must call the method determinant() inside the definition of the inverse() method to perform this check. If the determinant is not zero, return the inverse matrix. Otherwise, print a message "matrix is singular!" Method call: M.characteristic_polynomial() The characteristic polynomial of a two-by-two matrix is a polynomial in an unknown, say x, and it is defined as x^2-T*x+D for the instance M, defined as above. T stands for the trace and D for the determinant of M. You must call the methods determinant() and trace() inside the definition of the method characteristic_polynomial(). The method characteristic_polynomial() must return a string of the form x^2-T*x+D, where T and D have been evaluated to their floating point values. Method call: M1.matrix_product(M2) For two instances M1 and M2 of the object matrix, defined as M1 = Matrix([[a1,a2],[a3,a4]]) and M2 = Matrix([[b1,b2],[b3,b4]]) the method matrix_product() must return a new matrix of the form [a1*b1+a2*b3, a1*b2+a2*b4] [a3*b1+a4*b3, a3*b2+a4*b4] where a1,a2,a3,a4, and b1,b2,b3,b4 are float entries of the instances M1 and M2. Regrading the multiplication by a constant: For a matrix defined as M = Matrix([[a1,a2],[a3,a4]]), the print(7*M) must produce a new matrix [7*a1, 7*a2] [7*a3, 7*a4] where a1,a2,a3,a4 are some floting point numbers. Regarding addition/subtraction of two matrices M1 and M2, defined as M1 = Matrix([[a1,a2],[a3,a4]]) and M2 = Matrix([[b1,b2],[b3,b4]]) print(M1+M2) should produce a new matrix C of the form: [a1+b1, a2+b2] [a3+b3, a4+b4] and print(M1-M2) should produce a new matrix C of the form: [a1-b1, a2-b2] [a3-b3, a4-b4]
(python, please help)
# do comment if any problem arises
# Code
class Matrix:
def __init__(self, matrix):
self.matrix = matrix
def __str__(self):
return f"[{self.matrix[0][0]}, {self.matrix[0][1]}] [{self.matrix[1][0]}, {self.matrix[1][1]}]"
def determinant(self):
return self.matrix[0][0]*self.matrix[1][1]-self.matrix[0][1]*self.matrix[1][0]
def trace(self):
return self.matrix[0][0]+self.matrix[1][1]
def inverse(self):
D = self.determinant()
if D == 0:
print('matrix is singular!')
return
a1 = self.matrix[1][1]/D
a2 = -1*self.matrix[0][1]/D
a3 = -1*self.matrix[1][0]/D
a4 = self.matrix[0][0]/D
return Matrix([[a1, a2], [a3, a4]])
def characteristic_polynomial(self):
T = self.trace()
D = self.determinant()
return f'x^2-{T}x+{D}'
def matrix_product(self, M2):
# matrix a
a1 = self.matrix[0][0]
a2 = self.matrix[0][1]
a3 = self.matrix[1][0]
a4 = self.matrix[1][1]
# matrix b
b1 = M2.matrix[0][0]
b2 = M2.matrix[0][1]
b3 = M2.matrix[1][0]
b4 = M2.matrix[1][1]
# [a1*b1+a2*b3, a1*b2+a2*b4] [a3*b1+a4*b3, a3*b2+a4*b4]
return Matrix([[a1*b1+a2*b3, a1*b2+a2*b4], [a3*b1+a4*b3, a3*b2+a4*b4]])
def __rmul__(self, other):
a1 = self.matrix[0][0]
a2 = self.matrix[0][1]
a3 = self.matrix[1][0]
a4 = self.matrix[1][1]
return Matrix([[7*a1, 7*a2], [7*a3, 7*a4]])
def __add__(self, M2):
# matrix a
a1 = self.matrix[0][0]
a2 = self.matrix[0][1]
a3 = self.matrix[1][0]
a4 = self.matrix[1][1]
# matrix b
b1 = M2.matrix[0][0]
b2 = M2.matrix[0][1]
b3 = M2.matrix[1][0]
b4 = M2.matrix[1][1]
return Matrix([[a1+b1, a2+b2], [a3+b3, a4+b4]])
def __sub__(self, M2):
# matrix a
a1 = self.matrix[0][0]
a2 = self.matrix[0][1]
a3 = self.matrix[1][0]
a4 = self.matrix[1][1]
# matrix b
b1 = M2.matrix[0][0]
b2 = M2.matrix[0][1]
b3 = M2.matrix[1][0]
b4 = M2.matrix[1][1]
return Matrix([[a1-b1, a2-b2], [a3-b3, a4-b4]])
# testing above class
first = Matrix([[4, 7], [2, 6]])
# display output of all functions
print(f'a1: {first}')
print(f'determinant: {first.determinant()}')
print(f'trace: {first.trace()}')
print(f'inverse: {first.inverse()}')
second = Matrix([[1, 2], [3, 4]])
print(f'characteristic_polynomial: {first.characteristic_polynomial()}')
print(f'matrix_product: {first.matrix_product(second)}')
print(f'7*a1= {7*first}')
print(f'a2: {second}')
print(f'a1+a2= {first+second}')
print(f'a1-a2= {first-second}')
Screenshot of code:
Output:
Get Answers For Free
Most questions answered within 1 hours.