On Wednesday 08 July 2009, Kwankyu wrote:
> Hi,
>
> I was surprised to see
>
> sage: R.<x,y>=QQ[]
> sage: g=x+y
> sage: g.subs({x:x+1,y:x*y})
> x*y + x + y + 1
>
> So the order of substitution matters...unfortunately.
>
> sage: g.subs({x:x+1}).subs({y:x*y})
> x*y + x + 1
> sage: g.subs({y:x*y}).subs({x:x+1})
> x*y + x + y + 1
>
> So the order seems to be from right to left.
I am not sure what you mean by right to left, in the example above the
substitution is in the order of standard Python method chaining, where the
second subs() call is on whatever the first subs() call returns:
sage: gbar = g.subs({x:x+1})
x + y + 1
sage: gbar.subs({y:x*y})
x*y + x + 1
> This seems to me unnatural. Anyway this is undocumented.
This behaviour is documented in the subs() docstring:
.. note::
The evaluation is performed by evaluating every
``variable:value`` pair separately. This has side effects
if e.g. x=y, y=z is provided. If x=y is evaluated first,
all x variables will be replaced by z eventually.
> What should be done to this?
We could try to re-implement subs() such that it doesn't have these side-
effects but is still as fast.
Martin
--
name: Martin Albrecht
_pgp: http://pgp.mit.edu:11371/pks/lookup?op=get&search=0x8EF0DC99
_otr: 47F43D1A 5D68C36F 468BAEBA 640E8856 D7951CCF
_www: http://www.informatik.uni-bremen.de/~malb
_jab: [email protected]
--~--~---------~--~----~------------~-------~--~----~
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/sage-devel
URLs: http://www.sagemath.org
-~----------~----~----~----~------~----~------~--~---