Question: The following class named Matrix has a template given to you. The init ( ) method takes 2 parameters. The parameter name is a string,

The following class named Matrix has a template given to you. The init() method takes 2 parameters. The parameter name is a string, and the parameter data is a nested list representing a Matrix. (Each element in data is a list of elements for that row).
For example [[1,2],[3,4]] represents
12
34
Modify the class according to the following specifications
Write a method named Average. Matrix.Average() should return the average of all values in the matrix as a float. Assume the average of an empty matrix is 0.
Overload the subtraction operator so that you can subtract two matrices from each other. Subtracting 2 Matrices should result in a new matrix. If the matrices are the same side then the result is a new matrix. The new matrix will have the 'Matrix1.name - Matrix2.name'. The data for the matrix should be element by element subtraction of the second matrix from the first. If the matrices are not the same size, then the method should return the string 'error'.
Overload the addition operator so that if the matrix is added to a value (integer or float), all values in the matrix increase by that value
Examples
Example 1 Code:
a = Matrix('a',[[9,8],[7,6]])
print(a) # this should work with the template
Example 1 Output:
a=
98
76
Example 2 Code:
a = Matrix('a',[[9,8],[7,6]])
avg = a.Average()
print(avg) # this should work with the template
Example 2 Output:
7.5
Example 3 Code:
a = Matrix('a',[[9,8],[7,6]])
b = Matrix('b',[[1,2],[3,4]])
print('printing a-b')
print(a-b)
Example 3 Output:
printing a-b
a-b=
86
42
Example 4 Code:
a = Matrix('a',[[1,2],[3,4]])
a+1
print(a)
Example 4 Output:
a=
23
45
Example 3 Code:
a = Matrix('a',[[9,8],[7,6]])
b = Matrix('b',[[1],[3]])
print('printing a-b')
print(a-b)
Example 3 Output:
printing a-b
error

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!