Fix Sympy Not Working: TypeError

  • Thread starter Thread starter ergospherical
  • Start date Start date
Click For Summary
SUMMARY

The discussion addresses a TypeError encountered in Sympy when creating a matrix. The error message indicates that the input to the Matrix function must be a "list of lists" or a "list of values." The correct implementation involves defining the matrix using nested lists, as shown in the example: g = sp.Matrix([[g00,g01,g02,g03],[g10,g11,g12,g13],[g20,g21,g22,g23],[g30,g31,g32,g33]]). This adjustment resolves the TypeError and allows for proper matrix creation in Sympy.

PREREQUISITES
  • Familiarity with Python programming
  • Understanding of Sympy library (version 1.8 or later)
  • Basic knowledge of matrix operations
  • Concept of nested lists in Python
NEXT STEPS
  • Review the Sympy documentation on sp.Matrix for additional examples
  • Explore Python's list data structure and its manipulation techniques
  • Learn about error handling in Python to better understand TypeErrors
  • Investigate alternatives to Sympy for matrix operations, such as NumPy
USEFUL FOR

This discussion is beneficial for Python developers, mathematicians using Sympy for symbolic computation, and anyone troubleshooting matrix-related errors in their code.

ergospherical
Science Advisor
Homework Helper
Education Advisor
Insights Author
Messages
1,100
Reaction score
1,387
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
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   Reactions: ergospherical
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   Reactions: sysprog, ergospherical and pbuk
Aah the old list of lists trick -- Maxwell Smart of Get Smart!
 
  • Like
Likes   Reactions: sysprog
Matrices as lists of lists are horrible, numpy.ndarray is much nicer.
 
  • Like
Likes   Reactions: jim mcnamara and jedishrfu

Similar threads

  • · Replies 9 ·
Replies
9
Views
5K
  • · Replies 15 ·
Replies
15
Views
4K