Stange behavior with QQbar[z]

28 views
Skip to first unread message

Thierry Dumont

unread,
Nov 7, 2016, 8:59:14 AM11/7/16
to sage-support
Something strange (at least for me).

I am computing with polynomials in QQbar[z]. So I do:

sage: P.<z>=QQbar[]

And I define 2 polynomials N and D. Coefficients of N and D are all
real, algebraic numbers (say like QQbar(sqrt(3))) and I consider the
rational fraction R=N/D

sage: R.parent()
Fraction Field of Univariate Polynomial Ring in z over Algebraic Field

Now, I want to look at R on the imaginary axis. I do (may be this is not
a good idea):

sage: PP.<x>=QQbar[]
sage: RIaxe=R(z=QQbar(I)*x)
sage: RIaxe.parent()
Fraction Field of Univariate Polynomial Ring in x over Algebraic Field

it seems ok.

I take the numerator:
sage: pp=RIaxe.numerator()
sage: print pp
0.05638275005921478?*I*x^3 - 0.4022786138875136?*x^2 - 1/2*I*x + 1

okay.

sage: pp.parent()
Univariate Polynomial Ring in x over Algebraic Field

okay.

-----
Now starts my problem; I want to extract from pp the polynomial which
has real coefficients:
sage: l=[s for s in pp.coefficients() if s.imag()==0]
sage: print l
[1, -0.4022786138875136?]

okay. Now let us construct a polynomial from l:
sage: print sum([l[i]*z^i for i in range(0,len(l))])
(-0.4022786138875136? + 0.?e-18*I)*z + 1
^^^^^
Why? an imaginary part?
----------------------

Now if I use the radical_expression method:

sage: l=[QQbar(pp.coefficients()[i].radical_expression())*x^i for i in
range(0,pp.degree()+1) if pp.coefficients()[i].imag()==0]
sage: -0.4022786138875136?*x^2 + 1

which is ok, (but... a bit slow).

Why ?

Yours,
t.
tdumont.vcf

Nils Bruin

unread,
Nov 7, 2016, 11:28:12 AM11/7/16
to sage-support
On Monday, November 7, 2016 at 5:59:14 AM UTC-8, tdumont wrote:

okay. Now let us construct a polynomial from l:
sage: print sum([l[i]*z^i for i in range(0,len(l))])
(-0.4022786138875136? + 0.?e-18*I)*z + 1
                          ^^^^^
       Why? an imaginary part?
       ----------------------
There are polynomials for which this doesn't happen. It would be better to include enough information to reproduce your observations.

The representation you see is a float approximation to the algebraic number. You can approximate real numbers quite well with complex numbers with non-zero imaginary part.

sage: P.<x>=QQbar[]
sage: u=[u for u in (x^3+x+1).roots(multiplicities=false) if u.imag()==0][0]
sage: u
-0.6823278038280193?
sage: v=(u*QQbar.zeta(8))^4; v
-0.2167565719512513? + 0.?e-18*I
sage: v.imag() == 0
True
sage: v._exact_value()
a^4 where a^12 + 2*a^8 + 5*a^4 + 1 = 0 and a in 0.4824786170789168? - 0.4824786170789168?*I
sage: v.simplify()
sage: v
-0.2167565719512513?
sage: v._exact_value()
a^2 - a where a^3 + a - 1 = 0 and a in 0.6823278038280193?
sage: v.minpoly()
x^3 + 2*x^2 + 5*x + 1
sage: v.minpoly().roots(QQbar)
[(-0.2167565719512513?, 1),
 (-0.891621714024375? - 1.954093392512700?*I, 1),
 (-0.891621714024375? + 1.954093392512700?*I, 1)]

(I have seen cases where "simplify" didn't do the trick, but recomputing the algebraic number by taking the roots of the minimal polynomial did)

Vincent Delecroix

unread,
Nov 8, 2016, 2:43:55 AM11/8/16
to sage-support
Concerning representation of algebraic numbers, it is printed as an
exact rational if and only if it is stored as an exact rational. It
will be if the method exactify has been called on the underlying
representation of the number. Here is a simple example that shows the
difference

sage: a = QQbar(2).sqrt() + QQbar(3).sqrt()
sage: b = a**2 - 2*QQbar(6).sqrt()

sage: b
5.000000000000000?
sage: type(b._descr) # b is an formal sum
<class 'sage.rings.qqbar.ANBinaryExpr'>

sage: b == 5 # calls exactify
True
sage: b # now prints as 5...
5
sage: type(b._descr) # because it *is* an exact rational
<class 'sage.rings.qqbar.ANRational'>


Vincent
> --
> You received this message because you are subscribed to the Google Groups
> "sage-support" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sage-support...@googlegroups.com.
> To post to this group, send email to sage-s...@googlegroups.com.
> Visit this group at https://groups.google.com/group/sage-support.
> For more options, visit https://groups.google.com/d/optout.

Thierry Dumont

unread,
Nov 8, 2016, 4:17:57 AM11/8/16
to sage-s...@googlegroups.com
Le 08/11/2016 à 08:43, Vincent Delecroix a écrit :
> Concerning representation of algebraic numbers, it is printed as an
> exact rational if and only if it is stored as an exact rational. It
> will be if the method exactify has been called on the underlying
> representation of the number. Here is a simple example that shows the
> difference
>

Ha, yes...

But I am not sure to understand.

sage: y=QQbar(cos(pi/18))
sage: y.radical_expression()
1/4*(4*(1/128*I*sqrt(3) + 1/128)^(1/3) + 1)/(1/128*I*sqrt(3) + 1/128)^(1/6)

ok! good!

sage: y
0.9848077530122081? + 0.?e-18*I

ok.
sage: y.imag()
0.?e-18
sage: y.imag() == 0
True
I accept this as 0 is "in" 0.?e-18

Now:

sage: y.exactify()
sage: y
0.9848077530122081? + 0.?e-18*I

raaahhh ! grrr !

t.
tdumont.vcf

Vincent Delecroix

unread,
Nov 8, 2016, 5:05:28 AM11/8/16
to sage-support
This is *not* a rational!! We might want to special case the
representation of real numbers of QQbar. I opened
https://trac.sagemath.org/ticket/21838 for that purpose.

Vincent

Thierry Dumont

unread,
Nov 8, 2016, 5:28:30 AM11/8/16
to sage-s...@googlegroups.com
sure...

>We might want to special case the
> representation of real numbers of QQbar. I opened
> https://trac.sagemath.org/ticket/21838 for that purpose.
>
> Vincent
>
In my code, all values are real in QQbar. So at least temporary, I wrote

realify=lambda x: x.real()

which is not as beautiful as could be dreamed, but it works.
Thanks!

t.d.

tdumont.vcf

Vincent Delecroix

unread,
Nov 8, 2016, 5:50:41 AM11/8/16
to sage-support
In that case you should use AA ( = real algebraic field) instead of QQbar
{{{
sage: AA(cos(pi/18))
0.9848077530122081?
}}}
Reply all
Reply to author
Forward
0 new messages