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

Commit

Permalink
Trac #25218: NumberField attempts to evaluate fractional powers
Browse files Browse the repository at this point in the history
It still falls back to the symbolic ring, but only if root extraction
within the NumberField fails.
  • Loading branch information
Brent Baccala committed Apr 20, 2018
1 parent a9d274b commit 75a46a0
Showing 1 changed file with 38 additions and 25 deletions.
63 changes: 38 additions & 25 deletions src/sage/rings/number_field/number_field_element.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2179,8 +2179,12 @@ cdef class NumberFieldElement(FieldElement):
sage: (1+sqrt2)^-1
sqrt2 - 1
If the exponent is not integral, perform this operation in
the symbolic ring::
If the exponent is not integral, attempt this operation in the NumberField:
sage: K(2)^(1/2)
sqrt2
If this fails, perform this operation in the symbolic ring::
sage: sqrt2^(1/5)
2^(1/10)
Expand Down Expand Up @@ -2212,30 +2216,39 @@ cdef class NumberFieldElement(FieldElement):
"""
if (isinstance(base, NumberFieldElement) and
(isinstance(exp, Integer) or type(exp) is int or exp in ZZ)):
return generic_power(base, exp)
else:
cbase, cexp = canonical_coercion(base, exp)
if not isinstance(cbase, NumberFieldElement):
return cbase ** cexp
# Return a symbolic expression.
# We use the hold=True keyword argument to prevent the
# symbolics library from trying to simplify this expression
# again. This would lead to infinite loops otherwise.
from sage.symbolic.ring import SR
return generic_power_c(base, exp)

if (isinstance(base, NumberFieldElement) and exp in QQ):
qqexp=QQ(exp)
n = qqexp.numerator()
d = qqexp.denominator()
try:
res = QQ(base)**QQ(exp)
except TypeError:
pass
else:
if res.parent() is not SR:
return parent(cbase)(res)
return res
sbase = SR(base)
if sbase.operator() is operator.pow:
nbase, pexp = sbase.operands()
return nbase.power(pexp * exp, hold=True)
else:
return sbase.power(exp, hold=True)
return base.nth_root(d)**n
except ValueError:
pass

cbase, cexp = canonical_coercion(base, exp)
if not isinstance(cbase, NumberFieldElement):
return cbase ** cexp
# Return a symbolic expression.
# We use the hold=True keyword argument to prevent the
# symbolics library from trying to simplify this expression
# again. This would lead to infinite loops otherwise.
from sage.symbolic.ring import SR
try:
res = QQ(base)**QQ(exp)
except TypeError:
pass
else:
if res.parent() is not SR:
return parent(cbase)(res)
return res
sbase = SR(base)
if sbase.operator() is operator.pow:
nbase, pexp = sbase.operands()
return nbase.power(pexp * exp, hold=True)
else:
return sbase.power(exp, hold=True)

cdef void _reduce_c_(self):
"""
Expand Down

0 comments on commit 75a46a0

Please sign in to comment.