运行 简化时出错(Python 的 Sympy)
Error when running Simpify (Python's Sympy)
我使用 Python 的 Sympy 模块。
我想使用 sympy.printing.mathml 打印出 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>
我正在使用 sympy 1.4 版。
Python版本使用3.6.3.
注意错误有一个指向等号的箭头
Symbol ('y' )=Integer (4 )*Symbol ('x' )**Integer (3 )+Integer (3 )*Symbol ('x' )**Integer (2 )+Integer (2 )*Symbol ('x' )+Integer (1 )
^
SyntaxError: invalid syntax
sympify
对表达式进行运算,但不对方程式进行运算。因此,您可以在等号处拆分等式字符串,
并将 sympify
分别应用到 left-hand 和 right-hand 边:
map(sym.sympify, "y=4*x**3+3*x**2+2*x+1".split('='))
然后从两个表达式中形成一个 sympy Equation 对象:
>>> 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)
然后您可以将 mathml
应用于此 SymPy Equality
对象。
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'))
产量
<mrow><mi>y</mi><mo>=</mo><mrow><mrow><mn>4</mn><mo>⁢</mo><msup><mi>x</mi><mn>3</mn></msup></mrow><mo>+</mo><mrow><mn>3</mn><mo>⁢</mo><msup><mi>x</mi><mn>2</mn></msup></mrow><mo>+</mo><mrow><mn>2</mn><mo>⁢</mo><mi>x</mi></mrow><mo>+</mo><mn>1</mn></mrow></mrow>
<mrow><mi>y</mi><mo>=</mo><mrow><mrow><mi>a</mi><mo>⁢</mo><msup><mi>x</mi><mn>3</mn></msup></mrow><mo>+</mo><mrow><mi>b</mi><mo>⁢</mo><msup><mi>x</mi><mn>2</mn></msup></mrow><mo>+</mo><mrow><mi>c</mi><mo>⁢</mo><mi>x</mi></mrow><mo>+</mo><mi>d</mi></mrow></mrow>
我使用 Python 的 Sympy 模块。
我想使用 sympy.printing.mathml 打印出 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>
我正在使用 sympy 1.4 版。 Python版本使用3.6.3.
注意错误有一个指向等号的箭头
Symbol ('y' )=Integer (4 )*Symbol ('x' )**Integer (3 )+Integer (3 )*Symbol ('x' )**Integer (2 )+Integer (2 )*Symbol ('x' )+Integer (1 )
^
SyntaxError: invalid syntax
sympify
对表达式进行运算,但不对方程式进行运算。因此,您可以在等号处拆分等式字符串,
并将 sympify
分别应用到 left-hand 和 right-hand 边:
map(sym.sympify, "y=4*x**3+3*x**2+2*x+1".split('='))
然后从两个表达式中形成一个 sympy Equation 对象:
>>> 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)
然后您可以将 mathml
应用于此 SymPy Equality
对象。
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'))
产量
<mrow><mi>y</mi><mo>=</mo><mrow><mrow><mn>4</mn><mo>⁢</mo><msup><mi>x</mi><mn>3</mn></msup></mrow><mo>+</mo><mrow><mn>3</mn><mo>⁢</mo><msup><mi>x</mi><mn>2</mn></msup></mrow><mo>+</mo><mrow><mn>2</mn><mo>⁢</mo><mi>x</mi></mrow><mo>+</mo><mn>1</mn></mrow></mrow>
<mrow><mi>y</mi><mo>=</mo><mrow><mrow><mi>a</mi><mo>⁢</mo><msup><mi>x</mi><mn>3</mn></msup></mrow><mo>+</mo><mrow><mi>b</mi><mo>⁢</mo><msup><mi>x</mi><mn>2</mn></msup></mrow><mo>+</mo><mrow><mi>c</mi><mo>⁢</mo><mi>x</mi></mrow><mo>+</mo><mi>d</mi></mrow></mrow>