Search code examples
pythonsympysymbolic-math

Sympy error using Lambda and sympify: "SympifyError: S"


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

I then want to include this in a symbolic expression 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

Solution

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

    The registry for the singleton classes (accessible as 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:

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