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 

from docs subs:

if keyword simultaneous true, subexpressions not evaluated until substitutions have been made.


Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -