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

Commit

Permalink
Add sage.rings.abc.AlgebraicField etc., deprecate is_AlgebraicField
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthias Koeppe committed Oct 16, 2021
1 parent d60b10d commit a990a14
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 46 deletions.
5 changes: 2 additions & 3 deletions src/sage/modular/dirichlet.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@
from sage.categories.map import Map
from sage.rings.rational_field import is_RationalField
import sage.rings.abc
from sage.rings.qqbar import is_AlgebraicField
from sage.rings.ring import is_Ring

from sage.misc.functional import round
Expand Down Expand Up @@ -1347,7 +1346,7 @@ def gauss_sum(self, a=1):
m = G.modulus()
if isinstance(K, sage.rings.abc.ComplexField):
return self.gauss_sum_numerical(a=a)
elif is_AlgebraicField(K):
elif isinstance(K, sage.rings.abc.AlgebraicField):
L = K
zeta = L.zeta(m)
elif number_field.is_CyclotomicField(K) or is_RationalField(K):
Expand Down Expand Up @@ -1427,7 +1426,7 @@ def gauss_sum_numerical(self, prec=53, a=1):
def phi(t):
return t
CC = K
elif is_AlgebraicField(K):
elif isinstance(K, sage.rings.abc.AlgebraicField):
from sage.rings.complex_mpfr import ComplexField
CC = ComplexField(prec)
phi = CC.coerce_map_from(K)
Expand Down
24 changes: 24 additions & 0 deletions src/sage/rings/abc.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,30 @@
Abstract base classes for rings
"""

class AlgebraicField_common(Field):
r"""
Abstract base class for :class:`~sage.rings.qqbar.AlgebraicField_common`.
"""

pass


class AlgebraicField(AlgebraicField_common):
r"""
Abstract base class for :class:`~sage.rings.qqbar.AlgebraicField`.
"""

pass


class AlgebraicRealField(AlgebraicField_common):
r"""
Abstract base class for :class:`~sage.rings.qqbar.AlgebraicRealField`.
"""

pass


cdef class RealField(Field):
r"""
Abstract base class for :class:`~sage.rings.real_mpfr.RealField_class`.
Expand Down
30 changes: 8 additions & 22 deletions src/sage/rings/polynomial/polynomial_element.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -171,31 +171,17 @@ from .polynomial_compiled cimport CompiledPolynomialFunction

from sage.rings.polynomial.polydict cimport ETuple

cdef object is_AlgebraicRealField
cdef object is_AlgebraicField
cdef object is_AlgebraicField_common
cdef object NumberField_quadratic
cdef object is_ComplexIntervalField

cdef void late_import():
# A hack to avoid circular imports.
global is_AlgebraicRealField
global is_AlgebraicField
global is_AlgebraicField_common
global NumberField_quadratic
global is_ComplexIntervalField

if is_AlgebraicRealField is not None:
if NumberField_quadratic is not None:
return

import sage.rings.qqbar
is_AlgebraicRealField = sage.rings.qqbar.is_AlgebraicRealField
is_AlgebraicField = sage.rings.qqbar.is_AlgebraicField
is_AlgebraicField_common = sage.rings.qqbar.is_AlgebraicField_common
import sage.rings.number_field.number_field
NumberField_quadratic = sage.rings.number_field.number_field.NumberField_quadratic
import sage.rings.complex_interval_field
is_ComplexIntervalField = sage.rings.complex_interval_field.is_ComplexIntervalField


cdef class Polynomial(CommutativeAlgebraElement):
Expand Down Expand Up @@ -7964,16 +7950,16 @@ cdef class Polynomial(CommutativeAlgebraElement):
else:
return [rt.rhs() for rt in rts]

if L != K or is_AlgebraicField_common(L):
if L != K or isinstance(L, sage.rings.abc.AlgebraicField_common):
# So far, the only "special" implementations are for real
# and complex root isolation and for p-adic factorization
if (is_IntegerRing(K) or is_RationalField(K)
or is_AlgebraicRealField(K)) and \
(is_AlgebraicRealField(L) or isinstance(L, sage.rings.abc.RealIntervalField)):
or isinstance(K, sage.rings.abc.AlgebraicRealField)) and \
isinstance(L, (sage.rings.abc.AlgebraicRealField, sage.rings.abc.RealIntervalField)):

from sage.rings.polynomial.real_roots import real_roots

if is_AlgebraicRealField(L):
if isinstance(L, sage.rings.abc.AlgebraicRealField):
rts = real_roots(self, retval='algebraic_real')
else:
diam = ~(ZZ(1) << L.prec())
Expand All @@ -7998,14 +7984,14 @@ cdef class Polynomial(CommutativeAlgebraElement):
return [rt for (rt, mult) in rts]

