On Tue, Jul 26, 2011 at 4:41 PM, Chris Smith <smi...@gmail.com> wrote:
> On Tue, Jul 26, 2011 at 3:26 PM, Aaron Meurer <asmeu...@gmail.com> wrote:
>> On Tue, Jul 26, 2011 at 9:02 AM, Chris Smith <smi...@gmail.com> wrote:
>>>> Actually, I see now that this is the wrong approach.  subs should be
>>>> able to do multiple substitutions at once at the core level.  The
>>>> reason is efficiency. Consider the following
>>>
>>> Just to be clear, by "multiple substitutions at once" do you mean
>>> doing multiple substitutions before rebuilding the expression? So `(x
>>> + y + z).subs([(x, 1), (y, 2)])` would iterate through all
>>> replacements with the `[x, y, z]` args before rebuilding the Add? BTW,
>>> to keep this static one would either have to remove the subexpressions
>>> after a successful replacement or use dummies as intermediates.
>>
>> If you do it right, there should be no need for Dummys, because you
>> should only hit each part of the expression exactly once, and never
>> recurse through something that was newly put there by subs.  This is
>> how xreplace works (see the code).  Actually, it's important that you
>> do hit each part only once, first, because it's more efficient that
>> way, and second, because if you don't you risk substituting something
>> twice.
>>
> Yes, that is what I meant by "remove the subexpressions after a
> successful replacement" -- they have t be removed from consideration
> after a successful hit.
>

I think if you carefully make sure that each part of the expression is
hit only once, there is no need to remove anything.  It's actually
pretty simple. Iterate through the args recursively, if you see an
expression you want to replace, put it in a list of new args.
Otherwise, put the original expresion in the new args list.  Once you
reach the end, rebuild.

I'm pretty sure Ronan's xreplace does this, and it's only four lines.

Aaron Meurer

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To post to this group, send email to sympy@googlegroups.com.
To unsubscribe from this group, send email to 
sympy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sympy?hl=en.

Reply via email to