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
callssympify
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 butsympify
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.