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

New ABC sage.structure.element.NumberFieldElement, deprecate is_NumberFieldElement #35100

Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
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
10 changes: 7 additions & 3 deletions src/sage/interfaces/maxima_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1164,6 +1164,8 @@ def reduce_load_MaximaLib():
import sage.rings.real_double
import sage.symbolic.expression
import sage.symbolic.integration.integral

from sage.structure.element import NumberFieldElement
from sage.symbolic.operators import FDerivativeOperator, add_vararg, mul_vararg

car=EclObject("car")
Expand Down Expand Up @@ -1523,9 +1525,11 @@ def pyobject_to_max(obj):
"""
if isinstance(obj,sage.rings.rational.Rational):
return EclObject(obj) if (obj.denom().is_one()) else EclObject([[rat], obj.numer(),obj.denom()])
elif isinstance(obj,sage.rings.number_field.number_field_element_quadratic.NumberFieldElement_quadratic) and obj.parent().defining_polynomial().list() == [1,0,1]:
re, im = obj.list()
return EclObject([[mplus], pyobject_to_max(re), [[mtimes], pyobject_to_max(im), max_i]])
elif isinstance(obj, NumberFieldElement):
from sage.rings.number_field.number_field_element_quadratic import NumberFieldElement_quadratic
if isinstance(obj, NumberFieldElement_quadratic) and obj.parent().defining_polynomial().list() == [1,0,1]:
re, im = obj.list()
return EclObject([[mplus], pyobject_to_max(re), [[mtimes], pyobject_to_max(im), max_i]])
return EclObject(obj)

# This goes from SR to EclObject
Expand Down
5 changes: 2 additions & 3 deletions src/sage/rings/finite_rings/residue_field.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,8 @@ from sage.rings.finite_rings.finite_field_ntl_gf2e import FiniteField_ntl_gf2e
from sage.rings.finite_rings.finite_field_prime_modn import FiniteField_prime_modn
from sage.rings.finite_rings.finite_field_pari_ffelt import FiniteField_pari_ffelt
from sage.rings.ideal import is_Ideal
from sage.structure.element cimport Element
from sage.structure.element cimport Element, NumberFieldElement

from sage.rings.number_field.number_field_element import is_NumberFieldElement
from sage.rings.number_field.number_field_ideal import is_NumberFieldIdeal

from sage.modules.free_module_element import FreeModuleElement
Expand Down Expand Up @@ -295,7 +294,7 @@ class ResidueFieldFactory(UniqueFactory):
if not is_Ideal(p):
if isinstance(p, (int, Integer, Rational)):
p = ZZ.ideal(p)
elif is_NumberFieldElement(p):
elif isinstance(p, NumberFieldElement):
if p.parent().is_field():
p = p.parent().ring_of_integers().ideal(p)
else:
Expand Down
2 changes: 1 addition & 1 deletion src/sage/rings/integer_ring.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ from sage.categories.basic import EuclideanDomains
from sage.categories.infinite_enumerated_sets import InfiniteEnumeratedSets
from sage.structure.coerce cimport is_numpy_type
from sage.structure.element cimport parent
from sage.structure.element import NumberFieldElement
mkoeppe marked this conversation as resolved.
Show resolved Hide resolved
from sage.structure.parent_gens import ParentWithGens
from sage.structure.parent cimport Parent
from sage.structure.richcmp cimport rich_to_bool
Expand Down Expand Up @@ -414,7 +415,6 @@ cdef class IntegerRing_class(PrincipalIdealDomain):
if x in self:
return self

from sage.rings.number_field.number_field_element import NumberFieldElement
if isinstance(x, NumberFieldElement):
K, from_K = parent(x).subfield(x)
return K.order(K.gen())
Expand Down
Empty file.
4 changes: 3 additions & 1 deletion src/sage/rings/number_field/number_field_element.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ from sage.libs.gmp.types cimport mpz_t
from sage.rings.integer cimport Integer
from sage.rings.polynomial.polynomial_element cimport Polynomial
from sage.structure.element cimport FieldElement, RingElement, ModuleElement
from sage.structure.element cimport NumberFieldElement as NumberFieldElement_base
from sage.structure.parent cimport Parent
from sage.structure.parent_base cimport ParentWithBase
from sage.libs.ntl.types cimport ZZ_c, ZZX_c
from sage.libs.ntl.ntl_ZZX cimport ntl_ZZX
from sage.libs.ntl.ntl_ZZ cimport ntl_ZZ

cdef class NumberFieldElement(FieldElement):

cdef class NumberFieldElement(NumberFieldElement_base):
cdef ZZX_c __numerator
cdef ZZ_c __denominator
# Pointers to the defining polynomial (with numerator) for the field.
Expand Down
12 changes: 10 additions & 2 deletions src/sage/rings/number_field/number_field_element.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,19 @@ def is_NumberFieldElement(x):

sage: from sage.rings.number_field.number_field_element import is_NumberFieldElement
sage: is_NumberFieldElement(2)
doctest:warning...
DeprecationWarning: is_NumberFieldElement is deprecated;
use isinstance(..., sage.structure.element.NumberFieldElement) instead
See https://trac.sagemath.org/34931 for details.
False
sage: k.<a> = NumberField(x^7 + 17*x + 1)
sage: is_NumberFieldElement(a+1)
True
"""
from sage.misc.superseded import deprecation
deprecation(34931,
'is_NumberFieldElement is deprecated; '
'use isinstance(..., sage.structure.element.NumberFieldElement) instead')
return isinstance(x, NumberFieldElement)


