First off, if you want an explicit order of substitution, you should
not use a dictionary. Subs also allows the [(old, new), ...] syntax,
which lets you define a specific order.
But even in this case, it will perform the substitution iteratively.
This is actually a useful feature. For example, you can take the
output of cse() and back-substituted it all in one step, even though
the various substitutions depend on each other.
In your case, I recommend adding additional steps with dummy
variables. So, something like
a, b, x, y = symbols('a b x y')
xx, yy = symbols('xx yy', cls=Dummy)
expr = x**2 + y**3
expr.subs([(x, a*xx + b*yy), (y, b*xx - a*yy), (xx, x), (yy, y)])
This gives:
In [4]: a, b, x, y = symbols('a b x y')
In [5]: xx, yy = symbols('xx yy', cls=Dummy)
In [6]: expr = x**2 + y**3
In [7]: expr.subs([(x, a*xx + b*yy), (y, b*xx - a*yy), (xx, x), (yy, y)])
Out[7]:
2 3
(a⋅x + b⋅y) + (-a⋅y + b⋅x)
Something about this behavior should be added to the docstring of subs.
Aaron Meurer
On Wed, Jul 20, 2011 at 6:58 AM, Martin Richter
<[email protected]> wrote:
> Hi everyone.
>
> I'm concerned about how subs() handles dictionaries, in which
> the values of the dict contain some of the keys, such as
>
> (x**2+y**3).subs({x: a*x+b*y, y: b*x-a*y})
>
> Would it be preferable that the result is
>
> (a*x+b*y)**2+(b*x-a*y)**3
>
> ? Right now subs() re-substitues terms which were already substituted
>
> (a*x+b*y)**2+(b*(a*x+b*y)-a*y)**3
>
> , which seems especially strange, since the dictionary does not imply
> any order for the substitution. I think, if I would like to
> resubstitute substituted terms, I would simply apply subs() several
> times
>
> (x**2+y**3).subs({y: b*x-a*y}).subs({x: a*x+b*y})
>
> A possible solution would be to uniquely change the names of all variables
> which are to be replaced internally, then do the substitution given as
> dictionary and then rename everything back. Would this be a useful thing
> to have?
>
>
> Thanks,
> Martin
>
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/sympy?hl=en.
>
>
--
You received this message because you are subscribed to the Google Groups
"sympy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sympy?hl=en.