Я использую модуль Sympy в Python.
Я хочу распечатать код mathml, используя sympy.printing.mathml.
Я пытаюсь преобразовать строку в формулу с помощью sympify, ноЯ получаю ошибку.
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'))
Когда я запускаю test001.py , я получаю следующую ошибку.
$ 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)
Я хочу вывести что-то вроде следующего в качестве результата выполнения.
Что мне делать?
<mi>y</mi><mo>=</mo><mn>4</mn><mo>⁡<!--FUNCTION APPLICATION--></mo><msup><mi>x</mi><mn>3</mn></msup><mo>+</mo><mn>3</mn><mo>⁡<!--FUNCTION APPLICATION--></mo><msup><mi>x</mi><mn>2</mn></msup><mo>+</mo><mn>2</mn><mo>⁡<!--FUNCTION APPLICATION--></mo><mi>x</mi><mo>+</mo><mn>1</mn>
<mi>y</mi><mo>=</mo><mi>a</mi><mo>⁡<!--FUNCTION APPLICATION--></mo><msup><mi>x</mi><mn>3</mn></msup><mo>+</mo><mi>b</mi><mo>⁡<!--FUNCTION APPLICATION--></mo><msup><mi>x</mi><mn>2</mn></msup><mo>+</mo><mi>c</mi><mo>⁡<!--FUNCTION APPLICATION--></mo><mi>x</mi><mo>+</mo><mi>d</mi>
Я использую Simpy версии 1.4.Версия Python использует 3.6.3.