Question: Hello, I have a Python lab question requiring assistance. Attached images are the question and example format: The following are the initial codes provided: import
Hello, I have a Python lab question requiring assistance. Attached images are the question and example format:


The following are the initial codes provided:
import random from copy import deepcopy
class Matrix:
def __init__(self, nrows, ncols): """Construct a (nrows X ncols) matrix""" #I wrote a bit but am highly unsure about my work. Matrix = [] low = 0 high = 10 nrows = A ncols = B [random.choices(range(low,high), k=ncols) for i in range(nrows)] print Matrix pass def add(self, m): """return a new Matrix object after summation""" # Check two matrix are in the same size # add self.matrix with m.matrix result = deepcopy(self) pass return result
def sub(self, m): """return a new Matrix object after substraction""" # Check two matrix are in the same size # substraction self.matrix from m.matrix result = deepcopy(self) pass return result
def mul(self, m): """return a new Matrix object after multiplication""" # Check if the nrows of m is the same as ncols of self # Multiply self.matrix and m.matrix result = Matrix(self.nrows, m.ncols) pass return result
def transpose(self): """return a new Matrix object after transpose""" # Tranpose pass return result def display(self): """Display the content in the matrix""" pass
a_rows = int(input("Enter A matrix's rows:")) a_cols = int(input("Enter A matrix's cols:")) print("Matrix A({}, {}):".format(a_rows, a_cols)) A = Matrix(a_rows, a_cols) A.display() b_rows = int(input("Enter B matrix's rows:")) b_cols = int(input("Enter B matrix's cols:")) print("Matrix B({}, {}):".format(b_rows, b_cols)) B = Matrix(b_rows, b_cols) B.display() print("="*10, 'A + B', "="*10) result = A.add(B) if result is not None: result.display() print("="*10, 'A - B', "="*10) result = A.sub(B) if result is not None: result.display()
print("="*10, 'A * B', "="*10) result = A.mul(B) if result is not None: result.display()
print("="*5, "the transpose of A*B", "="*5) result = result.transpose() if result is not None: result.display()
Please provide a solution without using Numpy. Thanks so very much.
1. (100%) download the template code "matrix.ipynb" and complete the class Matrix. The class has 6 basic function. (1) initialize the matrix(construct a matrix nrows * m columns), the element in the matrix is random int from 010. (2) add matrix and return a new matrix object after summation (3) sub matrix and return a new matrix object after subtraction (4) multiply matrix and return a new matrix object after multiplication (5) return a new matrix object after transpose (6) display the content in the matrix Hint1: You can use the module copy Hint2: the return objects of (2) (5) should be Matrix or None Hint3: Maybe you should check if the matrixes can be calculated or not. Example: unacceptable adding/multiple Enter A matrix's rows: 3 Enter A matrix's cols:4 Matrix A(3,4) : 125190594825 Enter B matrix's rows: 2 Enter B matrix's cols:3 Matrix B(2,3) : 435777 == =="== ==0A+B========== Matrixs' size should be in the same size =========AB========== Matrixs' size should be in the same size =========AB========== Cannot multiply two matrixs with size (3,4) and (2, 3) ===x the transpose of AB====
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