Expand Down Expand Up @@ -190,7 +198,7 @@ def _inverse_mod_generic(elt, I):
return I.small_residue(y)


cdef class NumberFieldElement(FieldElement):
cdef class NumberFieldElement(NumberFieldElement_base):
"""
An element of a number field.

Expand Down Expand Up @@ -3858,7 +3866,7 @@ cdef class NumberFieldElement(FieldElement):
"""
from .number_field_ideal import is_NumberFieldIdeal
if not is_NumberFieldIdeal(P):
if is_NumberFieldElement(P):
if isinstance(P, NumberFieldElement):
P = self.number_field().fractional_ideal(P)
else:
raise TypeError("P must be an ideal")
Expand Down
4 changes: 2 additions & 2 deletions src/sage/rings/universal_cyclotomic_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@

from sage.structure.richcmp import rich_to_bool
from sage.structure.unique_representation import UniqueRepresentation
from sage.structure.element import FieldElement, parent
from sage.structure.element import FieldElement, parent, NumberFieldElement

from sage.structure.coerce import py_scalar_to_element
from sage.categories.morphism import Morphism
from sage.rings.ring import Field
Expand Down Expand Up @@ -1537,7 +1538,6 @@ def _element_constructor_(self, elt):
import sage.rings.abc
P = parent(elt)
if isinstance(P, sage.rings.abc.NumberField_cyclotomic):
from sage.rings.number_field.number_field_element import NumberFieldElement
if isinstance(elt, NumberFieldElement):
from sage.rings.number_field.number_field import CyclotomicField
n = P.gen().multiplicative_order()
Expand Down
6 changes: 4 additions & 2 deletions src/sage/schemes/elliptic_curves/cm.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing

from sage.misc.cachefunc import cached_function
from sage.structure.element import NumberFieldElement


