Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
#18836: simplified code after review
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnCremona committed Jul 12, 2015
1 parent 450209d commit accaa9f
Showing 1 changed file with 25 additions and 14 deletions.
39 changes: 25 additions & 14 deletions src/sage/rings/number_field/number_field.py
Expand Up @@ -123,7 +123,6 @@
import maps
import structure
import number_field_morphisms
from itertools import count, izip


def is_NumberFieldHomsetCodomain(codomain):
Expand Down Expand Up @@ -8377,7 +8376,7 @@ def refine_embedding(self, e, prec=None):
- ``e`` - an embedding of a number field into either
RR or CC (with some precision)
``RR`` or ``CC`` (with some precision)
- ``prec`` - (default None) the desired precision; if None,
current precision is doubled; if Infinity, the equivalent
Expand Down Expand Up @@ -8430,6 +8429,8 @@ def refine_embedding(self, e, prec=None):
From: Number Field in a with defining polynomial x^3 - 2
To: Algebraic Field
Defn: a |--> -0.6299605249474365? - 1.091123635971722?*I
sage: e(a)^3 == 2
True
sage: ComplexField(200)(e(a))
-0.62996052494743658238360530363911417528512573235075399004099 - 1.0911236359717214035600726141898088813258733387403009407036*I
sage: e(a)^3
Expand Down Expand Up @@ -8471,10 +8472,12 @@ def refine_embedding(self, e, prec=None):
To: Algebraic Real Field
Defn: a |--> 0.6823278038280193?
"""
if not self is e.domain():
if self is not e.domain():
raise ValueError("wrong domain in refine_embedding")
from sage.rings.all import (AA, QQbar, RealField, ComplexField)

RC = e.codomain()
if RC in (sage.rings.qqbar.AA, sage.rings.qqbar.QQbar):
if RC in (AA, QQbar):
return e
if RC in (RLF, CLF):
prec_old = e.gen_image().approx().prec()
Expand All @@ -8488,24 +8491,32 @@ def refine_embedding(self, e, prec=None):
elif prec_old >= prec:
return e

# We first compute all the embeddings at the new precision:
# Determine the new codomain:

if sage.rings.real_mpfr.is_RealField(RC) or RC in (RDF, RLF):
if prec == Infinity:
elist = self.embeddings(sage.rings.qqbar.AA)
K = AA
else:
elist = self.real_embeddings(prec)
K = RealField(prec)
else:
if prec == Infinity:
elist = self.embeddings(sage.rings.qqbar.QQbar)
K = QQbar
else:
elist = self.complex_embeddings(prec)
K = ComplexField(prec)

# Compute all the roots of the defining polynomial in the new
# codomain: as there is no function to find one root close to
# a given real or complex number, we find all of them and pick
# the closest.

rr = self.defining_polynomial().roots(K, multiplicities=False)

# Now we determine which is an extension of the old one; this
# relies on the fact that coercing a high-precision root into a
# field with lower precision will equal the lower-precision root!
diffs = [(RC(ee(self.gen()))-old_root).abs() for ee in elist]
return elist[min(izip(diffs,count()))[1]]
# Now we determine which of these is closest to the old root
# and use that one to define the new embedding:

diffs = [(RC(r)-old_root).abs() for r in rr]
new_root = rr[diffs.index(min(diffs))]
return self.hom([new_root], check=False)

class NumberField_cyclotomic(NumberField_absolute):
"""
Expand Down

0 comments on commit accaa9f

Please sign in to comment.