Dear Emil,

There's a bit of a tug of war going on with comparison for number field 
elements. On the one hand, it should always be possible to compare two 
elements of a given number field, and the resulting ordering should be a 
total order and one that's deterministic and quick to compute. On the other 
hand, it would be nice if comparison "did the obvious thing" for fields 
with real embeddings. These two conditions are hard to satisfy 
simultaneously (just count the number of tickets on trac on this issue).

This doesn't really answer your question, of course. If you really do want 
to sort according to a given real embedding and you don't want to be 
trapped by numerical precision issues, then the right tool for the job is 
the AA (algebraic real) class. Sadly it doesn't seem to be possible to 
create a number field with a specified embedding into AA (or into its 
complex cousin QQbar); this is a definite bug!

A kludgy workaround is as follows:

sage: K.<a> = NumberField(x^3 - x - 1)
sage: emb = K.embeddings(AA)[0]
sage: emb
Ring morphism:
  From: Number Field in a with defining polynomial x^3 - x - 1
  To:   Algebraic Real Field
  Defn: a |--> 1.324717957244746?
sage: L = [K.random_element() for _ in xrange(50)]
sage: L.sort(key = emb)
sage: L
[-11*a^2 - 3*a - 3, -3*a^2 - 11*a + 1, -a^2 - 37/3*a, -a^2 - 7*a, 1/3*a^2 - 
3*a - 5, -4*a^2 - 1, -6, -3*a^2 - 1/2*a - 2/661, -3*a^2, -a^2 - 1/2*a - 1, 
-16/5, -a^2 - 2/5, -1/5*a - 5/3, -2/5*a - 1, -4/29*a - 1, -2/3*a^2 + 
1/57*a, -1/2*a^2 - 1/4, -1, -a + 1/2, -1/2*a - 1/9, -1/34*a^2 - 1/3*a - 
1/4, -2*a^2 + 3, -3*a^2 + 2/3*a + 4, -a + 1, a^2 - 1/303*a - 2, -1/8*a, 
-2/13*a^2 + 1/4, -1/4*a^2 + 1/3*a + 2/19, a^2 - a, a - 2/3, -1/9*a + 1, 
1/2*a^2 - 7/10*a + 1, a^2 + a - 1, a + 1, -24/11*a^2 + 4*a + 1, 1/6*a^2 - 
2/5*a + 3, 2*a^2 + a - 2, a^2 + 3/5*a + 1, 1/2*a^2 - 1/2*a + 4, a^2 + 2*a - 
4/23, 4*a^2 - 1, 1/3*a^2 + 9/2*a - 1/3, -a^2 + 25/3, 5*a^2 - 2*a + 1, 4*a^2 
+ a, -3*a^2 - a + 15, 1/2*a^2 + 6*a + 11/4, -1/2*a^2 + 13*a + 2, 43/2*a^2 + 
a - 1/11, -1/4*a^2 + a + 226]
sage: [RR(x) for x in L]
[-26.2778082004479, -18.8365305284323, -18.0930658055986, 
-11.0279033669599, -8.38919464965201, -8.01951066498677, -6.00000000000000, 
-5.93001769597062, -5.26463299874008, -3.41723664486907, -3.20000000000000, 
-2.15487766624669, -1.93161025811562, -1.52988718289790, -1.18271971824065, 
-1.14667777824789, -1.12743883312335, -1.00000000000000, 
-0.824717957244746, -0.773470089733484, -0.743186701422171, 
-0.509755332493385, -0.381487693910247, -0.324717957244746, 
-0.249494340212861, -0.165589744655593, -0.0199811794225681, 
0.108116393747979, 0.430159709001946, 0.658051290578079, 0.852809115861695, 
0.950136263052024, 2.07959562349144, 2.32471795724475, 2.47004782989529, 
2.76259242814322, 2.83447328973813, 3.54970844059354, 4.21507985450097, 
4.23040053725792, 6.01951066498677, 6.21285669635025, 6.57845566708664, 
7.12495241674397, 8.34422862223152, 8.41064904401518, 11.5757465765918, 
18.3438946110583, 38.9636786906395, 226.885998540683]

On Tuesday, 20 March 2012 15:50:00 UTC, Emil wrote:
>
> I have an array of number field elements:
>
> sage: [x for x in bounds]
> [4*x - 1, 10/3*x - 4/3, 4*x - 1, 2*x - 1, 4*x - 1, 4*x - 1, 0]
>
> They belong to a field that has an embedding, and I would like to sort
> them according to their real values.
>
> I can do it using float conversions:
>
> sage: sorted(float(x) for x in bounds)
> [-0.26794919243112281, -0.11324865405187112, 0.0, 0.46410161513775439,
> 0.46410161513775439, 0.46410161513775439, 0.46410161513775439]
>
> Or:
>
> sage: sorted(bounds, key = lambda x: float(x))
> [2*x - 1, 10/3*x - 4/3, 0, 4*x - 1, 4*x - 1, 4*x - 1, 4*x - 1]
>
> But, this is a bit messy, and could be imprecise.
>
> It would be good if I could just do:
>
> sage: sorted(bounds)
> [4*x - 1, 10/3*x - 4/3, 4*x - 1, 2*x - 1, 4*x - 1, 4*x - 1, 0]
>
> But this doesn't appear to work! Is there a way to do this?  Thanks,
>
> Emil
>
>

-- 
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/sage-support
URL: http://www.sagemath.org

Reply via email to