Python Sympy MathML:MathML与print\u mathm

2024-06-24 13:31:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我不明白什么?在

以下代码可以正常工作:

from sympy.printing.mathml import print_mathml
s = "x**2 + 8*x + 16"
print_mathml(s)

但这会产生一个错误:

^{pr2}$

最后,我要做的是将“x**2+8*x+16”转换成表示MathML,以便在web上输出。所以我的计划是在字符串上使用mathml()函数,然后通过c2p发送输出,如下所示:

from sympy.printing.mathml import mathml
from sympy.utilities.mathml import c2p
s = "x**2 + 8*x + 16"
print(c2p(mathml(s))

但如前所述,mathml()函数抛出一个错误。在


Tags: 函数字符串代码fromimportweb错误mathml
2条回答

正如@Emyen指出的,问题是你的输入是一个字符串。使用sympify将字符串转换为表达式,或者,更好的方法是使用符号将表达式创建为Python表达式,例如

x = symbols('x')
expr = x**2 + 8*x + 16

请参阅https://github.com/sympy/sympy/wiki/Idioms-and-Antipatterns#strings-as-input,了解使用字符串而不是表达式是个坏主意的原因。在

我还没有足够的声誉点来评论,所以我现在就回答。在

根据辛普森医生的说法。Sympy.printing.mathml的mathml函数需要表达式。但用字符串格式?在

{a1}

代码部分:

def mathml(expr, **settings):
    """Returns the MathML representation of expr"""
    return MathMLPrinter(settings).doprint(expr)

-

^{pr2}$

你犯了什么错误?在

你有这个吗:

[....]
return MathMLPrinter(settings).doprint(expr)   File "C:\Python27\lib\site-packages\sympy\printing\mathml.py", line 39, in doprint
unistr = mathML.toxml() AttributeError: 'str' object has no attribute 'toxml'

我想是图书馆出了问题。在

unistr = mathML.toxml()

你可以看看这里http://docs.sympy.org/dev/_modules/sympy/printing/mathml.html 查看文件。在

相关问题 更多 >