gpt4 book ai didi

Python Sympy MathML : mathml vs print_mathml

转载 作者:太空宇宙 更新时间:2023-11-03 13:42:36 26 4
gpt4 key购买 nike

我不明白什么?

下面的代码工作正常:

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

但这会产生一个错误:

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

最终,我要做的是将“x**2 + 8*x + 16”转换为表示形式的 MathML 以在网络上输出。所以我的计划是对字符串使用 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() 函数会抛出一个错误。

最佳答案

我还没有足够的声誉点数来发表评论,所以我会改为回答。

根据 Sympy 文档。 Sympy.printing.mathml 的 mathml 函数需要一个表达式。但是以字符串格式?

http://docs.sympy.org/dev/modules/printing.html#sympy.printing.mathml.mathml

代码部分:

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

-

def doprint(self, expr):
"""
Prints the expression as MathML.
"""
mathML = Printer._print(self, expr)
unistr = mathML.toxml()
xmlbstr = unistr.encode('ascii', 'xmlcharrefreplace')
res = xmlbstr.decode()
return res

你遇到了什么错误?

你明白了吗:

[....]
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查看文件。

关于Python Sympy MathML : mathml vs print_mathml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27659790/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com