Question: PYTHON PROGRAMMING This class is intended to create a way to store the values and shape of a matrix. The matrix can be imported


PYTHON PROGRAMMING
"""
This class is intended to create a way to store the values and shape of a matrix. The matrix can be imported as a list (matrix with single column, or vector), or as a list of lists (2-dimensional matrices). The class includes an __init__ function to read in the matrix, or create a matrix of 1's of a specific dimensionality. The class also includes a __repr__ function to designate the method for printing the matrix to screen.
"""
class Matrix(object): """ Read the matrix and store as part of the class object """ def __init__(self, value = {}, dim=(1,1)): if isinstance(value, list): if len(value)>0: if type(value[0]) is int or type(value[0]) is float: row = (int, float) else: row = type(value[0]) for i in value: if type(i) is not int, type(i) is not float, type(i) is not list: raise RuntimeError("Matrix is invalid. Please ensure that all elements share a type.") if row is list: lenInner = len(value[0]) for i in value: if len(i) is not lenInner: raise RuntimeError("Matrix is invalid. Please ensure that all rows have uniform length.") for j in i: if type(j) is not int and type(j) is not float: raise RuntimeError("Matrix is invalid. Please ensure that all elements are numeric (either float or int).") self.value = value try: self.shape = (len(value), len(value[0])) except: self.shape = (len(value), 1) if: matrix = [] for i in range(dim[0]): row = [] for j in range(dim[1]): row.append(1) matrix.append(row) value = matrix shape = dim """ Print the matrix to screen """ def __repr__(self) string = " " for i in range(shape[0]): if shape[1]>1: if i 1: string += "[ " string += str(value[i]) + " " else: string += str(value[i]) + " " if shape[1]>1: string += "] " return string
12. class Matrix(object): 13 Read the matrix and store as part of the class object 14 15 16 17- def __init__(self, value = {}, dim=(1,1)): if isinstance(value, list): if len(value)>0: if type (value[0]) is int or type(value[0]) is float: row = (int, float) else: row = type(value[0]) for i in value: if type(i) is not int, type(i) is not float, type(i) is not list: raise RuntimeError("Matrix is invalid. Please ensure that all elements share a type.") if row is list: lenInner = len(value[0]) for i in value: if len(i) is not lenInner: raise RuntimeError("Matrix is invalid. Please ensure that all rows have uniform length.") for j ini: if type(j) is not int and type(j) is not float: raise RuntimeError("Matrix is invalid. Please ensure that all elements are numeric (either float or int).") self.value = value 35 36 37 - 38 40 try: | self.shape = (len(value), len(value[0])) except: | self. shape = (len(value), 1) if: matrix = [] for i in range(dim[0]): row = [] for j in range(dim[1]): row.append(1) matrix.append(row) 41 42 43 44 45 46 47 value = matrix shape = dim 48 51 Print the matrix to screen A 55 - 56 - 57 - 58 59 def __repr__(self) string = for i in range(shape[0]): if shape[1]>1: if i 1: string += "[" string += str(value[i]) + " " else: string += str(value[i]) + "" if shape[1]>1: string += "] " return string em Create a 3x8 matrix by providing a list of lists 10 pts e Create a 10x10 matrix using the 'dim' argument 10 pts Measure the dimensions of a 3x8 matrix 10 pts Measure the dimensions of a 10x10 matrix 10 pts & Test invalid matrix entry 1 5 pts & Test invalid matrix entry 2 5 pts 12. class Matrix(object): 13 Read the matrix and store as part of the class object 14 15 16 17- def __init__(self, value = {}, dim=(1,1)): if isinstance(value, list): if len(value)>0: if type (value[0]) is int or type(value[0]) is float: row = (int, float) else: row = type(value[0]) for i in value: if type(i) is not int, type(i) is not float, type(i) is not list: raise RuntimeError("Matrix is invalid. Please ensure that all elements share a type.") if row is list: lenInner = len(value[0]) for i in value: if len(i) is not lenInner: raise RuntimeError("Matrix is invalid. Please ensure that all rows have uniform length.") for j ini: if type(j) is not int and type(j) is not float: raise RuntimeError("Matrix is invalid. Please ensure that all elements are numeric (either float or int).") self.value = value 35 36 37 - 38 40 try: | self.shape = (len(value), len(value[0])) except: | self. shape = (len(value), 1) if: matrix = [] for i in range(dim[0]): row = [] for j in range(dim[1]): row.append(1) matrix.append(row) 41 42 43 44 45 46 47 value = matrix shape = dim 48 51 Print the matrix to screen A 55 - 56 - 57 - 58 59 def __repr__(self) string = for i in range(shape[0]): if shape[1]>1: if i 1: string += "[" string += str(value[i]) + " " else: string += str(value[i]) + "" if shape[1]>1: string += "] " return string em Create a 3x8 matrix by providing a list of lists 10 pts e Create a 10x10 matrix using the 'dim' argument 10 pts Measure the dimensions of a 3x8 matrix 10 pts Measure the dimensions of a 10x10 matrix 10 pts & Test invalid matrix entry 1 5 pts & Test invalid matrix entry 2 5 pts
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
