使用Lambda的Sympy错误并表示同情:“SympifyError:S”

时间:2022-06-02 20:23:34

I'm having trouble using sympify and Lambda together. I want to declare a function (func_mx) which takes the argument S.

我在使用sympify和Lambda时遇到了麻烦。我想声明一个带有参数S的函数(func_mx)。

I then want to include this in a symbolic expression X

然后我想把它包含在符号表达式X中

Here is the code I'm trying to execute:

这是我正在尝试执行的代码:

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})

This is the error I am getting, I'm having trouble interpreting what it means:

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

 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

1 个解决方案

#1


2  

Unfortunately "S" is predefined in sympy and is therefore interpreted by sympify as sympy.core.singleton.SingletonRegistry, see the documentation:

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

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

单例类的注册表(可以作为S访问)。

This is a bit more obvious when you try sympify on a simple expression, e.g.

当您尝试同意一个简单的表达式时,这一点会更加明显,例如:

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

You can avoid this problem by replacing "S" with any other letter, e.g a lowercase "s". If you want to keep the letter "S", you can also add it to your locals using the symbol you defined previously:

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

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

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

#1


2  

Unfortunately "S" is predefined in sympy and is therefore interpreted by sympify as sympy.core.singleton.SingletonRegistry, see the documentation:

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

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

单例类的注册表(可以作为S访问)。

This is a bit more obvious when you try sympify on a simple expression, e.g.

当您尝试同意一个简单的表达式时,这一点会更加明显,例如:

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

You can avoid this problem by replacing "S" with any other letter, e.g a lowercase "s". If you want to keep the letter "S", you can also add it to your locals using the symbol you defined previously:

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

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

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