Skip to content

Commit

Permalink
using Parent in ring extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
fchapoton committed Oct 29, 2024
1 parent 1b3f398 commit 6562754
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 46 deletions.
4 changes: 3 additions & 1 deletion src/sage/rings/morphism.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2879,8 +2879,10 @@ cdef class FrobeniusEndomorphism_generic(RingHomomorphism):
over Finite Field of size 5
"""
from sage.rings.ring import CommutativeRing
from sage.categories.commutative_rings import CommutativeRings
from sage.categories.homset import Hom
if not isinstance(domain, CommutativeRing):
if not (domain in CommutativeRings() or
isinstance(domain, CommutativeRing)): # TODO: remove this line
raise TypeError("The base ring must be a commutative ring")
self._p = domain.characteristic()
if not self._p.is_prime():
Expand Down
25 changes: 20 additions & 5 deletions src/sage/rings/number_field/order.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,9 @@
# https://www.gnu.org/licenses/
# ****************************************************************************

from sage.categories.integral_domains import IntegralDomains
from sage.misc.cachefunc import cached_method
from sage.rings.ring import IntegralDomain
from sage.structure.parent import Parent
from sage.structure.sequence import Sequence
from sage.rings.integer_ring import ZZ
import sage.rings.abc
Expand Down Expand Up @@ -426,7 +427,7 @@ def EquationOrder(f, names, **kwds):
return K.order(K.gens())


class Order(IntegralDomain, sage.rings.abc.Order):
class Order(Parent, sage.rings.abc.Order):
r"""
An order in a number field.
Expand Down Expand Up @@ -478,8 +479,8 @@ def __init__(self, K):
0.0535229072603327 + 1.20934552493846*I
"""
self._K = K
IntegralDomain.__init__(self, ZZ, names=K.variable_names(),
normalize=False)
Parent.__init__(self, base=ZZ, names=K.variable_names(),
normalize=False, category=IntegralDomains())
self._populate_coercion_lists_(embedding=self.number_field())
if self.absolute_degree() == 2:
self.is_maximal() # cache
Expand Down Expand Up @@ -665,7 +666,7 @@ def krull_dimension(self):
sage: O2.krull_dimension()
1
"""
return ZZ(1)
return ZZ.one()

def integral_closure(self):
r"""
Expand Down Expand Up @@ -733,6 +734,20 @@ def ngens(self):
"""
return self.absolute_degree()

def gens(self) -> tuple:
"""
Return the generators as a tuple.
EXAMPLES::
sage: x = polygen(ZZ, 'x')
sage: K.<a> = NumberField(x^3 + x^2 - 2*x + 8)
sage: O = K.maximal_order()
sage: O.gens()
(1, 1/2*a^2 + 1/2*a, a^2)
"""
return tuple(self.gen(i) for i in range(self.absolute_degree()))

