python - SymPy: Swap two variables -
in expression like
import sympy = sympy.symbol('a') b = sympy.symbol('b') x = + 2*b
i'd swap a
, b
retrieve b + 2*a
. tried
y = x.subs([(a, b), (b, a)]) y = x.subs({a: b, b: a})
but neither works; result 3*a
in both cases b
, reason, gets replaced first.
any hints?
there simultaneous
argument can pass substitution, ensure substitutions happen simultaneously , don't interfere 1 doing now.
y = x.subs({a:b, b:a}, simultaneous=true)
outputs:
2*a + b
if keyword
simultaneous
true
, subexpressions not evaluated until substitutions have been made.
Comments
Post a Comment