How to correct this bug in power series substitution?

37 views
Skip to first unread message

Martin Raum

unread,
Jul 10, 2012, 2:01:15 PM7/10/12
to sage-...@googlegroups.com
Hallo all!

I have found the following bug which mostly occurs when substituting power series over inexact rings. E.g.

R.<x> = CC[[]]
x.subs(x = x**2)

gives x. Same for the base field Qp(7).

The reason is that the generic method subs iterates the generators of R (in this case x) and checks whether any keyword matches the string representation of that generator. But

x._repr_()

does not give 'x', but 0.0... + 1.0... x.

There are two obvious ways to fix this: Change _repr_  or change subs. By changing _repr_, we would accept that some elements are wrongly printed as exact elements, but this approach would correspond to the fact that

CC(0) == CC(0)

is true. By changing subs, we would introduce a specialized method for power series rings, while so far any ParentWithGens comes with the same implementation.

I would rather change _repr_. What do you think?

Best, Martin

David Roe

unread,
Jul 10, 2012, 3:48:56 PM7/10/12
to sage-...@googlegroups.com
The reason is that the generic method subs iterates the generators of R (in this case x) and checks whether any keyword matches the string representation of that generator. But

x._repr_()

does not give 'x', but 0.0... + 1.0... x.

There are two obvious ways to fix this: Change _repr_  or change subs. By changing _repr_, we would accept that some elements are wrongly printed as exact elements, but this approach would correspond to the fact that

CC(0) == CC(0)

is true. By changing subs, we would introduce a specialized method for power series rings, while so far any ParentWithGens comes with the same implementation.

I would rather change _repr_. What do you think?
 
I would rather change subs.  Why not use the variable name rather than the string representation?  In this example it's not clear why it's useful to see "zero" entries, but consider the following:

sage: K = Qp(5,4,print_mode='terse'); R.<x> = K[]
sage: a = K(6).log(); y = a*x; y
(555 + O(5^4))*x
sage: z = 555*x; z
(555 + O(5^4))*x
sage: y-z
(0 + O(5^4))*x

The print representation lets you know that the degree of the result is uncertain (in this case the degree is secretly 1, but sometimes the degree could be 0).
David

William Stein

unread,
Jul 10, 2012, 3:58:36 PM7/10/12
to sage-...@googlegroups.com
On Tue, Jul 10, 2012 at 11:01 AM, Martin Raum
<Marti...@matha.rwth-aachen.de> wrote:
> Hallo all!
>
> I have found the following bug which mostly occurs when substituting power
> series over inexact rings. E.g.
>
> R.<x> = CC[[]]
> x.subs(x = x**2)
>
> gives x. Same for the base field Qp(7).

Quick remark. It is far, far better to use a dictionary as input to
subs, if you can stand typing the extra braces. For example, the
above issue works fine with a dictionary:

sage: R.<x> = CC[[]]
sage: x.subs({x:x**2})
0.000000000000000 + 0.000000000000000*x + 1.00000000000000*x^2

The issue is that if you use subs(foo=bar), then Python interprets foo
as the keyword (=string) "foo"; there is *no way* to do subs(x=x^2) in
Python and have x be a Python variable. Here's another example where
subs with a dictionary is much better:

sage: R.<x,x> = CC[[]]; f = R.0 + R.1; f
x + x
sage: f.subs({R.0:5}) == 5 + R.1
True
sage: f.subs({R.1:5}) == 5 + R.0
True

There's no way with strings to ever do the above...



>
> The reason is that the generic method subs iterates the generators of R (in
> this case x) and checks whether any keyword matches the string
> representation of that generator. But
>
> x._repr_()
>
> does not give 'x', but 0.0... + 1.0... x.
>
> There are two obvious ways to fix this: Change _repr_ or change subs. By
> changing _repr_, we would accept that some elements are wrongly printed as
> exact elements, but this approach would correspond to the fact that
>
> CC(0) == CC(0)
>
> is true. By changing subs, we would introduce a specialized method for power
> series rings, while so far any ParentWithGens comes with the same
> implementation.
>
> I would rather change _repr_. What do you think?
>
> Best, Martin
>
> --
> --
> To post to this group, send an email to sage-...@googlegroups.com
> To unsubscribe from this group, send an email to
> sage-devel+...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/sage-devel
> URL: http://www.sagemath.org



--
William Stein
Professor of Mathematics
University of Washington
http://wstein.org

Julien Puydt

unread,
Jul 10, 2012, 11:57:56 PM7/10/12
to sage-...@googlegroups.com
Le 10/07/2012 21:58, William Stein a �crit :
I can't help but notice that help(f.subs) indeed says the argument
should be a dict, but most examples don't do that (in fact, only the
very first one uses a dict!).

Documentation bug?

Snark
Reply all
Reply to author
Forward
0 new messages