Search code examples
sympylimitrootintegral

How to use Limit and Integral in Sympy Parser


I want to know how to evaluates equations with operations in sympy parser(NOT with sympy.series.Limit Function), such as root, limit, or integral.

from sympy.parsing.sympy_parser import parse_expr
temp = parse_expr("limit(1/x, x, 0, +)", transformations='all')

    Traceback (most recent call last):
  File "c:\Users\asdf\AppData\Local\Programs\Python\Python310\Lib\site-packages\data_manage\test.py", line 70, in <module>    parse = parse_expr("limit(1/x, x, 0, +)", transformations='all')
  File "C:\Users\asdf\AppData\Local\Programs\Python\Python310\lib\site-packages\sympy\parsing\sympy_parser.py", line 1105, in parse_expr
    raise e from ValueError(f"Error from parse_expr with transformed code: {code!r}")
  File "C:\Users\asdf\AppData\Local\Programs\Python\Python310\lib\site-packages\sympy\parsing\sympy_parser.py", line 1096, in parse_expr
    rv = eval_expr(code, local_dict, global_dict)
  File "C:\Users\asdf\AppData\Local\Programs\Python\Python310\lib\site-packages\sympy\parsing\sympy_parser.py", line 915, 
in eval_expr
    expr = eval(
  File "<string>", line 1
    limit (Integer (1 )/Symbol ('x' ),Symbol ('x' ),Integer (0 ),+)
                                                                  ^
SyntaxError: invalid syntax

Solution

  • You need to wrap the limit direction into a string literal:

    temp = parse_expr("limit(1/x, x, 0, '+')", transformations='all')