#------------------------------------------------------------------------------ # matrix.py #------------------------------------------------------------------------------ from random import uniform class Matrix: """Class representing a rectangular matrix.""" # built-in methods --------------------------------------------------------- def __init__(self, L=[]): """ Initialize a Matrix object from a list of lists. If the list is empty, the resulting Matrix is empty (size 0x0). """ # get number of rows and columns n = self.numRows = len(L) m = self.numCols = ( len(L[0]) if n>0 else 0 ) self.elements = {} if n==0: return # end if # build dictionary for i in range(n): if len(L[i])!=m: msg = f'could not create Matrix from ragged list:\n{L}' raise ValueError(msg) # end if for j in range(m): self.elements[(i+1,j+1)] = L[i][j] # end for # end for # end __init__() def __str__(self): def __eq__(self, other): # Matrix instance methods -------------------------------------------------- def add(self, other): def sub(self, other): def scale(self, c): def trans(self): def mult(self, other): # Matrix class methods ----------------------------------------------------- def from_string(s=''): def identity(n): def randMatrix(n, m, a, b): # end class Matrix ------------------------------------------------------------