Question

Write a Python class Matrix which defines a two-by-two matrix with float entries as a Python...

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)

Homework Answers

Answer #1

# 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:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Monitor three consecutive phone calls going through a telephone switching office. Classify each one as a...
Monitor three consecutive phone calls going through a telephone switching office. Classify each one as a voice call (v) if someone is speaking, or a data call (d) if the call is carrying a modem or fax signal. Your observation is a sequence of three letters (each letter is eitherv or d). For example, two voice calls followed by one data call corresponds to vvd. Write the elements of the following sets: (1) A1 ={first call is a voice call}...
A student will randomly select 5 cards from a deck of 52 cards. Each card is...
A student will randomly select 5 cards from a deck of 52 cards. Each card is uniquely identified by a label which is a combination of a letter (one of the following: A, B, C, D) followed by a number (one of the following: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13). The labels on the cards are A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, B1, B2, B3, B4,...
A student will randomly select 5 cards from a deck of 52 cards. Each card is...
A student will randomly select 5 cards from a deck of 52 cards. Each card is uniquely identified by a label which is a combination of a letter (one of the following: A, B, C, D) followed by a number (one of the following: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13). The labels on the cards are A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, B1, B2, B3, B4,...
You are coding a simple game called Pig. Players take turns rolling a die. The die...
You are coding a simple game called Pig. Players take turns rolling a die. The die determines how many points they get. You may get points each turn your roll (turn points), you also have points for the entire game (grand points). The first player with 100 grand points is the winner. The rules are as follows: Each turn, the active player faces a decision (roll or hold): Roll the die. If it’s is a: 1: You lose your turn,...