Why Does My Python Matrix Multiplication Code Raise a TypeError?

  • Context: Python 
  • Thread starter Thread starter azerty12
  • Start date Start date
  • Tags Tags
    Error Python
Click For Summary

Discussion Overview

The discussion revolves around a TypeError encountered in a Python program implementing a recursive matrix multiplication algorithm. Participants explore the nature of the error and the structure of the matrices involved, focusing on the conditions under which the error arises.

Discussion Character

  • Technical explanation
  • Debugging

Main Points Raised

  • One participant describes encountering a TypeError when trying to assign a value to an element in a list representing a matrix.
  • Another participant suggests printing the matrices A, B, and C before the error occurs to diagnose the issue.
  • A participant questions whether A and B are nested lists, noting that accessing A[0][0] requires A to be structured as a nested list.
  • Another participant recommends checking the types of A and B to confirm they are not integers, which would cause the error.
  • A participant confirms that A and B are indeed nested lists, providing an example of their structure.
  • One participant points out that when the size of the matrices is 1, A and B may no longer be nested lists, leading to the subscripting error.
  • A later reply acknowledges that the issue was related to the size of the matrices, confirming that the problem was identified.

Areas of Agreement / Disagreement

Participants generally agree that the TypeError is related to the structure of the matrices, particularly when their size is 1. However, there is no consensus on the exact nature of the error until it is clarified that A and B can become non-nested lists.

Contextual Notes

The discussion highlights the importance of matrix dimensions and structure in programming, particularly in recursive algorithms. The issue arises specifically when the matrices reduce to a single element.

azerty12
Messages
15
Reaction score
0
Hi all!

I'm learning programming on Python.
Currently, I'm trying to implement a recursive algorithm using a divide and conquer method to compute a product of matrices.

Yet an error occurs when I try to assign an element in a list (representing a matrix)

I can't understand where the problem comes from, since I tried on the shell to enter a matrix exactly as in the program and assign its values (C[0][0]=...) and it works.
But when I try to execute the program, the following error is raised:line 42, in sqmat_mult_rec
C[0][0]=A[0][0]*B[0][0]
TypeError: 'int' object is not subscriptable
Any idea?

(code attached)
 

Attachments

Last edited:
Technology news on Phys.org
Why don't you try printing out A, B and C right before the error occurs? The 'not subscriptable' error usually means that you are trying to look at a subscript that doesn't exist.
 
I tried to and actually each of the objects I try to assign exist...
 
I'm sure they exist, but are they nested lists? When you write A[0][0], it means A must be of the form [[something]]. If A is a simple variable (which is what the message implies) or a simple list [], then you will get an error when you try to access A[0][0]. What does the print out of A and B look like?
 
Try adding
Code:
print(type(A))
print(type(B))
before that call to see what type is getting passed. From the error, A or B are integers.
 
phyzguy said:
I'm sure they exist, but are they nested lists? When you write A[0][0], it means A must be of the form [[something]]. If A is a simple variable (which is what the message implies) or a simple list [], then you will get an error when you try to access A[0][0]. What does the print out of A and B look like?

A and B are nested lists indeed.
They look like A=[[1,2,3],[2,3,4],[2,2,3]] for instance.
But what amazes me is that when I try to work with A[0][0] in the python shell, then there is absolutely no problem whereas with my program (with IDLE) an error is raised...
 
jhae2.718 said:
Try adding
Code:
print(type(A))
print(type(B))
before that call to see what type is getting passed. From the error, A or B are integers.

I get type list
 
Edit: Your problem is that when [itex]n[/itex] is 1, A and B are no longer nested lists.

Then, the subscripting fails because A[0][0] doesn't exist when e.g. A = [1]
 
Last edited:
jhae2.718 said:
Edit: Your problem is that when [itex]n[/itex] is 1, A and B are no longer nested lists.

Then, the subscripting fails because A[0][0] doesn't exist when e.g. A = [1]

sorry for the delay I had no internet access for a while
thank you for your help jhae2.718, it looks like this was the problem indeed!
 

Similar threads

Replies
1
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K
Replies
3
Views
3K
  • · Replies 9 ·
Replies
9
Views
10K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 10 ·
Replies
10
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
55
Views
7K