def basis(self): # this must be defined in derived class
r"""
Return a basis over `\ZZ` of this order.
Expand Down
17 changes: 9 additions & 8 deletions src/sage/rings/ring_extension.pxd
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from sage.categories.map cimport Map
from sage.rings.ring cimport CommutativeRing
from sage.structure.parent cimport Parent


cdef class RingExtension_generic(CommutativeRing):
cdef class RingExtension_generic(Parent):
cdef _type
cdef _backend
cdef _defining_morphism
Expand All @@ -15,10 +15,10 @@ cdef class RingExtension_generic(CommutativeRing):
cdef type _fraction_field_type

cpdef is_defined_over(self, base)
cpdef CommutativeRing _check_base(self, CommutativeRing base)
cpdef _degree_over(self, CommutativeRing base)
cpdef _is_finite_over(self, CommutativeRing base)
cpdef _is_free_over(self, CommutativeRing base)
cpdef Parent _check_base(self, Parent base)
cpdef _degree_over(self, Parent base)
cpdef _is_finite_over(self, Parent base)
cpdef _is_free_over(self, Parent base)
cdef Map _defining_morphism_fraction_field(self, bint extend_base)


Expand All @@ -31,10 +31,11 @@ cdef class RingExtensionWithBasis(RingExtension_generic):
cdef _basis_names
cdef _basis_latex_names

cpdef _basis_over(self, CommutativeRing base)
# cpdef _free_module(self, CommutativeRing base, bint map)
cpdef _basis_over(self, Parent base)
# cpdef _free_module(self, Parent base, bint map)


cdef class RingExtensionWithGen(RingExtensionWithBasis):
cdef _gen
cdef _name
cdef public object _latex_names
35 changes: 17 additions & 18 deletions src/sage/rings/ring_extension.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,12 @@ from sage.cpython.getattr import dir_with_other_class
from sage.misc.latex import latex, latex_variable_name

from sage.structure.factory import UniqueFactory
from sage.structure.parent cimport Parent
from sage.structure.element cimport Element
from sage.structure.category_object import normalize_names
from sage.categories.map cimport Map
from sage.categories.commutative_rings import CommutativeRings
from sage.categories.fields import Fields
from sage.rings.ring cimport CommutativeRing
from sage.rings.integer_ring import ZZ
from sage.rings.infinity import Infinity

Expand Down Expand Up @@ -497,7 +497,7 @@ RingExtension = RingExtensionFactory("sage.rings.ring_extension.RingExtension")
# General extensions
####################

cdef class RingExtension_generic(CommutativeRing):
cdef class RingExtension_generic(Parent):
r"""
A generic class for all ring extensions.
Expand Down Expand Up @@ -564,8 +564,8 @@ cdef class RingExtension_generic(CommutativeRing):
...
ValueError: exotic defining morphism between two rings in the tower; consider using another variable name
"""
cdef CommutativeRing base, ring
cdef CommutativeRing b, backend
cdef Parent base, ring
cdef Parent b, backend
cdef Map f

base = defining_morphism.domain()
Expand All @@ -575,7 +575,7 @@ cdef class RingExtension_generic(CommutativeRing):
# but CommutativeRings() seems safer, especially when dealing with
# morphisms which do not need to preserve the base
category = CommutativeRings()
CommutativeRing.__init__(self, ZZ, category=category)
Parent.__init__(self, ZZ, category=category)
self._base = base
self._backend = ring
self._backend_defining_morphism = defining_morphism
Expand Down Expand Up @@ -1197,14 +1197,14 @@ cdef class RingExtension_generic(CommutativeRing):
:meth:`base`, :meth:`bases`, :meth:`absolute_base`
"""
cdef CommutativeRing b
cdef Parent b
b = self
while isinstance(b, RingExtension_generic):
if b is base or (<RingExtension_generic>b)._backend is base: return True
b = (<RingExtension_generic>b)._base
return b is base

cpdef CommutativeRing _check_base(self, CommutativeRing base):
cpdef Parent _check_base(self, Parent base):
r"""
Check if ``base`` is one of the successive bases of this
extension and, if it is, normalize it.
Expand Down Expand Up @@ -1243,7 +1243,7 @@ cdef class RingExtension_generic(CommutativeRing):
sage: L._check_base(None) is L.base() # needs sage.rings.finite_rings
True
"""
cdef CommutativeRing b
cdef Parent b
if base is None:
return self._base
b = self
Expand Down Expand Up @@ -1450,7 +1450,7 @@ cdef class RingExtension_generic(CommutativeRing):
base = self._check_base(base)
return self._degree_over(base)

cpdef _degree_over(self, CommutativeRing base):
cpdef _degree_over(self, Parent base):
r"""
Return the degree of this extension over ``base``.
Expand Down Expand Up @@ -1572,7 +1572,7 @@ cdef class RingExtension_generic(CommutativeRing):
sage: L.is_finite_over() # needs sage.rings.finite_rings
True
"""
cdef CommutativeRing b
cdef Parent b
base = self._check_base(base)
if base is self:
return True
Expand All @@ -1590,7 +1590,7 @@ cdef class RingExtension_generic(CommutativeRing):
b = (<RingExtension_generic?>b)._base
raise NotImplementedError

