I'm working on an application with a circuit simulator ahkab, and long story short I need to substitute the laplace variable s in some equations with 1j*w. It is much more convenient for me to perform this substitution and others using the names of symbols rather than the symbols themselves. I have come across some odd behaviour.
If you do
>>> x = Symbol('x')
>>> y = Symbol('y')
>>> expr = x + y
x + y
>>> expr.subs('x', 'w')
w + y
Which seems to work as I expect. The problem comes with the symbol is declared with complex = True, here
>>> s = Symbol('s', complex = True)
>>> y = Symbol('y')
>>> expr = s + y
s + y
>>> expr.subs(s, 'w') #Works as expected
w + y
>>> expr.subs('s', 'w') #Has no effect, this is my problem.
s + y
I haven't been able to find information about substitutions in this manner in the docs. To me this seems like a bug, but I don't know what the behaviour should be.
subs
calls sympify
on it arguments. See https://github.com/sympy/sympy/blob/master/sympy/core/basic.py#L845 for example. This means that strings will be converted to symbols but sympify
has no way of knowing that you want your strings to be converted to symbols with non-default assumptions. The only way to do that is to actually pass in the symbol. Note that symbols with different assumptions are not considered equal.
In [1]: from sympy import Symbol
In [2]: x = Symbol('s')
In [3]: y = Symbol('s')
In [4]: x == y
Out[4]: True
In [5]: y = Symbol('s', complex=True)
In [6]: x == y
Out[6]: False