How to correct this bug in power series substitution?

37 views

Martin Raum

Jul 10, 2012, 2:01:15 PM7/10/12
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

Jul 10, 2012, 3:48:56 PM7/10/12
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

Jul 10, 2012, 3:58:36 PM7/10/12
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
> For more options, visit this group at
> URL: http://www.sagemath.org

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

Julien Puydt

Jul 10, 2012, 11:57:56 PM7/10/12