Fix Sympy Not Working: TypeError

  • Thread starter ergospherical
  • Start date
In summary, the conversation discusses the use of sympy to create a matrix using nested lists as the input. The speaker mentions an error message they encountered when trying to use a variable named "glist" as the argument for the Matrix function, and explains that the error is caused by not using a nested list structure. They also mention that using the numpy.ndarray is a better alternative to creating matrices.
  • #1
ergospherical
1,072
1,365
Computer says "TypeError: Data type not understood; expecting list of lists or lists of values."

Python:
import sympy as sp

t,r,a,b = sp.symbols('t r a b')

g00 = -(1-(1/r))
g01 = 0
g02 = 0
g03 = 0
g10 = 0
g11 = 1/(1-(1/r))
g12 = 0
g13 = 0
g20 = 0
g21 = 0
g22 = r**2
g23 = 0
g30 = 0
g31 = 0
g32 = 0
g33 = (r**2)*((sp.sin(a))**2)

g = sp.Matrix([g00,g01,g02,g03],[g10,g11,g12,g13],[g20,g21,g22,g23],[g30,g31,g32,g33])
print(g)
 
Technology news on Phys.org
  • #2
Make a list as a variable named “glist”

and then use it as the argument to Matrix

find an example online using sp.Matrix as a reference.
 
  • Like
Likes ergospherical
  • #3
You just need to make the Matrix input a nested list, like this:
Python:
g = sp.Matrix([[g00,g01,g02,g03],[g10,g11,g12,g13],[g20,g21,g22,g23],[g30,g31,g32,g33]])

This is what the error message means when it says a "list of lists".
 
  • Like
Likes sysprog, ergospherical and pbuk
  • #4
Aah the old list of lists trick -- Maxwell Smart of Get Smart!
 
  • Like
Likes sysprog
  • #5
Matrices as lists of lists are horrible, numpy.ndarray is much nicer.
 
  • Like
Likes jim mcnamara and jedishrfu

FAQ: Fix Sympy Not Working: TypeError

Why am I getting a TypeError when using Sympy?

A TypeError in Sympy usually occurs when there is a mismatch between the type of data being used and the type of data expected by the function being used. For example, trying to perform a mathematical operation on a string instead of a number can result in a TypeError.

How can I fix a TypeError in Sympy?

The first step in fixing a TypeError in Sympy is to identify where the error is occurring. This can be done by looking at the line number and the specific function that is causing the error. From there, you can check to see if the data being used is of the correct type and make any necessary changes.

Can a TypeError in Sympy be caused by incorrect syntax?

Yes, a TypeError in Sympy can be caused by incorrect syntax. If the syntax of a function is not correct, it can result in a TypeError when the function is executed. It is important to double check the syntax of your code when troubleshooting a TypeError.

Is it possible to get a TypeError in Sympy if the library is not imported correctly?

Yes, a TypeError in Sympy can occur if the library is not imported correctly. If the library is not imported at all, the functions from the library will not be recognized and will result in a TypeError when used. Make sure to properly import the Sympy library at the beginning of your code.

Are there any common mistakes that can lead to a TypeError in Sympy?

Yes, there are a few common mistakes that can lead to a TypeError in Sympy. Some of these include using incorrect data types, incorrect syntax, and not importing the library correctly. It is important to carefully check your code for these mistakes when troubleshooting a TypeError in Sympy.

Similar threads

Replies
9
Views
4K
Replies
15
Views
4K
Back
Top