Mathematical Computing: Problem 3. The description of this problem is probably l
ID: 3841581 • Letter: M
Question
Mathematical Computing:
Problem 3. The description of this problem is probably longer than the source code. For simplicity, we won't include error checking, so illegal input will crash the program. As usual, turn in source code and tests to show your code works handle pairs of numbers as tuples). Store the matrix in a list or list of lists, whichever you prefer. Keep track of the shape m x n. Include the following basic methods Matrix( Gm,n)) Provide a constructor that sets the shape of the matrix and sets all the entries to a default value of zero newMatrix The call newMatrix ((min)) clears the existing data in the Matrix and sets it to a matrix of zeros of shape Cm,n) getShape getShape C) returns a tuple giving the shape of the matrix. get Element The call getElement (Ci,j)) returns the element at row i j and column set Element The call setElement C (i,j), value) sets the entry at posi- tion Ci, j) to value The rest of the methods should use those above in order to be independent of the internal implementation of the matrix.Explanation / Answer
# Question 1. Which was a bit ambigous but I somehow did. Question 2 is not clear completely.
# here is link of code https://goo.gl/K8eu5i in case indentation mess up.
class Matrix:
def __init__(self, shape):
self.empty_matrix(shape)
def getShape(self):
return self.shape
def newMatrix(self,(m, n)):
self.empty_matrix((m, n))
def empty_matrix(self, shape):
self.shape = shape
(m, n) = self.shape;
mat = []
for i in range(0,m):
row = []
for j in range(0, n):
row.append(0)
mat.append(row)
self.mat = mat
def getElement(self, (i, j)):
return self.mat[i][j]
def setElement(self, (i, j), value):
self.mat[i][j] = value
def setElements(self, value_dict):
for key in value_dict:
self.setElement(key, value_dict[key])
def setByRows(self, listOfList):
m = len(listOfList)
n = len(listOfList[0])
self.newMatrix((m, n))
self.mat = listOfList
def add(self, A, B):
if (A.getShape != B.Shape):
return
(m, n) = A.getShape()
C.newMatrx((m,n))
for i in range(0, m):
for j in range(0, n):
C.setElement((i,j), A.getElement((i,j)) + B.getElement((i,j)))
def scalarMult(self, num, A):
(m, n) = A.getShape()
C.newMatrx((m,n))
for i in range(0, m):
for j in range(0, n):
C.setElement((i,j), num*A.getElement((i,j)))
def duplicate(self, A):
self.scalarMult(1, A)
def mult(self, A, B):
(m, n) = A.getShape()
(m1, n1) = B.getShape()
if n != m1:
return
C.newMatrx((m,n1))
for i in range(0, m):
for j in range(0, n1):
val = 0
for k in range(0, m1):
val += A.getElement((i,k)) * B.getElement((k,j))
C.setElement((i,j),val)