gpt4 book ai didi

python - 使用 Lambda 和 sympify 时出现 Sympy 错误 : "SympifyError: S"

转载 作者:行者123 更新时间:2023-12-01 02:03:30 27 4
gpt4 key购买 nike

我在同时使用 sympify 和 Lambda 时遇到问题。我想声明一个函数 (func_mx),它接受参数 S

然后我想将其包含在符号表达式中 X

这是我尝试执行的代码:

import sympy as sp

S = sp.Symbol('S')
A = sp.Symbol('A')
mux_m = sp.Symbol('mux_m')
KX = sp.Symbol('KX')

func_mux = sp.Lambda(S, (mux_m * S) / (KX + S))
X = sp.sympify("(func_mux(S) - D) * X", locals={"func_mux": func_mux})

这是我收到的错误,我无法解释它的含义:

 File "utils.py", line 87, in <module>
X = sp.sympify("(func_mux(S) - D) * X", locals={"func_mux": func_mux})
File "/usr/local/lib/python2.7/site-packages/sympy/core/sympify.py", line 354, in sympify
expr = parse_expr(a, local_dict=locals, transformations=transformations, evaluate=evaluate)
File "/usr/local/lib/python2.7/site-packages/sympy/parsing/sympy_parser.py", line 894, in parse_expr
return eval_expr(code, local_dict, global_dict)
File "/usr/local/lib/python2.7/site-packages/sympy/parsing/sympy_parser.py", line 807, in eval_expr
code, global_dict, local_dict) # take local objects in preference
File "<string>", line 1, in <module>
File "/usr/local/lib/python2.7/site-packages/sympy/core/function.py", line 1553, in __call__
return self.expr.xreplace(dict(list(zip(self.variables, args))))
File "/usr/local/lib/python2.7/site-packages/sympy/core/basic.py", line 1101, in xreplace
value, _ = self._xreplace(rule)
File "/usr/local/lib/python2.7/site-packages/sympy/core/basic.py", line 1115, in _xreplace
a_xr = a._xreplace(rule)
File "/usr/local/lib/python2.7/site-packages/sympy/core/basic.py", line 1115, in _xreplace
a_xr = a._xreplace(rule)
File "/usr/local/lib/python2.7/site-packages/sympy/core/basic.py", line 1122, in _xreplace
return self.func(*args), True
File "/usr/local/lib/python2.7/site-packages/sympy/core/cache.py", line 93, in wrapper
retval = cfunc(*args, **kwargs)
File "/usr/local/lib/python2.7/site-packages/sympy/core/compatibility.py", line 809, in wrapper
result = user_function(*args, **kwds)
File "/usr/local/lib/python2.7/site-packages/sympy/core/operations.py", line 30, in __new__
args = list(map(_sympify, args))
File "/usr/local/lib/python2.7/site-packages/sympy/core/sympify.py", line 387, in _sympify
return sympify(a, strict=True)
File "/usr/local/lib/python2.7/site-packages/sympy/core/sympify.py", line 303, in sympify
raise SympifyError(a)
sympy.core.sympify.SympifyError: SympifyError: S

最佳答案

不幸的是,“S”是在 sympy 中预定义的,因此被 sympify 解释为 sympy.core.singleton.SingletonRegistry,请参阅 documentation :

The registry for the singleton classes (accessible as S).

当您在简单表达式上尝试 sympify 时,这一点会更加明显,例如

>> sp.sympify('S + A')
TypeError: unsupported operand type(s) for +: 'SingletonRegistry' and 'Symbol'

您可以通过将“S”替换为任何其他字母(例如小写的“s”)来避免此问题。如果您想保留字母“S”,您还可以使用您之前定义的符号将其添加到您的 locals 中:

X = sp.sympify("(funcmux(S) - D) * X", locals={"funcmux": funcmux, "S": S})

关于python - 使用 Lambda 和 sympify 时出现 Sympy 错误 : "SympifyError: S",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49320273/

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