我正在使用工具sympy.parsing.mathematica
将Mathematica表达式解析为python语法。我希望能够处理包含数字和字母的可变名称。
例如,当通过调用"1 + a23b + 4"
解析字符串mathematica("1 + a23b")
时,将得到输出"a23*b + 1"
。如何指示我希望将"a23b"
作为一个单一变量来处理,从而使上面示例中的输出变为"a23b + 1"
?
我尝试通过调用{'a23b':'a23b'}
传递表单mathematica("1 + a23b",{'a23b':'a23b'})
的字典。但是,这会引发带有以下消息的ValueError
ValueError: 'a23b' function form is invalid.
。
有什么办法解决这个问题吗?
发布于 2022-08-25 04:52:09
在SymPy 1.11中,不推荐使用mathematica
解析函数:
In [3]: from sympy.parsing.mathematica import mathematica
In [4]: mathematica("1 + a23b")
<ipython-input-4-925ed25e63e8>:1: SymPyDeprecationWarning:
The ``mathematica`` function for the Mathematica parser is now
deprecated. Use ``parse_mathematica`` instead.
The parameter ``additional_translation`` can be replaced by SymPy's
.replace( ) or .subs( ) methods on the output expression instead.
See https://docs.sympy.org/latest/explanation/active-deprecations.html#mathematica-parser-new
for details.
This has been deprecated since SymPy version 1.11. It
will be removed in a future version of SymPy.
mathematica("1 + a23b")
Out[4]: a₂₃⋅b + 1
相反,建议使用parse_mathematica
函数,它以您希望的方式处理这种情况:
In [5]: from sympy.parsing.mathematica import parse_mathematica
In [6]: parse_mathematica("1 + a23b")
Out[6]: a23b + 1
https://stackoverflow.com/questions/73487323
复制