Skip to content
This repository was archived by the owner on Feb 1, 2023. It is now read-only.

Commit 67d812a

Browse files
author
Release Manager
committed
Trac #30783: Sage thinks that I^(2/3) = -1
Revert #25218. Rationale: #25218 makes `a^(p/q)` where a is a number field element attempt to return a solution to `a^p = x^q` in the number field. This makes powering inconsistent with coercions, since number fields often (and, in the case of quadratic fields, automatically) come with a complex embedding and complex numbers of various kinds use the principal branch of the power function. Among other confusing behaviors, we have {{{ sage: I^(2/3) I^(2/3) sage: QQbar(I^(2/3)) 0.500000000000000? + 0.866025403784439?*I sage: QQbar(I)^(2/3) 0.500000000000000? + 0.866025403784439?*I }}} but {{{ sage: I.pyobject()^(2/3) -1 }}} and {{{ sage: QQi.<i> = QuadraticField(-1) sage: i^(2/3) -1 }}} but {{{ sage: QQbar(i)^(2/3) 0.500000000000000? + 0.866025403784439?*I }}} as well as {{{ sage: CC((i^(1/6))^2) 0.866025403784439 + 0.500000000000000*I sage: CC((i^(1/3))) -1.00000000000000*I sage: i^(1/3)*CC(i)^(1/3) 0.500000000000000 - 0.866025403784439*I }}} URL: https://trac.sagemath.org/30783 Reported by: mmezzarobba Ticket author(s): Marc Mezzarobba Reviewer(s): Sébastien Labbé
2 parents 257a9b5 + 0807787 commit 67d812a

File tree

1 file changed

+23
-36
lines changed

1 file changed

+23
-36
lines changed

src/sage/rings/number_field/number_field_element.pyx

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2295,12 +2295,8 @@ cdef class NumberFieldElement(FieldElement):
22952295
sage: (1+sqrt2)^-1
22962296
sqrt2 - 1
22972297
2298-
If the exponent is not integral, attempt this operation in the NumberField:
2299-
2300-
sage: K(2)^(1/2)
2301-
sqrt2
2302-
2303-
If this fails, perform this operation in the symbolic ring::
2298+
If the exponent is not integral, perform this operation in
2299+
the symbolic ring::
23042300
23052301
sage: sqrt2^(1/5)
23062302
2^(1/10)
@@ -2333,38 +2329,29 @@ cdef class NumberFieldElement(FieldElement):
23332329
if (isinstance(base, NumberFieldElement) and
23342330
(isinstance(exp, Integer) or type(exp) is int or exp in ZZ)):
23352331
return generic_power(base, exp)
2336-
2337-
if (isinstance(base, NumberFieldElement) and exp in QQ):
2338-
qqexp = QQ(exp)
2339-
n = qqexp.numerator()
2340-
d = qqexp.denominator()
2332+
else:
2333+
cbase, cexp = canonical_coercion(base, exp)
2334+
if not isinstance(cbase, NumberFieldElement):
2335+
return cbase ** cexp
2336+
# Return a symbolic expression.
2337+
# We use the hold=True keyword argument to prevent the
2338+
# symbolics library from trying to simplify this expression
2339+
# again. This would lead to infinite loops otherwise.
2340+
from sage.symbolic.ring import SR
23412341
try:
2342-
return base.nth_root(d)**n
2343-
except ValueError:
2342+
res = QQ(base)**QQ(exp)
2343+
except TypeError:
23442344
pass
2345-
2346-
cbase, cexp = canonical_coercion(base, exp)
2347-
if not isinstance(cbase, NumberFieldElement):
2348-
return cbase ** cexp
2349-
# Return a symbolic expression.
2350-
# We use the hold=True keyword argument to prevent the
2351-
# symbolics library from trying to simplify this expression
2352-
# again. This would lead to infinite loops otherwise.
2353-
from sage.symbolic.ring import SR
2354-
try:
2355-
res = QQ(base)**QQ(exp)
2356-
except TypeError:
2357-
pass
2358-
else:
2359-
if res.parent() is not SR:
2360-
return parent(cbase)(res)
2361-
return res
2362-
sbase = SR(base)
2363-
if sbase.operator() is operator.pow:
2364-
nbase, pexp = sbase.operands()
2365-
return nbase.power(pexp * exp, hold=True)
2366-
else:
2367-
return sbase.power(exp, hold=True)
2345+
else:
2346+
if res.parent() is not SR:
2347+
return parent(cbase)(res)
2348+
return res
2349+
sbase = SR(base)
2350+
if sbase.operator() is operator.pow:
2351+
nbase, pexp = sbase.operands()
2352+
return nbase.power(pexp * exp, hold=True)
2353+
else:
2354+
return sbase.power(exp, hold=True)
23682355

23692356
cdef void _reduce_c_(self):
23702357
"""

0 commit comments

Comments
 (0)