Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

8. Set goals that relate to practice as well as competition.

Answered: 1 week ago