# Comparing numbers in an algebraic field

I get a very strange results from comparison of number in an algebraic field, see the example:

version()
X = polygen(ZZ)
f = X^3 - 2*X^2 - 2*X - 2
Qb.<b> = QQ.extension(f, embedding=3)

b.N() # b is positive
0 < b # gives False; why?
0 < 1 # gives True; fine
Qb(0) < Qb(1) # gives False; WTF?


sage: version()
'Sage Version 6.1.1, Release Date: 2014-02-04'
sage: X = polygen(ZZ)
sage: f = X^3 - 2*X^2 - 2*X - 2
sage: Qb.<b> = QQ.extension(f, embedding=3)
sage:
sage: b.N() # b is positive
2.91963956583942
sage: 0 < b # gives False; why?
False
sage: 0 < 1 # gives True; fine
True
sage: Qb(0) < Qb(1) # gives False; WTF?
False
sage:


Can I do something to obtain the correct results of comparison, or is using the flawed .N() the only option?

edit retag close merge delete

Sort by » oldest newest most voted

What bothers me is that, though Qb is defined with an embedding in $\mathbb{C}$, there is no canonical coercion from Qb to QQbar, as if the information of the embedding was lost (ideally, there should be a difference between "number field" and "embedded number field", even two different parents):

sage: cm = sage.structure.element.get_coercion_model()
sage: cm.common_parent(Qb,QQbar)
TypeError: no common canonical parent for objects with parents: 'Number Field in b with defining polynomial x^3 - 2*x^2 - 2*x - 2' and 'Algebraic Field'


Not even a conversion:

sage: QQbar(b)
TypeError: Illegal initializer for algebraic number
sage: AA(b)
TypeError: Illegal initializer for algebraic number


That said, the following workaround to consider b as a real algebraic number could do the job:

sage: Qb.embeddings(AA)
[
Ring morphism:
From: Number Field in b with defining polynomial x^3 - 2*x^2 - 2*x - 2
To:   Algebraic Real Field
Defn: b |--> 2.919639565839419?
]
sage: bb = Qb.embeddings(AA)[0](b) ; bb
2.919639565839419?
sage: 0 < bb
True

more

Thanks Thierry. Actually what is even stranger is that floor(x-y)>=0 works well in place of x>=y. The problem with AA is that there seem to be some memory leaks :-/

( 2014-11-13 07:14:22 -0500 )edit
1

Hi Tom, could you please provide more info on the memory leaks ? This will help in improving Sage source code.

( 2014-11-13 11:55:45 -0500 )edit

Comparison in number fields is not satisfactory in Sage.

The current implementation (except in some special cases) seems to be that

• > always returns True,
• < always returns False.

That is the case with b defined as in your example:

sage: b > 0
True
sage: 0 > b
True
sage: b < 0
False
sage: 0 < b
False


Comparison works well in quadratic number fields since ticket #13213.

The method is_real_positive() from ticket #8347 works in some number fields (but unfortunately not in the field Qb in your question).

more