From a627ce685f4ac30ba9660f66b24fae246209c5d1 Mon Sep 17 00:00:00 2001 From: Matthias Koeppe Date: Thu, 28 Jul 2022 19:04:22 -0700 Subject: [PATCH 1/4] src/sage/rings/qqbar.py (AlgebraicNumber_base._maxima_init_): New --- src/sage/rings/qqbar.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/sage/rings/qqbar.py b/src/sage/rings/qqbar.py index 7db32272b26..3c0830c23c7 100644 --- a/src/sage/rings/qqbar.py +++ b/src/sage/rings/qqbar.py @@ -4710,6 +4710,35 @@ def radical_expression(self): roots = candidates interval_field = interval_field.to_prec(interval_field.prec() * 2) + def _maxima_init_(self, I=None): + r""" + EXAMPLES:: + + sage: maxima(QQbar(sqrt(5/2))) + sqrt(5)/sqrt(2) + sage: maxima(AA(-sqrt(5))) + -sqrt(5) + sage: QQbar(sqrt(-2))._maxima_init_() + Traceback (most recent call last): + ... + NotImplementedError: conversion implemented only for square roots of nonnegative rationals + """ + try: + return self._rational_()._maxima_init_(I) + except ValueError: + pass + try: + square_QQ = (self ** 2)._rational_() + except ValueError: + raise NotImplementedError('conversion implemented only for square roots of nonnegative rationals') + else: + if square_QQ < 0: + raise NotImplementedError('conversion implemented only for square roots of nonnegative rationals') + if self >= 0: + return 'sqrt(' + square_QQ._maxima_init_() + ')' + else: + return '-sqrt(' + square_QQ._maxima_init_() + ')' + class AlgebraicNumber(AlgebraicNumber_base): r""" From 6204a76d0ec2cbc3cd350cd82f77b2b3ec8088c0 Mon Sep 17 00:00:00 2001 From: Matthias Koeppe Date: Thu, 28 Jul 2022 21:22:39 -0700 Subject: [PATCH 2/4] src/sage/rings/qqbar.py (AlgebraicNumber_base._maxima_init_): Fixup --- src/sage/rings/qqbar.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sage/rings/qqbar.py b/src/sage/rings/qqbar.py index 3c0830c23c7..eadc0689385 100644 --- a/src/sage/rings/qqbar.py +++ b/src/sage/rings/qqbar.py @@ -4724,7 +4724,7 @@ def _maxima_init_(self, I=None): NotImplementedError: conversion implemented only for square roots of nonnegative rationals """ try: - return self._rational_()._maxima_init_(I) + return self._rational_()._maxima_init_() except ValueError: pass try: From 3e560cf5b30377da9d3122427f81f9b0ef93c5c4 Mon Sep 17 00:00:00 2001 From: Matthias Koeppe Date: Thu, 28 Jul 2022 21:25:00 -0700 Subject: [PATCH 3/4] src/sage/rings/qqbar.py: Add doctest --- src/sage/rings/qqbar.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/sage/rings/qqbar.py b/src/sage/rings/qqbar.py index eadc0689385..7da3e3fa415 100644 --- a/src/sage/rings/qqbar.py +++ b/src/sage/rings/qqbar.py @@ -4714,6 +4714,8 @@ def _maxima_init_(self, I=None): r""" EXAMPLES:: + sage: maxima(AA(7)) + 7 sage: maxima(QQbar(sqrt(5/2))) sqrt(5)/sqrt(2) sage: maxima(AA(-sqrt(5))) From 58f4cd1c63b7d7a8aee04de5d8c53a7fac27d061 Mon Sep 17 00:00:00 2001 From: Matthias Koeppe Date: Thu, 28 Jul 2022 22:55:42 -0700 Subject: [PATCH 4/4] src/sage/rings/qqbar.py (AlgebraicNumber_base._maxima_init_): Generalize using radical_expression --- src/sage/rings/qqbar.py | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/src/sage/rings/qqbar.py b/src/sage/rings/qqbar.py index 7da3e3fa415..9e865493bf9 100644 --- a/src/sage/rings/qqbar.py +++ b/src/sage/rings/qqbar.py @@ -4717,29 +4717,26 @@ def _maxima_init_(self, I=None): sage: maxima(AA(7)) 7 sage: maxima(QQbar(sqrt(5/2))) - sqrt(5)/sqrt(2) + sqrt(10)/2 sage: maxima(AA(-sqrt(5))) -sqrt(5) - sage: QQbar(sqrt(-2))._maxima_init_() + sage: maxima(QQbar(sqrt(-2))) + sqrt(2)*%i + sage: maxima(AA(2+sqrt(5))) + sqrt(5)+2 + sage: maxima(QQ[x](x^7 - x - 1).roots(AA, False)[0]) Traceback (most recent call last): ... - NotImplementedError: conversion implemented only for square roots of nonnegative rationals + NotImplementedError: cannot find radical expression """ try: return self._rational_()._maxima_init_() except ValueError: pass - try: - square_QQ = (self ** 2)._rational_() - except ValueError: - raise NotImplementedError('conversion implemented only for square roots of nonnegative rationals') - else: - if square_QQ < 0: - raise NotImplementedError('conversion implemented only for square roots of nonnegative rationals') - if self >= 0: - return 'sqrt(' + square_QQ._maxima_init_() + ')' - else: - return '-sqrt(' + square_QQ._maxima_init_() + ')' + rad = self.radical_expression() + if isinstance(rad.parent(), sage.rings.abc.SymbolicRing): + return rad._maxima_init_() + raise NotImplementedError('cannot find radical expression') class AlgebraicNumber(AlgebraicNumber_base):