@cached_function
def hilbert_class_polynomial(D, algorithm=None):
Expand Down Expand Up @@ -624,8 +626,6 @@ def is_cm_j_invariant(j, method='new'):
True
"""
# First we check that j is an algebraic number:
from sage.rings.number_field.number_field_element import NumberFieldElement
from sage.rings.number_field.number_field import NumberField
if not isinstance(j, NumberFieldElement) and j not in QQ:
raise NotImplementedError("is_cm_j_invariant() is only implemented for number field elements")

Expand Down Expand Up @@ -672,6 +672,8 @@ def is_cm_j_invariant(j, method='new'):

K = j.parent()
if h < K.absolute_degree():
from sage.rings.number_field.number_field import NumberField

K = NumberField(jpol, 'j')
j = K.gen()

Expand Down
3 changes: 2 additions & 1 deletion src/sage/schemes/elliptic_curves/heegner.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
from sage.rings.real_mpfr import RealField
from sage.quadratic_forms.all import (BinaryQF,
BinaryQF_reduced_representatives)
from sage.structure.element import NumberFieldElement
from sage.structure.sage_object import SageObject
from sage.structure.richcmp import (richcmp_method, richcmp,
richcmp_not_equal, rich_to_bool)
Expand Down Expand Up @@ -2683,7 +2684,7 @@ def __init__(self, N, D, c=ZZ(1), f=None, check=True):
elif isinstance(f, BinaryQF):
# convert from BinaryQF
f = tuple(f)
elif sage.rings.number_field.number_field_element.is_NumberFieldElement(f):
elif isinstance(f, NumberFieldElement):
# tau = number field element
g = f.minpoly()
if g.degree() != 2:
Expand Down
3 changes: 3 additions & 0 deletions src/sage/structure/element.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,9 @@ cdef class EuclideanDomainElement(PrincipalIdealDomainElement):
cdef class FieldElement(CommutativeRingElement):
cpdef _floordiv_(self, other)

cdef class NumberFieldElement(FieldElement):
pass

cdef class AlgebraElement(RingElement):
pass

Expand Down
24 changes: 24 additions & 0 deletions src/sage/structure/element.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ abstract base classes.
PrincipalIdealDomainElement
EuclideanDomainElement
FieldElement
NumberFieldElement
CommutativeAlgebraElement
Expression
AlgebraElement
Expand Down Expand Up @@ -4263,6 +4264,29 @@ cdef class FieldElement(CommutativeRingElement):
other = self.parent()(other)
return bool(self) or other.is_zero()


cdef class NumberFieldElement(FieldElement):
r"""
Abstract base class for :class:`~sage.rings.number_field.number_field_element.NumberFieldElement`

This class is defined for the purpose of :func:`isinstance` tests. It should not be
instantiated.

EXAMPLES::

sage: k.<a> = NumberField(x^3 + x + 1)
sage: isinstance(a, sage.structure.element.NumberFieldElement)
True

By design, there is a unique direct subclass::

sage: len(sage.structure.element.NumberFieldElement.__subclasses__()) <= 1
True
"""

pass


def is_AlgebraElement(x):
"""
Return ``True`` if x is of type AlgebraElement.
Expand Down
17 changes: 10 additions & 7 deletions src/sage/symbolic/expression_conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@

from sage.rings.rational_field import QQ
from sage.symbolic.ring import SR
from sage.structure.element import Expression
from sage.structure.element import Expression, NumberFieldElement
from sage.functions.all import exp
from sage.symbolic.operators import arithmetic_operators, relation_operators, FDerivativeOperator, add_vararg, mul_vararg
from sage.rings.number_field.number_field_element_quadratic import NumberFieldElement_gaussian
from sage.rings.universal_cyclotomic_field import UniversalCyclotomicField


Expand Down Expand Up @@ -441,9 +440,10 @@ def pyobject(self, ex, obj):
sage: ii.pyobject(pi, pi.pyobject())
'Pi'
"""
if (self.interface.name() in ['pari', 'gp'] and
isinstance(obj, NumberFieldElement_gaussian)):
return repr(obj)
if (self.interface.name() in ['pari', 'gp'] and isinstance(obj, NumberFieldElement)):
from sage.rings.number_field.number_field_element_quadratic import NumberFieldElement_gaussian
if isinstance(obj, NumberFieldElement_gaussian):
return repr(obj)
try:
return getattr(obj, self.name_init)()
except AttributeError:
Expand Down Expand Up @@ -1020,10 +1020,13 @@ def pyobject(self, ex, obj):
"""
try:
result = getattr(obj, self.name_init)()
if isinstance(obj, NumberFieldElement_gaussian):
return "((%s)::EXPR COMPLEX INT)" % result
except AttributeError:
result = repr(obj)
else:
if isinstance(obj, NumberFieldElement):
from sage.rings.number_field.number_field_element_quadratic import NumberFieldElement_gaussian
if isinstance(obj, NumberFieldElement_gaussian):
return "((%s)::EXPR COMPLEX INT)" % result
return "((%s)::EXPR INT)" % result

def symbol(self, ex):
Expand Down