I'm having a problem to read a function using the parse_expr
sympy function.
Here's how I'm using it:
from sympy import*
import sympy as smp
import numpy as np
from sympy.parsing.sympy_parser import parse_expr
print("Welcome...")
x=smp.Symbol('x')
function=input("Enter your function in terms of X\nIf you want to use Fixed point method input de G(x) expresion\n")
function=parse_expr(function, x)
And here's what I'm getting:
Welcome...
Enter your function in terms of X
If you want to use Fixed point method input de G(x) expresion
2*x
Traceback (most recent call last):
File "c:\Users\jp159\Desktop\Desktop\Studies\Exercises.py", line 8, in <module>
function=parse_expr(function, x)
File "C:\Users\jp159\AppData\Local\Programs\Python\Python39\lib\site-packages\sympy\parsing\sympy_parser.py", line 988, in parse_expr
raise TypeError('expecting local_dict to be a dict')
TypeError: expecting local_dict to be a dict
CodePudding user response:
In an isympy session initialized with:
>>> from __future__ import division
>>> from sympy import *
>>> x, y, z, t = symbols('x y z t')
>>> k, m, n = symbols('k m n', integer=True)
>>> f, g, h = symbols('f g h', cls=Function)
>>> init_printing()
Documentation can be found at https://docs.sympy.org/1.8/
Since local_dict is optional, lets omit it:
In [1]: parse_expr('2*x')
Out[1]: 2⋅x
but we can make a dict - that references one of the defined symbols:
In [4]: parse_expr('2*x',{'x':x})
Out[4]: 2⋅x
In [5]: parse_expr('2*x',{'x':y})
Out[5]: 2⋅y
I can also use a string that doesn't look like any defined symbol:
In [7]: parse_expr('2*X')
Out[7]: 2⋅X
In [8]: _.free_symbols
Out[8]: {X}
One key skill for new programmers is reading the documentation. Yes, often that can be hard to understand, but still it should be the first stop. SO (and general web searches) should come later.
sympy
is written in Python, so as basic knowledge of that language is assumed.