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

Commit

Permalink
Simplify NumberFieldElement._symbolic_
Browse files Browse the repository at this point in the history
  • Loading branch information
jdemeyer authored and gagern committed Mar 25, 2015
1 parent ee3823a commit 52512cc
Showing 1 changed file with 20 additions and 45 deletions.
65 changes: 20 additions & 45 deletions src/sage/rings/number_field/number_field_element.pyx
Expand Up @@ -1727,7 +1727,7 @@ cdef class NumberFieldElement(FieldElement):
sage: 2^a
Traceback (most recent call last):
...
TypeError: An embedding into RR or CC must be specified.
TypeError: an embedding into RR or CC must be specified
"""
if (isinstance(base, NumberFieldElement) and
(isinstance(exp, Integer) or type(exp) is int or exp in ZZ)):
Expand Down Expand Up @@ -2149,7 +2149,7 @@ cdef class NumberFieldElement(FieldElement):
sage: SR(a) # indirect doctest
Traceback (most recent call last):
...
TypeError: An embedding into RR or CC must be specified.
TypeError: an embedding into RR or CC must be specified
Now a more complicated example::
Expand Down Expand Up @@ -2194,50 +2194,25 @@ cdef class NumberFieldElement(FieldElement):
sage: SR(a)
(1/2*sqrt(5) + 1/2)^(1/3)
"""
if self.__symbolic is None:

K = self._parent.fraction_field()

gen = K.gen()
if not self is gen:
try:
# share the hard work...
gen_image = gen._symbolic_(SR)
self.__symbolic = self.polynomial()(gen_image)
return self.__symbolic
except TypeError:
pass # we may still be able to do this particular element...

embedding = K.specified_complex_embedding()
if embedding is None:
raise TypeError, "An embedding into RR or CC must be specified."

if isinstance(K, number_field.NumberField_cyclotomic):
# solution by radicals may be difficult, but we have a closed form
from sage.all import exp, I, pi, ComplexField, RR
CC = ComplexField(53)
two_pi_i = 2 * pi * I
k = ( K._n()*CC(K.gen()).log() / CC(two_pi_i) ).real().round() # n ln z / (2 pi i)
gen_image = exp(k*two_pi_i/K._n())
if self is gen:
self.__symbolic = gen_image
else:
self.__symbolic = self.polynomial()(gen_image)
else:
from sage.rings.qqbar import AA, QQbar
from sage.rings.real_lazy import LazyField
if self.parent().is_totally_real():
af = AA
else:
af = QQbar
gen = embedding.gen_image()
if isinstance(gen.parent(), LazyField):
gen = gen.eval(af)
else:
gen = af(gen)
return SR(gen.radical_expression())
K = self._parent.fraction_field()

embedding = K.specified_complex_embedding()
if embedding is None:
raise TypeError("an embedding into RR or CC must be specified")

return self.__symbolic
if isinstance(K, number_field.NumberField_cyclotomic):
# solution by radicals may be difficult, but we have a closed form
from sage.all import exp, I, pi, ComplexField, RR
CC = ComplexField(53)
two_pi_i = 2 * pi * I
k = ( K._n()*CC(K.gen()).log() / CC(two_pi_i) ).real().round() # n ln z / (2 pi i)
gen_image = exp(k*two_pi_i/K._n())
return self.polynomial()(gen_image)
else:
from sage.rings.qqbar import QQbar
gen = embedding.gen_image()
element = self.polynomial()(gen.eval(QQbar))
return SR(element.radical_expression())

def galois_conjugates(self, K):
r"""
Expand Down

0 comments on commit 52512cc

Please sign in to comment.