Hi, I am implementing a matrox with several functions. I have pasted the error, the assignment and my code. Hopefully you can see what causes the error and how to fix it. Thank you so much.  My transpose function gives an error: IndexError: list assignment index out of range self =   def test_transpose(self): self.assertEqual(str(self.a.transpose()), ' 1 4 7\n 2 5 8\n 3 6 9\n') self.assertEqual(str(self.b.transpose()), ' 2 8 14\n 4 10 16\n 6 12 18\n') self.assertEqual(str(self.c.transpose()), ' 1 0 0\n 0 1 0\n 0 0 1\n') self.assertEqual(str(self.a.transpose().transpose()), ' 1 2 3\n 4 5 6\n 7 8 9\n') self.assertEqual(str(self.e.transpose()), ' 0 0 0 1\n 0 0 1 0\n 0 1 0 0\n 1 0 0 0\n') > self.assertEqual(str(self.f.transpose()), ' 1 2 3 1\n 2 3 2 3\n 3 1 1 2\n')   tests_matrix.py:34: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _   self = 1 2 3 2 3 1 3 2 1 1 3 2     def transpose(self): nl = [[None for j in range(len(self.list1[i]))] for i in range(len(self.list1))] for i in range(len(self.list1)): # or remove -1 for j in range(len(self.list1[i])): # or remove -1 #nl[j].append(self.list1[i][j]) > nl[j][i] = self.list1[i][j] E IndexError: list assignment index out of range Below I have pasted the assignment and my code. here is the assignment: Implement a matrix class (in matrix.py).  a) The initializer should take a list of lists as an argument, where each outer list is a row, and each value in an inner list is a value in the corresponding row.  b) Implement the __str__ method to nicely format the string representation of the matrix: one line per row, two characters per number (%2d) and a space between numbers. For example: m = Matrix([[1,0,0],[0,1,0],[0,0,1]]) print(m) > 1  0  0 > 0  1  0 > 0  0  1 c) Implement a method scale(factor) that returns a new matrix where each value is multiplied by scale. For example: m = Matrix([[1,2,3],[4,5,6],[7,8,9]]) n = m.scale(2) print(n)> 2  4  6 > 8 10 12 >14 16 18 print(m) > 1  2  3 > 4 5 6 > 7 8 9 d) Implement a method transpose() that returns a new matrix that has been transposed. Transposing flips a matrix over its diagonal: it switches rows and columns. m = Matrix([[1,2,3],[4,5,6],[7,8,9]]) print(m) > 1  2  3 > 4 5 6 > 7 8 9 print(m.transpose()) > 1 4 7 > 2 5 8 > 3 6 9   My code:  class Matrix: def __init__(self, rows): self.list1 = rows def __str__(self): res = "" for i in range(len(self.list1)): for j in range(len(self.list1[i])): res = res + "{:2d}".format(self.list1[i][j]) if j < len(self.list1[i]) - 1: res += " " res = res + "\n" return res def scale(self, w): nl = [] for i in range(len(self.list1)): nl.append([]) for j in range(len(self.list1[i])): nl[i].append(self.list1[i][j] * w) return Matrix(nl) def transpose(self): nl = [[None for j in range(len(self.list1[i]))] for i in range(len(self.list1))] for i in range(len(self.list1)): # or remove -1 for j in range(len(self.list1[i])): # or remove -1 #nl[j].append(self.list1[i][j]) nl[j][i] = self.list1[i][j] return Matrix(nl) #return nl def multiply(self, b): nl = [[0 for j in range(len(b.list1[i]))] for i in range(len(self.list1))] if len(self.list1[0]) == len(b.list1): for i in range(len(self.list1)): for j in range(len(b.list1[0])): for k in range(len(b.list1)): nl[i][j] += self.list1[i][k] * b.list1[k][j] return Matrix(nl) else: return None # repr is similar to __str__, but __str__ is used when # printing a value, __repr__ when showing a value in # the interpreter. # repr is intended to be an unambiguous, complete representation # of the object, while str is intended to be nicely readable # for this object they are the same def __repr__(self): return str(self) if __name__ == "__main__": m = Matrix([[1, 0, 0], [0, 1, 0], [0, 0, 1]]) m = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) n = m.scale(2) print(n) print(m.transpose()) e = Matrix([[1, 2, 3], [2, 1, 3], [3, 2, 1]]) print(e.multiply(e)) print(e.__repr__())

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Hi, I am implementing a matrox with several functions. I have pasted the error, the assignment and my code. Hopefully you can see what causes the error and how to fix it. Thank you so much. 


My transpose function gives an error:

