Edit code please, I am editing the set element function but I keep getting the s
ID: 3814842 • Letter: E
Question
Edit code please, I am editing the set element function but I keep getting the same original matrix. I need the 5 to be put into row 1 column 1:
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)
print(mat)
mat[row][col] = data
print(mat[row][col])
print(mat)
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])for _ in range(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)
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()
incorrect output:
right output:
1 2 0
3 5 4
Explanation / 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):
row_list = self.matrix[row]
current = row_list.first
while current :
if current.col == col :
current.data = data
return
current = current.next
self.matrix[row].insertLast(col,data)
# 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])for _ in range(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)
# 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()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.