On Wed, Jul 8, 2009 at 1:28 AM, Kwankyu<[email protected]> 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. This seems to me
> unnatural. Anyway this is undocumented.
Actually, i guess it is documented. However, I consider it a serious
design flaw.
Many thanks for pointing this out!!
I consider this a serious design flaw for the following reasons:
(1) it is too hard to understand the above behavior, since it depends
on the hash values symbolic variables, which might possibly be
system-dependent.
(2) it is totally inconsistent with the behavior for symbolic
expressions, where things are done right.
(3) it is totally inconsistent with the behavior of *homomorphisms*,
where things are also done right.
Here is a session to illustrate the above points:
# BAD
sage: R.<x,y>=QQ[]
sage: g=x+y
sage: g.subs({x:x+1,y:x*y})
x*y + x + y + 1
# BAD
sage: R.<x,y>=QQ[]
sage: g=x+y
sage: g.subs(x=x+1,y=x*y)
x*y + x + y + 1
# GOOD
sage: R.<x,y>=QQ[]
sage: phi = R.hom([x+1,x*y])
sage: g=x+y
sage: phi(g)
x*y + x + 1
# GOOD
sage: var('x,y')
sage: g = x+y
sage: g.subs({x:x+1,y:x*y})
x*y + x + 1
# GOOD
sage: var('x,y')
sage: g = x+y
sage: g.subs(x=x+1,y=x*y)
x*y + x + 1
> What should be done to this?
1. I suggest that for now you use Hom, as illustrated above, as a workaround.
2. I think subs should be reimplemented using Hom. Note that this
could break existing code, so will have to be done carefully. We
can leave the old behavior in for speed, but as a non-default option.
I really hope Martin agrees with me about this, since having his
support will make this step easier.
3. Come up with a fast way to implement the new behavior.
This is now trac #6482: http://sagetrac.org/sage_trac/ticket/6482
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---