On Jul 8, 6:51 pm, Martin Albrecht <[email protected]>
wrote:
> 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.
>


I mean the substitution y:x*y is applied first in the following

sage: R.<x,y>=QQ[]
sage: g=x+y
sage: g.subs({x:x+1,y:x*y})
x*y + x + y + 1

where I think applying x:x+1 first seems intuitive if order ever
should be significant.

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to