Python 中的 WolframClient:如何将 Wolframclient.evaluate 的输出转换为 sympy 表达式?

发布于 2025-01-18 12:40:49 字数 326 浏览 5 评论 0原文

我在Python中使用了Wolframclient,在计算的某些点,我有以下输出

(Plus[-9, Times[2, Power[Global`y, 4]]], Plus[Times[3, Global`x], Times[-2, Power[Global`y, 3]]])

,现在需要将此输出转换为Sympy格式,类似方法

    [-9 + 2*y**4, 3*x - 2*y**3]

可以做到这一点?

谢谢, 卢卡

I'm using wolframclient in Python and at certain point of my computation I have an output like the following

(Plus[-9, Times[2, Power[Global`y, 4]]], Plus[Times[3, Global`x], Times[-2, Power[Global`y, 3]]])

Now I need to convert this output into sympy format, something like

    [-9 + 2*y**4, 3*x - 2*y**3]

Is there a way to do that?

Thanks,
Luca

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

徒留西风 2025-01-25 12:40:49

Mathematica解析器似乎不喜欢元组,因此,如果您可以将两个部分分开并用空字符串替换“全局”,则可以工作:

>>> a = 'Plus[-9, Times[2, Power[Global`y, 4]]]'
>>> b = 'Plus[Times[3, Global`x], Times[-2, Power[Global`y, 3]]]'
>>> from sympy.parsing.mathematica import parse_mathematica as p # in current dev version?
>>> [p(i.replace('Global`','')) for i in (a,b)]
[2*y**4 - 9, 3*x - 2*y**3]

The mathematica parser doesn't seem to like the tuple, so if you can get the two parts separate and replace the "Global`" with a null string, it can work:

>>> a = 'Plus[-9, Times[2, Power[Global`y, 4]]]'
>>> b = 'Plus[Times[3, Global`x], Times[-2, Power[Global`y, 3]]]'
>>> from sympy.parsing.mathematica import parse_mathematica as p # in current dev version?
>>> [p(i.replace('Global`','')) for i in (a,b)]
[2*y**4 - 9, 3*x - 2*y**3]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文