Sympy error using Lambda and sympify: "SympifyError: S"
Sympy error using Lambda and sympify: "SympifyError: S"
我在同时使用 sympify 和 Lambda 时遇到问题。我想声明一个接受参数 S
.
的函数 (func_mx
)
然后我想将其包含在符号表达式中 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})
我在同时使用 sympify 和 Lambda 时遇到问题。我想声明一个接受参数 S
.
func_mx
)
然后我想将其包含在符号表达式中 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})