Please write in Python Write these functions in the code below Link class: __str
ID: 3814435 • Letter: P
Question
Please write in Python
Write these functions in the code below
Link class: __str__()
Matrix class:
setElement()
insertLink()
deleteLink()
__add__()
__mul__()
getRow()
getCol()
__str__()
You may always write other auxiliary or helper functions if you need them. The str() function will return a string representation of the matrix that is similar to the Dense Matrix representation, i.e. in the string representation both the zero and non-zero elements will be displayed.
Dense Matrix Rep: Not Output
matrix.txt:
2 3
1 2 0
3 0 4
2 3
0 1 5
6 2 0
2 3
1 0 5
0 4 1
3 2
4 3
2 0
1 0
class Link (object):
def __init__ (self, col = 0, data = 0, next = None):
self.col = col
self.data = data
self.next = next
# returns a String representation of a Link (col, data)
def __str__ (self):
s = ''
return s
class LinkedList (object):
def __init__ (self):
self.first = None
def insertLast (self, col, data):
newLink = Link (col, data)
current = self.first
if (current == None):
self.first = newLink
return
while (current.next != None):
current = current.next
current.next = newLink
# returns a String representation of a LinkedList
def __str__ (self):
s = ''
return s
class Matrix (object):
def __init__ (self, row = 0, col = 0):
self.row = row
self.col = col
self.matrix = []
# Performs assignment operation: matrix[row][col] = data
def setElement (self, row, col, data):
return
# Adds two sparse matrices
def __add__ (self, other):
return
# Multiplies two sparse matrices
def __mul__ (self, other):
return
# Returns a linked list representing a row
def getRow (self, n):
return
# Returns a linked list representing a column
def getCol (self, n):
return
# Returns a string representation of a matrix
def __str__ (self):
s = ''
return s
def readMatrix (inFile):
line = inFile.readline().rstrip(" ").split()
row = int (line[0])
col = int (line[1])
mat = Matrix (row, col)
for i in range (row):
line = inFile.readline().rstrip(" ").split()
newRow = LinkedList()
for j in range (col):
elt = int(line[j])
if (elt != 0):
newRow.insertLast (j, elt)
mat.matrix.append (newRow)
line = inFile.readline()
return mat
def main ():
inFile = open ("matrix.txt", "r")
print ("Test Matrix Addition")
matA = readMatrix (inFile)
print (matA)
matB = readMatrix (inFile)
print (matB)
matC = matA + matB
print (matC)
print (" Test Matrix Multiplication")
matP = readMatrix (inFile)
print (matP)
matQ = readMatrix (inFile)
print (matQ)
matR = matP * matQ
print (matR)
print (" Test Setting a Zero Element to a Non-Zero Value")
matA.setElement (1, 1, 5)
print (matA)
print (" Test Getting a Row")
row = matP.getRow(1)
print (row)
print (" Test Getting a Column")
col = matQ.getCol(0)
print (col)
inFile.close()
main()
Test Matrix Addition Test Matrix Multiplication 11 1 15 5 41 11 41 13 21 2 11 10 826 493 332 158 d4 5 54 M11 r2 12 32 r1 t14 98 121 45 t13 6 19 62 e15 421Explanation / Answer
def makeDense(mat):
dense_matrix = []
for row in mat.matrix :
tmp = [0]*mat.col
current = row.first
while current:
tmp[current.col] = current.data
current = current.next
dense_matrix.append(tmp)
return dense_matrix
def LinkedList_from_row(row):
res = LinkedList()
for i in range(len(row)):
if row[i] != 0 :
res.insertLast(i,row[i])
return res
def makeSparse(mat):
res = Matrix(len(mat),len(mat[0]))
for row in mat:
newRow = LinkedList()
for j in range(len(row)):
if row[j] != 0 :
newRow.insertLast(j,row[j])
res.matrix.append(newRow)
return res
class Link (object):
def __init__ (self, col = 0, data = 0, next = None):
self.col = col
self.data = data
self.next = next
# returns a String representation of a Link (col, data)
def __str__ (self):
s = str((col,data))
return s
class LinkedList (object):
def __init__ (self):
self.first = None
def insertLast (self, col, data):
newLink = Link (col, data)
current = self.first
if (current == None):
self.first = newLink
return
while (current.next != None):
current = current.next
current.next = newLink
# returns a String representation of a LinkedList
def __str__ (self):
s = ''
current = self.first
prev_ind = -1
while current :
cur_ind = current.col
s += ' 0'*(cur_ind-prev_ind-1)
prev_ind = cur_ind
s += ' '+str(current.data)
current = current.next
return s
class Matrix (object):
def __init__ (self, row = 0, col = 0):
self.row = row
self.col = col
self.matrix = []
# Performs assignment operation: matrix[row][col] = data
def setElement (self, row, col, data):
mat = makeDense(self)
mat[row][col] = data
return makeSparse(mat)
# Adds two sparse matrices
def __add__ (self, other):
A = makeDense(self)
B = makeDense(other)
C = []
for i in range(len(A)):
tmp = []
for j in range(len(A[0])):
tmp.append(A[i][j]+B[i][j])
C.append(tmp)
return makeSparse(C)
# Multiplies two sparse matrices
def __mul__ (self, other):
A = makeDense(self)
B = makeDense(other)
# C = [[0]*len(B[0])]*len(A)
C = [ [0]*len(B[0]) for _ in xrange(len(A)) ]
for i in range(len(A)):
for j in range(len(B[0])):
for k in range(len(B)):
C[i][j] += (A[i][k] * B[k][j])
return makeSparse(C)
# Returns a linked list representing a row
def getRow (self, n):
return self.matrix[n]
# Returns a linked list representing a column
def getCol (self, n):
mat = makeDense(self)
res = []
for row in mat:
res.append(row[n])
return LinkedList_from_row(res)
# Returns a string representation of a matrix
def __str__ (self):
s = ''
dense_matrix = makeDense(self)
for row in dense_matrix:
row_s = [str(x) for x in row]
s += ' '.join(row_s)
s += ' '
return s
def readMatrix (inFile):
line = inFile.readline().rstrip(" ").split()
row = int (line[0])
col = int (line[1])
mat = Matrix (row, col)
for i in range (row):
line = inFile.readline().rstrip(" ").split()
newRow = LinkedList()
for j in range (col):
elt = int(line[j])
if (elt != 0):
newRow.insertLast (j, elt)
mat.matrix.append (newRow)
return mat
def main ():
inFile = open ("matrix.txt", "r")
print ("Test Matrix Addition")
matA = readMatrix (inFile)
print (matA)
matB = readMatrix (inFile)
print (matB)
matC = matA + matB
print (matC)
print (" Test Matrix Multiplication")
matP = readMatrix (inFile)
print (matP)
matQ = readMatrix (inFile)
print (matQ)
matR = matP * matQ
print (matR)
print (" Test Setting a Zero Element to a Non-Zero Value")
matA.setElement (1, 1, 5)
print (matA)
print (" Test Getting a Row")
row = matP.getRow(1)
print (row)
print (" Test Getting a Column")
col = matQ.getCol(0)
print (col)
inFile.close()
main()