cpdef _is_finite_over(self, CommutativeRing base):
cpdef _is_finite_over(self, Parent base):
r"""
Return whether or not this extension is finite over ``base``.
Expand Down Expand Up @@ -1635,7 +1635,7 @@ cdef class RingExtension_generic(CommutativeRing):
sage: L.is_free_over() # needs sage.rings.finite_rings
True
"""
cdef CommutativeRing b
cdef Parent b
base = self._check_base(base)
if base is self or base.is_field():
return True
Expand All @@ -1653,7 +1653,7 @@ cdef class RingExtension_generic(CommutativeRing):
b = (<RingExtension_generic?>b)._base
raise NotImplementedError

cpdef _is_free_over(self, CommutativeRing base):
cpdef _is_free_over(self, Parent base):
r"""
Return whether or not this extension is finite over ``base``.
Expand Down Expand Up @@ -2191,7 +2191,7 @@ cdef class RingExtensionWithBasis(RingExtension_generic):
b = b.base_ring()
return base

cpdef _degree_over(self, CommutativeRing base):
cpdef _degree_over(self, Parent base):
r"""
Return the degree of this extension over ``base``.
Expand All @@ -2218,7 +2218,7 @@ cdef class RingExtensionWithBasis(RingExtension_generic):
else:
return len(self._basis) * self._base._degree_over(base)

cpdef _is_finite_over(self, CommutativeRing base):
cpdef _is_finite_over(self, Parent base):
r"""
Return whether or not this extension is finite over ``base``.
Expand All @@ -2237,7 +2237,7 @@ cdef class RingExtensionWithBasis(RingExtension_generic):
return True
return self._base._is_finite_over(base)

cpdef _is_free_over(self, CommutativeRing base):
cpdef _is_free_over(self, Parent base):
r"""
Return whether or not this extension is free over ``base``.
Expand Down Expand Up @@ -2298,7 +2298,7 @@ cdef class RingExtensionWithBasis(RingExtension_generic):
base = self._check_base(base)
return self._basis_over(base)

cpdef _basis_over(self, CommutativeRing base):
cpdef _basis_over(self, Parent base):
r"""
Return a basis of this extension over ``base``.
Expand Down Expand Up @@ -2588,7 +2588,6 @@ cdef class RingExtensionWithGen(RingExtensionWithBasis):
RingExtensionWithBasis.__init__(self, defining_morphism, basis, basis_names, check, **kwargs)
self._gen = self._backend(gen)
self._names = (self._name,)
self._latex_names = (latex_variable_name(self._name),)
self._basis_latex_names = basis_latex_names

def _repr_topring(self, **options):
Expand Down
10 changes: 5 additions & 5 deletions src/sage/rings/ring_extension_element.pxd
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from sage.rings.ring cimport CommutativeRing
from sage.structure.parent cimport Parent
from sage.structure.element cimport Element
from sage.structure.element cimport CommutativeAlgebraElement
from sage.rings.ring_extension cimport RingExtension_generic
Expand All @@ -13,10 +13,10 @@ cdef class RingExtensionFractionFieldElement(RingExtensionElement):
pass

cdef class RingExtensionWithBasisElement(RingExtensionElement):
cdef _vector(self, CommutativeRing base)
cdef _matrix(self, CommutativeRing base)
cdef _trace(self, CommutativeRing base)
cdef _norm(self, CommutativeRing base)
cdef _vector(self, Parent base)
cdef _matrix(self, Parent base)
cdef _trace(self, Parent base)
cdef _norm(self, Parent base)
cpdef minpoly(self, base=*, var=*)


13 changes: 7 additions & 6 deletions src/sage/rings/ring_extension_element.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ from sage.misc.latex import latex

from sage.structure.category_object import normalize_names
from sage.structure.element cimport CommutativeAlgebraElement
from sage.structure.parent cimport Parent
from sage.rings.integer_ring import ZZ
from sage.categories.fields import Fields
from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing
Expand Down Expand Up @@ -1033,7 +1034,7 @@ cdef class RingExtensionWithBasisElement(RingExtensionElement):
base = (<RingExtension_generic>self._parent)._check_base(base)
return self._vector(base)

cdef _vector(self, CommutativeRing base):
cdef _vector(self, Parent base):
r"""
Return the vector of coordinates of this element over ``base``
(in the basis output by the method :meth:`basis_over`).
Expand Down Expand Up @@ -1198,7 +1199,7 @@ cdef class RingExtensionWithBasisElement(RingExtensionElement):
raise ValueError("the extension is not finite free")
return self._matrix(base)