if (is_IntegerRing(K) or is_RationalField(K)
or is_AlgebraicField_common(K) or input_gaussian) and \
(isinstance(L, sage.rings.abc.ComplexIntervalField) or is_AlgebraicField_common(L)):
or isinstance(K, sage.rings.abc.AlgebraicField_common) or input_gaussian) and \
isinstance(L, (sage.rings.abc.ComplexIntervalField, sage.rings.abc.AlgebraicField_common)):

from sage.rings.polynomial.complex_roots import complex_roots

if isinstance(L, sage.rings.abc.ComplexIntervalField):
rts = complex_roots(self, min_prec=L.prec())
elif is_AlgebraicField(L):
elif isinstance(L, sage.rings.abc.AlgebraicField):
rts = complex_roots(self, retval='algebraic')
else:
rts = complex_roots(self, retval='algebraic_real')
Expand Down
36 changes: 32 additions & 4 deletions src/sage/rings/qqbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,7 @@
import operator

import sage.rings.ring
import sage.rings.abc
import sage.rings.number_field.number_field_base
from sage.misc.fast_methods import Singleton
from sage.misc.cachefunc import cached_method
Expand Down Expand Up @@ -583,7 +584,7 @@
CC = ComplexField()
CIF = ComplexIntervalField()

class AlgebraicField_common(sage.rings.ring.Field):
class AlgebraicField_common(sage.rings.abc.AlgebraicField_common):
r"""
Common base class for the classes :class:`~AlgebraicRealField` and
:class:`~AlgebraicField`.
Expand Down Expand Up @@ -1013,7 +1014,7 @@ def NF_elem_map(e):
return Factorization(factorization, unit = f.lc() / trial.lc())


class AlgebraicRealField(Singleton, AlgebraicField_common):
class AlgebraicRealField(Singleton, AlgebraicField_common, sage.rings.abc.AlgebraicRealField):
r"""
The field of algebraic reals.
Expand Down Expand Up @@ -1449,20 +1450,29 @@ def is_AlgebraicRealField(F):
r"""
Check whether ``F`` is an :class:`~AlgebraicRealField` instance. For internal use.
This function is deprecated. Use :func:`isinstance` with
:class:`~sage.rings.abc.AlgebraicRealField` instead.
EXAMPLES::
sage: from sage.rings.qqbar import is_AlgebraicRealField
sage: [is_AlgebraicRealField(x) for x in [AA, QQbar, None, 0, "spam"]]
doctest:warning...
DeprecationWarning: is_AlgebraicRealField is deprecated;
use isinstance(..., sage.rings.abc.AlgebraicRealField instead
See https://trac.sagemath.org/32660 for details.
[True, False, False, False, False]
"""
from sage.misc.superseded import deprecation
deprecation(32660, 'is_AlgebraicRealField is deprecated; use isinstance(..., sage.rings.abc.AlgebraicRealField instead')
return isinstance(F, AlgebraicRealField)


# Create the globally unique AlgebraicRealField object.
AA = AlgebraicRealField()


class AlgebraicField(Singleton, AlgebraicField_common):
class AlgebraicField(Singleton, AlgebraicField_common, sage.rings.abc.AlgebraicField):
"""
The field of all algebraic complex numbers.
"""
Expand Down Expand Up @@ -1993,12 +2003,21 @@ def is_AlgebraicField(F):
r"""
Check whether ``F`` is an :class:`~AlgebraicField` instance.
This function is deprecated. Use :func:`isinstance` with
:class:`~sage.rings.abc.AlgebraicField` instead.
EXAMPLES::
sage: from sage.rings.qqbar import is_AlgebraicField
sage: [is_AlgebraicField(x) for x in [AA, QQbar, None, 0, "spam"]]
doctest:warning...
DeprecationWarning: is_AlgebraicField is deprecated;
use isinstance(..., sage.rings.abc.AlgebraicField instead
See https://trac.sagemath.org/32660 for details.
[False, True, False, False, False]
"""
from sage.misc.superseded import deprecation
deprecation(32660, 'is_AlgebraicField is deprecated; use isinstance(..., sage.rings.abc.AlgebraicField instead')
return isinstance(F, AlgebraicField)


Expand All @@ -2010,12 +2029,21 @@ def is_AlgebraicField_common(F):
r"""
Check whether ``F`` is an :class:`~AlgebraicField_common` instance.
This function is deprecated. Use :func:`isinstance` with
:class:`~sage.rings.abc.AlgebraicField_common` instead.
EXAMPLES::
sage: from sage.rings.qqbar import is_AlgebraicField_common
sage: [is_AlgebraicField_common(x) for x in [AA, QQbar, None, 0, "spam"]]
doctest:warning...
DeprecationWarning: is_AlgebraicField_common is deprecated;
use isinstance(..., sage.rings.abc.AlgebraicField_common) instead
See https://trac.sagemath.org/32610 for details.
[True, True, False, False, False]
"""
from sage.misc.superseded import deprecation
deprecation(32610, 'is_AlgebraicField_common is deprecated; use isinstance(..., sage.rings.abc.AlgebraicField_common) instead')
return isinstance(F, AlgebraicField_common)


