Ask Your Question

Comparing numbers in an algebraic field

asked 2014-11-12 13:25:40 -0500

updated 2015-07-31 11:15:50 -0500

FrédéricC gravatar image

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

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: b.N() # b is positive
sage: 0 < b # gives False; why?
sage: 0 < 1 # gives True; fine
sage: Qb(0) < Qb(1) # gives False; WTF?

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

edit retag flag offensive close merge delete

2 answers

Sort by » oldest newest most voted

answered 2014-11-12 15:44:28 -0500

tmonteil gravatar image

updated 2014-11-12 15:49:23 -0500

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
sage: 0 < bb
edit flag offensive delete link 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 :-/

tohecz gravatar imagetohecz ( 2014-11-13 07:14:22 -0500 )edit

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

tmonteil gravatar imagetmonteil ( 2014-11-13 11:55:45 -0500 )edit

answered 2014-11-18 05:25:20 -0500

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
sage: 0 > b
sage: b < 0
sage: 0 < b

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).

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2014-11-12 13:25:40 -0500

Seen: 166 times

Last updated: Nov 18 '14