IndexError: list assignment index out of range

  1. self = <tests_matrix.TestsStudent testMethod=test_transpose>
  2.  
  3. def test_transpose(self):
  4. self.assertEqual(str(self.a.transpose()), ' 1 4 7\n 2 5 8\n 3 6 9\n')
  5. self.assertEqual(str(self.b.transpose()), ' 2 8 14\n 4 10 16\n 6 12 18\n')
  6. self.assertEqual(str(self.c.transpose()), ' 1 0 0\n 0 1 0\n 0 0 1\n')
  7. self.assertEqual(str(self.a.transpose().transpose()), ' 1 2 3\n 4 5 6\n 7 8 9\n')
  8. self.assertEqual(str(self.e.transpose()), ' 0 0 0 1\n 0 0 1 0\n 0 1 0 0\n 1 0 0 0\n')
  9. > self.assertEqual(str(self.f.transpose()), ' 1 2 3 1\n 2 3 2 3\n 3 1 1 2\n')
  10.  
  11. tests_matrix.py:34:
  12. _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
  13.  
  14. self = 1 2 3
  15. 2 3 1
  16. 3 2 1
  17. 1 3 2
  18.  
  19.  
  20. def transpose(self):
  21. nl = [[None for j in range(len(self.list1[i]))] for i in range(len(self.list1))]
  22. for i in range(len(self.list1)): # or remove -1
  23. for j in range(len(self.list1[i])): # or remove -1
  24. #nl[j].append(self.list1[i][j])
  25. > nl[j][i] = self.list1[i][j]
  26. E IndexError: list assignment index out of range

Below I have pasted the assignment and my code. here is the assignment:

Implement a matrix class (in matrix.py). 

a) The initializer should take a list of lists as an argument, where each outer list is a row, and each value in an inner list is a value in the corresponding row. 

b) Implement the __str__ method to nicely format the string representation of the matrix: one line per row, two characters per number (%2d) and a space between numbers. For example:

m = Matrix([[1,0,0],[0,1,0],[0,0,1]])

print(m)
> 1  0  0
> 0  1  0
> 0  0  1

c) Implement a method scale(factor) that returns a new matrix where each value is multiplied by scale. For example:

m = Matrix([[1,2,3],[4,5,6],[7,8,9]])
n = m.scale(2)
print(n)> 2  4  6
> 8 10 12
>14 16 18
print(m)
> 1  2  3
> 4 5 6
> 7 8 9

d) Implement a method transpose() that returns a new matrix that has been transposed. Transposing flips a matrix over its diagonal: it switches rows and columns.

m = Matrix([[1,2,3],[4,5,6],[7,8,9]])
print(m)
> 1  2  3
> 4 5 6
> 7 8 9
print(m.transpose())
> 1 4 7
> 2 5 8
> 3 6 9

 

My code: 
class Matrix:
def __init__(self, rows):
self.list1 = rows

def __str__(self):
res = ""
for i in range(len(self.list1)):
for j in range(len(self.list1[i])):
res = res + "{:2d}".format(self.list1[i][j])
if j < len(self.list1[i]) - 1:
res += " "
res = res + "\n"
return res

def scale(self, w):
nl = []
for i in range(len(self.list1)):
nl.append([])
for j in range(len(self.list1[i])):
nl[i].append(self.list1[i][j] * w)
return Matrix(nl)

def transpose(self):
nl = [[None for j in range(len(self.list1[i]))] for i in range(len(self.list1))]
for i in range(len(self.list1)): # or remove -1
for j in range(len(self.list1[i])): # or remove -1
#nl[j].append(self.list1[i][j])
nl[j][i] = self.list1[i][j]
return Matrix(nl)
#return nl

def multiply(self, b):
nl = [[0 for j in range(len(b.list1[i]))] for i in range(len(self.list1))]
if len(self.list1[0]) == len(b.list1):
for i in range(len(self.list1)):
for j in range(len(b.list1[0])):
for k in range(len(b.list1)):
nl[i][j] += self.list1[i][k] * b.list1[k][j]
return Matrix(nl)
else:
return None

# repr is similar to __str__, but __str__ is used when
# printing a value, __repr__ when showing a value in
# the interpreter.
# repr is intended to be an unambiguous, complete representation
# of the object, while str is intended to be nicely readable
# for this object they are the same
def __repr__(self):
return str(self)


if __name__ == "__main__":
m = Matrix([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
m = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
n = m.scale(2)
print(n)
print(m.transpose())
e = Matrix([[1, 2, 3], [2, 1, 3], [3, 2, 1]])
print(e.multiply(e))
print(e.__repr__())

Expert Solution
steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Follow-up Questions
Read through expert solutions to related follow-up questions below.
Follow-up Question

Hi there, 

You mention what should be changed in a comment in the function multiply right? but you dint cgange it in the code, right? (i see no difference). besides, my error was in the transpose function. The multiply function actually passed the tests. Do you see any errors in the transpose function? Thanks

 

Solution
Bartleby Expert
SEE SOLUTION
Knowledge Booster
Linked List Representation
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-engineering and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY