Search code examples
pythonsympymathjaxmathml

Error when running Simpify (Python's Sympy)


I use Python's Sympy module.

I want to print out the mathml code using sympy.printing.mathml.

I am trying to convert a string to a formula using sympify, but I get an error.


test001.py

import sys
sys.path.append('/home/vagrant/.local/lib/python3.4/site-packages')
from sympy import *
from sympy.printing.mathml import mathml

print(mathml(sympify("y=4*x**3+3*x**2+2*x+1"),printer='presentation'))
print(mathml(sympify("y=a*x**3+b*x**2+c*x+d"),printer='presentation'))

When I run test001.py, I get the following error.

$ python test001.py
Traceback (most recent call last):
  File "/home/vagrant/.local/lib/python3.4/site-packages/sympy/core/sympify.py", line 368, in sympify
    expr = parse_expr(a, local_dict=locals, transformations=transformations, evaluate=evaluate)
  File "/home/vagrant/.local/lib/python3.4/site-packages/sympy/parsing/sympy_parser.py", line 965, in parse_expr
    return eval_expr(code, local_dict, global_dict)
  File "/home/vagrant/.local/lib/python3.4/site-packages/sympy/parsing/sympy_parser.py", line 878, in eval_expr
    code, global_dict, local_dict)  # take local objects in preference
  File "<string>", line 1
    Symbol ('y' )=Integer (4 )*Symbol ('x' )**Integer (3 )+Integer (3 )*Symbol ('x' )**Integer (2 )+Integer (2 )*Symbol ('x' )+Integer (1 )
                 ^
SyntaxError: invalid syntax

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "test001.py", line 6, in <module>
    print(mathml(sympify("y=4*x**3+3*x**2+2*x+1"),printer='presentation'))
  File "/home/vagrant/.local/lib/python3.4/site-packages/sympy/core/sympify.py", line 370, in sympify
    raise SympifyError('could not parse %r' % a, exc)
sympy.core.sympify.SympifyError: Sympify of expression 'could not parse 'y=4*x**3+3*x**2+2*x+1'' failed, because of exception being raised:
SyntaxError: invalid syntax (<string>, line 1)
Error in sys.excepthook:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in apport_excepthook
    from apport.fileutils import likely_packaged, get_recent_crashes
  File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
    from apport.report import Report
  File "/usr/lib/python3/dist-packages/apport/report.py", line 30, in <module>
    import apport.fileutils
  File "/usr/lib/python3/dist-packages/apport/fileutils.py", line 23, in <module>
    from apport.packaging_impl import impl as packaging
  File "/usr/lib/python3/dist-packages/apport/packaging_impl.py", line 23, in <module>
    import apt
  File "/usr/lib/python3/dist-packages/apt/__init__.py", line 23, in <module>
    import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'

Original exception was:
Traceback (most recent call last):
  File "/home/vagrant/.local/lib/python3.4/site-packages/sympy/core/sympify.py", line 368, in sympify
    expr = parse_expr(a, local_dict=locals, transformations=transformations, evaluate=evaluate)
  File "/home/vagrant/.local/lib/python3.4/site-packages/sympy/parsing/sympy_parser.py", line 965, in parse_expr
    return eval_expr(code, local_dict, global_dict)
  File "/home/vagrant/.local/lib/python3.4/site-packages/sympy/parsing/sympy_parser.py", line 878, in eval_expr
    code, global_dict, local_dict)  # take local objects in preference
  File "<string>", line 1
    Symbol ('y' )=Integer (4 )*Symbol ('x' )**Integer (3 )+Integer (3 )*Symbol ('x' )**Integer (2 )+Integer (2 )*Symbol ('x' )+Integer (1 )
                 ^
SyntaxError: invalid syntax

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "test001.py", line 6, in <module>
    print(mathml(sympify("y=4*x**3+3*x**2+2*x+1"),printer='presentation'))
  File "/home/vagrant/.local/lib/python3.4/site-packages/sympy/core/sympify.py", line 370, in sympify
    raise SympifyError('could not parse %r' % a, exc)
sympy.core.sympify.SympifyError: Sympify of expression 'could not parse 'y=4*x**3+3*x**2+2*x+1'' failed, because of exception being raised:
SyntaxError: invalid syntax (<string>, line 1)

I want to output something like the following as an execution result.

What should I do?

<mi>y</mi><mo>=</mo><mn>4</mn><mo>&#x2061;<!--FUNCTION APPLICATION--></mo><msup><mi>x</mi><mn>3</mn></msup><mo>+</mo><mn>3</mn><mo>&#x2061;<!--FUNCTION APPLICATION--></mo><msup><mi>x</mi><mn>2</mn></msup><mo>+</mo><mn>2</mn><mo>&#x2061;<!--FUNCTION APPLICATION--></mo><mi>x</mi><mo>+</mo><mn>1</mn>

<mi>y</mi><mo>=</mo><mi>a</mi><mo>&#x2061;<!--FUNCTION APPLICATION--></mo><msup><mi>x</mi><mn>3</mn></msup><mo>+</mo><mi>b</mi><mo>&#x2061;<!--FUNCTION APPLICATION--></mo><msup><mi>x</mi><mn>2</mn></msup><mo>+</mo><mi>c</mi><mo>&#x2061;<!--FUNCTION APPLICATION--></mo><mi>x</mi><mo>+</mo><mi>d</mi>

I am using sympy version 1.4. The Python version uses 3.6.3.


Solution

  • Notice that the error has an arrow pointing at the equal sign

    Symbol ('y' )=Integer (4 )*Symbol ('x' )**Integer (3 )+Integer (3 )*Symbol ('x' )**Integer (2 )+Integer (2 )*Symbol ('x' )+Integer (1 )
                 ^
    SyntaxError: invalid syntax
    

    sympify operates on expressions, but not equations. So instead, you could split the equation string at the equal sign, and apply sympify to the left-hand and right-hand sides separately:

    map(sym.sympify, "y=4*x**3+3*x**2+2*x+1".split('='))
    

    Then form a sympy Equation object out of the two expressions:

    >>> sym.Eq(*map(sym.sympify, "y=4*x**3+3*x**2+2*x+1".split('=')))
    Eq(y, 4*x**3 + 3*x**2 + 2*x + 1)
    

    You can then apply mathml to this SymPy Equality object.


    import sys
    # sys.path.append('/home/vagrant/.local/lib/python3.4/site-packages')
    import sympy as sym
    from sympy.printing.mathml import mathml
    
    
    print(mathml(sym.Eq(*map(sym.sympify, "y=4*x**3+3*x**2+2*x+1".split('='))),printer='presentation'))
    print(mathml(sym.Eq(*map(sym.sympify, "y=a*x**3+b*x**2+c*x+d".split('='))),printer='presentation'))
    

    yields

    <mrow><mi>y</mi><mo>=</mo><mrow><mrow><mn>4</mn><mo>&InvisibleTimes;</mo><msup><mi>x</mi><mn>3</mn></msup></mrow><mo>+</mo><mrow><mn>3</mn><mo>&InvisibleTimes;</mo><msup><mi>x</mi><mn>2</mn></msup></mrow><mo>+</mo><mrow><mn>2</mn><mo>&InvisibleTimes;</mo><mi>x</mi></mrow><mo>+</mo><mn>1</mn></mrow></mrow>
    <mrow><mi>y</mi><mo>=</mo><mrow><mrow><mi>a</mi><mo>&InvisibleTimes;</mo><msup><mi>x</mi><mn>3</mn></msup></mrow><mo>+</mo><mrow><mi>b</mi><mo>&InvisibleTimes;</mo><msup><mi>x</mi><mn>2</mn></msup></mrow><mo>+</mo><mrow><mi>c</mi><mo>&InvisibleTimes;</mo><mi>x</mi></mrow><mo>+</mo><mi>d</mi></mrow></mrow>