Expand Down Expand Up @@ -6551,7 +6579,7 @@ def __init__(self, poly):
poly = QQy(poly)
complex = False
elif isinstance(B, AlgebraicField_common):
complex = is_AlgebraicField(poly.base_ring())
complex = isinstance(poly.base_ring(), AlgebraicField)
else:
try:
poly = poly.change_ring(AA)
Expand Down
6 changes: 3 additions & 3 deletions src/sage/rings/qqbar_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ def wrapper(*args, **kwds):
from sage.rings.polynomial.multi_polynomial import MPolynomial
from sage.rings.polynomial.multi_polynomial_sequence import PolynomialSequence, is_PolynomialSequence
from sage.rings.ideal import Ideal, Ideal_generic
from sage.rings.qqbar import is_AlgebraicField_common, number_field_elements_from_algebraics
from sage.rings.qqbar import AlgebraicField_common, number_field_elements_from_algebraics

if not any(isinstance(a, (Polynomial, MPolynomial, Ideal_generic))
and is_AlgebraicField_common(a.base_ring())
and isinstance(a.base_ring(), AlgebraicField_common)
or is_PolynomialSequence(a)
and is_AlgebraicField_common(a.ring().base_ring()) for a in args):
and isinstance(a.ring().base_ring(), AlgebraicField_common) for a in args):
return func(*args, **kwds)

polynomials = []
Expand Down
26 changes: 12 additions & 14 deletions src/sage/symbolic/expression.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1524,8 +1524,9 @@ cdef class Expression(CommutativeRingElement):
ValueError: cannot convert sqrt(-3) to int
"""
from sage.functions.all import floor, ceil
from sage.rings.real_mpfi import RIF
try:
rif_self = sage.rings.all.RIF(self)
rif_self = RIF(self)
except TypeError:
raise ValueError("cannot convert %s to int" % self)
if rif_self > 0 or (rif_self.contains_zero() and self > 0):
Expand Down Expand Up @@ -3548,27 +3549,22 @@ cdef class Expression(CommutativeRingElement):
if not self.is_relational():
raise ValueError("self must be a relation")
cdef operators op = relational_operator(self._gobj)
from sage.rings.real_mpfi import is_RealIntervalField
from sage.rings.complex_interval_field import is_ComplexIntervalField
from sage.rings.all import RIF, CIF
from sage.rings.qqbar import is_AlgebraicField, is_AlgebraicRealField, AA, QQbar
if domain is None:
is_interval = True
if self.lhs().is_algebraic() and self.rhs().is_algebraic():
if op == equal or op == not_equal:
domain = QQbar
from sage.rings.qqbar import QQbar as domain
else:
domain = AA
from sage.rings.qqbar import AA as domain
else:
if op == equal or op == not_equal:
domain = CIF
from sage.rings.qqbar import CIF as domain
else:
domain = RIF
from sage.rings.real_mpfi import RIF as domain
else:
is_interval = (isinstance(domain, (sage.rings.abc.RealIntervalField,
sage.rings.abc.ComplexIntervalField))
or is_AlgebraicField(domain)
or is_AlgebraicRealField(domain))
is_interval = isinstance(domain, (sage.rings.abc.RealIntervalField,
sage.rings.abc.ComplexIntervalField,
sage.rings.abc.AlgebraicField_common))
zero = domain(0)
diff = self.lhs() - self.rhs()
vars = diff.variables()
Expand Down Expand Up @@ -3621,6 +3617,7 @@ cdef class Expression(CommutativeRingElement):
except (TypeError, ValueError, ArithmeticError, AttributeError) as ex:
errors += 1
if k == errors > 3 and isinstance(domain, sage.rings.abc.ComplexIntervalField):
from sage.rings.real_mpfi import RIF
domain = RIF.to_prec(domain.prec())
# we are plugging in random values above, don't be surprised
# if something goes wrong...
Expand Down Expand Up @@ -6644,8 +6641,9 @@ cdef class Expression(CommutativeRingElement):
except (TypeError, AttributeError):
pass
from sage.functions.all import floor, ceil
from sage.rings.real_mpfi import RIF
try:
rif_self = sage.rings.all.RIF(self)
rif_self = RIF(self)
except TypeError:
raise ValueError("could not convert %s to a real number" % self)
half = 1 / sage.rings.integer.Integer(2)
Expand Down

0 comments on commit a990a14

Please sign in to comment.