Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Give deprecation warning when raising negative AlgebraicReal to fractional odd power #38362

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/sage/features/sagemath.py
Original file line number Diff line number Diff line change
Expand Up @@ -879,8 +879,8 @@ class sage__rings__number_field(JoinFeature):
should be marked likewise::

sage: # needs sage.rings.number_field
sage: AA(-1)^(1/3)
-1
sage: AA(2)^(1/2)
1.414213562373095?
sage: QQbar(-1)^(1/3)
0.500000000000000? + 0.866025403784439?*I

Expand Down
32 changes: 26 additions & 6 deletions src/sage/rings/qqbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@
sage: QQbar((-8)^(1/3))
1.000000000000000? + 1.732050807568878?*I
sage: AA((-8)^(1/3))
doctest:warning...
DeprecationWarning: Taking the root of an algebraic real number will yield the principal root in the future.
See https://github.com/sagemath/sage/issues/38362 for details.
-2
sage: QQbar((-4)^(1/4))
1 + 1*I
Expand All @@ -134,10 +137,13 @@
sage: QQbar(sqrt(2) + QQbar(sqrt(3))) # needs sage.symbolic
3.146264369941973?

Note the different behavior in taking roots: for ``AA`` we prefer real
roots if they exist, but for ``QQbar`` we take the principal root::
Currently for ``AA`` we prefer real roots if they exist, but for ``QQbar`` we take the principal root.
However, this behavior will change in the future::

sage: AA(-1)^(1/3)
doctest:warning...
DeprecationWarning: Taking the root of an algebraic real number will yield the principal root in the future.
See https://github.com/sagemath/sage/issues/38362 for details.
-1
sage: QQbar(-1)^(1/3)
0.500000000000000? + 0.866025403784439?*I
Expand Down Expand Up @@ -4360,10 +4366,13 @@ def nth_root(self, n, all=False):
sage: AA(-2).nth_root(5, all=True) == QQbar(-2).nth_root(5, all=True) # long time
True
"""
n = ZZ(n)
if not all:
return self ** ~ZZ(n)
if self.parent() is AA and self.sign() < 0 and n % 2 != 0 and abs(n) > 1:
return -((-self) ** ~n)
return self ** ~n
else:
root = QQbar(self) ** ~ZZ(n)
root = QQbar(self) ** ~n
zlist = [root]
zeta = QQbar.zeta(n)
for k in range(1, n):
Expand Down Expand Up @@ -6373,6 +6382,9 @@ class AlgebraicNumberPowQQAction(Action):
TESTS::

sage: AA(-8)^(1/3)
doctest:warning...
DeprecationWarning: Taking the root of an algebraic real number will yield the principal root in the future.
See https://github.com/sagemath/sage/issues/38362 for details.
-2
sage: AA(-8)^(2/3)
4
Expand All @@ -6398,6 +6410,9 @@ def __init__(self, G, S):
sage: act = AlgebraicNumberPowQQAction(QQ, AA); act
Right Rational Powering by Rational Field on Algebraic Real Field
sage: act(AA(-2), 1/3)
doctest:warning...
DeprecationWarning: Taking the root of an algebraic real number will yield the principal root in the future.
See https://github.com/sagemath/sage/issues/38362 for details.
-1.259921049894873?

::
Expand Down Expand Up @@ -6429,8 +6444,13 @@ def _act_(self, e, x):

# Parent of the result
S = self.codomain()
if S is AA and d % 2 == 0 and x.sign() < 0:
S = QQbar
if S is AA and x.sign() < 0:
if d % 2 == 0:
S = QQbar
elif d != 1:
from sage.misc.superseded import deprecation
deprecation(38362, "Taking the root of an algebraic real number "
"will yield the principal root in the future.")

# First, check for exact roots.
if isinstance(x._descr, ANRational):
Expand Down
10 changes: 10 additions & 0 deletions src/sage/symbolic/expression_conversion_algebraic.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from sage.symbolic.expression_conversions import Converter
from sage.symbolic.operators import add_vararg, mul_vararg
from sage.symbolic.ring import SR
from sage.rings.qqbar import AlgebraicRealField


#############
Expand Down Expand Up @@ -74,6 +75,15 @@ def arithmetic(self, ex, operator):
sage: a.arithmetic(f, f.operator())
1.414213562373095?

Note that converting an expression where an odd root is taken will take the real root
if the target is ``AA``, but this behavior will change in the future::

sage: AA((-1)^(2/3))
doctest:warning...
DeprecationWarning: Taking the root of an algebraic real number will yield the principal root in the future.
See https://github.com/sagemath/sage/issues/38362 for details.
1

TESTS::

sage: f = pi^6
Expand Down
Loading