首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python Sympy MathML: mathml vs print_mathml

Python Sympy MathML: mathml vs print_mathml
EN

Stack Overflow用户
提问于 2014-12-27 01:05:10
回答 2查看 1.8K关注 0票数 3

我有什么不明白的吗?

下面的代码运行良好:

代码语言:javascript
复制
from sympy.printing.mathml import print_mathml
s = "x**2 + 8*x + 16"
print_mathml(s)

但这会产生一个错误:

代码语言:javascript
复制
from sympy.printing.mathml import mathml
s = "x**2 + 8*x + 16"
print mathml(s)

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

代码语言:javascript
复制
from sympy.printing.mathml import mathml
from sympy.utilities.mathml import c2p
s = "x**2 + 8*x + 16"
print(c2p(mathml(s))

但是如上所述,mathml()函数抛出了一个错误。

EN

回答 2

Stack Overflow用户

发布于 2014-12-27 01:20:47

我还没有足够的名誉点来评论,所以我转而回答。

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

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

代码部分:

代码语言:javascript
复制
def mathml(expr, **settings):
    """Returns the MathML representation of expr"""
    return MathMLPrinter(settings).doprint(expr)

-

代码语言:javascript
复制
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

你得到了哪个错误?

你拿到这个了吗:

代码语言:javascript
复制
[....]
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'

我猜是图书馆出了点问题。

代码语言:javascript
复制
unistr = mathML.toxml()

您可以查看此处的http://docs.sympy.org/dev/_modules/sympy/printing/mathml.html以查看该文件。

票数 3
EN

Stack Overflow用户

发布于 2015-01-28 07:34:38

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

代码语言:javascript
复制
x = symbols('x')
expr = x**2 + 8*x + 16

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

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27659790

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档