cdef _matrix(self, CommutativeRing base):
cdef _matrix(self, Parent base):
r"""
Return the matrix of the multiplication by this element (in
the basis output by :meth:`basis_over`).
Expand Down Expand Up @@ -1286,7 +1287,7 @@ cdef class RingExtensionWithBasisElement(RingExtensionElement):
raise ValueError("the extension is not finite free")
return self._trace(base)

cdef _trace(self, CommutativeRing base):
cdef _trace(self, Parent base):
r"""
Return the trace of this element over ``base``.
Expand Down Expand Up @@ -1314,7 +1315,7 @@ cdef class RingExtensionWithBasisElement(RingExtensionElement):
....: assert((x+y).trace(base) == x.trace(base) + y.trace(base))
"""
cdef RingExtensionWithBasis parent = self._parent
cdef CommutativeRing b
cdef Parent b
if base is parent:
return self
b = parent._base
Expand Down Expand Up @@ -1379,7 +1380,7 @@ cdef class RingExtensionWithBasisElement(RingExtensionElement):
raise ValueError("the extension is not finite free")
return self._norm(base)

cdef _norm(self, CommutativeRing base):
cdef _norm(self, Parent base):
r"""
Return the norm of this element over ``base``.
Expand Down Expand Up @@ -1407,7 +1408,7 @@ cdef class RingExtensionWithBasisElement(RingExtensionElement):
....: assert((x*y).norm(base) == x.norm(base) * y.norm(base))
"""
cdef RingExtensionWithBasis parent = self._parent
cdef CommutativeRing b
cdef Parent b
if base is parent:
return self
b = parent._base
Expand Down
6 changes: 3 additions & 3 deletions src/sage/rings/ring_extension_morphism.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ from sage.structure.richcmp import op_EQ, op_NE

from sage.structure.element cimport Element
from sage.categories.map import Map
from sage.rings.ring cimport CommutativeRing
from sage.structure.parent cimport Parent
from sage.rings.morphism cimport RingMap
from sage.rings.ring_extension cimport RingExtension_generic, RingExtensionWithBasis
from sage.rings.ring_extension_element cimport RingExtensionElement
Expand Down Expand Up @@ -57,7 +57,7 @@ cdef are_equal_morphisms(f, g):
sage: H(f^2) == H(g^2) # indirect doctest
True
"""
cdef CommutativeRing b
cdef Parent b
cdef tuple gens
if f is None and g is None:
return True
Expand Down Expand Up @@ -821,7 +821,7 @@ cdef class MapRelativeRingToFreeModule(Map):
From: Field in z6 with defining polynomial x^2 + (10*z3^2 + z3 + 6)*x + z3 over its base
To: Vector space of dimension 2 over Finite Field in z3 of size 11^3
"""
cdef CommutativeRing L, base
cdef Parent L, base

self._degree = (<RingExtensionWithBasis>E)._degree_over(K)
self._basis = [ (<RingExtensionElement>x)._backend for x in E.basis_over(K) ]
Expand Down

0 comments on commit 6562754

Please sign in to comment.