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

Commit 2598cf2

Browse files
committed
implement a factory
1 parent 1ce658d commit 2598cf2

File tree

2 files changed

+84
-79
lines changed

2 files changed

+84
-79
lines changed

src/sage/rings/polynomial/skew_polynomial_ring.py

+10-45
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ def _lagrange_polynomial(R, eval_pts, values):
217217
# Generic implementation of skew polynomial rings
218218
#################################################
219219

220-
class SkewPolynomialRing_general(Algebra, UniqueRepresentation):
220+
class SkewPolynomialRing_general(Algebra):
221221
r"""
222222
A general implementation of univariate skew polynomialring over a commutative ring.
223223
@@ -283,38 +283,7 @@ class SkewPolynomialRing_general(Algebra, UniqueRepresentation):
283283
:meth:`sage.rings.polynomial.skew_polynomial_ring_constructor.SkewPolynomialRing`
284284
:mod:`sage.rings.polynomial.skew_polynomial_element`
285285
"""
286-
@staticmethod
287-
def __classcall__(cls, base_ring, twist_map=None, name=None, sparse=False,
288-
element_class=None):
289-
r"""
290-
Set the default values for ``name``, ``sparse`` and ``element_class``.
291-
292-
EXAMPLES::
293-
294-
sage: R.<t> = ZZ[]
295-
sage: sigma = R.hom([t+1])
296-
sage: S.<x> = SkewPolynomialRing(R, sigma)
297-
sage: S.__class__(R, sigma, x)
298-
Skew Polynomial Ring in x over Univariate Polynomial Ring in t over Integer Ring
299-
twisted by t |--> t + 1
300-
"""
301-
if not element_class:
302-
if sparse:
303-
raise NotImplementedError("sparse skew polynomials are not implemented")
304-
else:
305-
from sage.rings.polynomial import skew_polynomial_element
306-
element_class = skew_polynomial_element.SkewPolynomial_generic_dense
307-
if twist_map is None:
308-
twist_map = IdentityMorphism(base_ring)
309-
else:
310-
if not isinstance(twist_map, Morphism):
311-
raise TypeError("given map is not a ring homomorphism")
312-
if twist_map.domain() != base_ring or twist_map.codomain() != base_ring:
313-
raise TypeError("given map is not an automorphism of %s" % base_ring)
314-
return super(SkewPolynomialRing_general,cls).__classcall__(cls,
315-
base_ring, twist_map, name, sparse, element_class)
316-
317-
def __init__(self, base_ring, twist_map, name, sparse, element_class):
286+
def __init__(self, base_ring, twist_map, name, sparse, element_class=None):
318287
r"""
319288
Initialize ``self``.
320289
@@ -356,7 +325,10 @@ def __init__(self, base_ring, twist_map, name, sparse, element_class):
356325
sage: TestSuite(T).run(skip=["_test_pickling", "_test_elements"])
357326
"""
358327
self.__is_sparse = sparse
359-
self._polynomial_class = element_class
328+
if element_class is None:
329+
from sage.rings.polynomial.skew_polynomial_element import SkewPolynomial_generic_dense
330+
element_class = SkewPolynomial_generic_dense
331+
self.Element = self._polynomial_class = element_class
360332
self._map = twist_map
361333
self._maps = {0: IdentityMorphism(base_ring), 1: self._map}
362334
Algebra.__init__(self, base_ring, names=name, normalize=True,
@@ -1380,17 +1352,10 @@ class SkewPolynomialRing_finite_order(SkewPolynomialRing_general):
13801352
:class:`sage.rings.polynomial.skew_polynomial_ring.SkewPolynomialRing_general`
13811353
:mod:`sage.rings.polynomial.skew_polynomial_finite_order`
13821354
"""
1383-
@staticmethod
1384-
def __classcall__(cls, base_ring, map, name=None, sparse=False, element_class=None):
1385-
if not element_class:
1386-
if sparse:
1387-
raise NotImplementedError("sparse skew polynomials are not implemented")
1388-
else:
1389-
from sage.rings.polynomial import skew_polynomial_finite_order
1390-
element_class = skew_polynomial_finite_order.SkewPolynomial_finite_order_dense
1391-
return super(SkewPolynomialRing_general,cls).__classcall__(cls, base_ring, map, name, sparse, element_class)
1392-
1393-
def __init__(self, base_ring, twist_map, name, sparse, element_class):
1355+
def __init__(self, base_ring, twist_map, name, sparse, element_class=None):
1356+
if element_class is None:
1357+
from sage.rings.polynomial.skew_polynomial_finite_order import SkewPolynomial_finite_order_dense
1358+
element_class = SkewPolynomial_finite_order_dense
13941359
SkewPolynomialRing_general.__init__(self, base_ring, twist_map, name, sparse, element_class)
13951360
self._order = twist_map.order()
13961361

src/sage/rings/polynomial/skew_polynomial_ring_constructor.py

+74-34
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
# ***************************************************************************
2727

2828
from __future__ import print_function, absolute_import, division
29+
from sage.structure.factory import UniqueFactory
2930
from sage import categories
3031
from sage.structure.category_object import normalize_names
3132
from sage.categories.morphism import Morphism, IdentityMorphism
@@ -34,8 +35,7 @@
3435
from sage.categories.commutative_rings import CommutativeRings
3536

3637

37-
38-
def SkewPolynomialRing(base_ring, base_ring_automorphism=None, names=None, sparse=False):
38+
class SkewPolynomialRingFactory(UniqueFactory):
3939
r"""
4040
Return the globally unique skew polynomial ring with the given properties
4141
and variable names.
@@ -191,36 +191,76 @@ def SkewPolynomialRing(base_ring, base_ring_automorphism=None, names=None, spars
191191
- Multivariate Skew Polynomial Ring
192192
- Add derivations.
193193
"""
194-
if base_ring not in categories.rings.Rings().Commutative():
195-
raise TypeError("base_ring must be a commutative ring")
196-
if base_ring not in CommutativeRings():
197-
raise TypeError('base_ring must be a commutative ring')
198-
if base_ring_automorphism is None:
199-
base_ring_automorphism = IdentityMorphism(base_ring)
200-
else:
201-
if (not isinstance(base_ring_automorphism,Morphism)
202-
or base_ring_automorphism.domain() != base_ring
203-
or base_ring_automorphism.codomain() != base_ring):
204-
raise TypeError("base_ring_automorphism must be a ring automorphism of base_ring (=%s)" % base_ring)
205-
if sparse:
206-
raise NotImplementedError("sparse skew polynomial rings are not implemented")
207-
if names is None:
208-
raise TypeError("you must specify the name of the variable")
209-
try:
210-
names = normalize_names(1, names)[0]
211-
except IndexError:
212-
raise NotImplementedError("multivariate skew polynomials rings not supported")
213-
214-
import sage.rings.polynomial.skew_polynomial_ring as spr
215-
216-
# We check whether sigma has finite order
217-
if base_ring in Fields():
194+
def create_key(self, base_ring, base_ring_automorphism=None, names=None, sparse=False):
195+
r"""
196+
Create a key from the input parameters
197+
198+
INPUT:
199+
200+
- ``base_ring`` -- a ring
201+
202+
- ``base_ring_automorphism`` -- a homomorphism of rings
203+
204+
- ``names`` -- a string; names of the indeterminates
205+
206+
- ``sparse`` - a boolean
207+
208+
EXAMPLES::
209+
210+
sage: k.<a> = GF(11^2)
211+
sage: Frob = k.frobenius_endomorphism()
212+
sage: SkewPolynomialRing.create_key(k, Frob, 'x')
213+
(Finite Field in a of size 11^2,
214+
Frobenius endomorphism a |--> a^11 on Finite Field in a of size 11^2,
215+
'x',
216+
False)
217+
"""
218+
if base_ring not in categories.rings.Rings().Commutative():
219+
raise TypeError("base_ring must be a commutative ring")
220+
if base_ring not in CommutativeRings():
221+
raise TypeError('base_ring must be a commutative ring')
222+
if base_ring_automorphism is None:
223+
base_ring_automorphism = IdentityMorphism(base_ring)
224+
else:
225+
if (not isinstance(base_ring_automorphism,Morphism)
226+
or base_ring_automorphism.domain() != base_ring
227+
or base_ring_automorphism.codomain() != base_ring):
228+
raise TypeError("base_ring_automorphism must be a ring automorphism of base_ring (=%s)" % base_ring)
229+
if sparse:
230+
raise NotImplementedError("sparse skew polynomial rings are not implemented")
231+
if names is None:
232+
raise TypeError("you must specify the name of the variable")
218233
try:
219-
order = base_ring_automorphism.order()
220-
if order is not Infinity:
221-
return spr.SkewPolynomialRing_finite_order(base_ring, base_ring_automorphism, names, sparse)
222-
except AttributeError:
223-
pass
224-
225-
# Generic implementation
226-
return spr.SkewPolynomialRing_general(base_ring, base_ring_automorphism, names, sparse)
234+
names = normalize_names(1, names)[0]
235+
except IndexError:
236+
raise NotImplementedError("multivariate skew polynomials rings not supported")
237+
return (base_ring, base_ring_automorphism, names, sparse)
238+
239+
def create_object(self, version, key):
240+
"""
241+
Create an object using the given key
242+
243+
TESTS::
244+
245+
sage: k.<a> = GF(11^2)
246+
sage: Frob = k.frobenius_endomorphism()
247+
sage: key = SkewPolynomialRing.create_key(k, Frob, 'x')
248+
sage: SkewPolynomialRing.create_object((9,1,9), key)
249+
Skew Polynomial Ring in x over Finite Field in a of size 11^2 twisted by a |--> a^11
250+
"""
251+
import sage.rings.polynomial.skew_polynomial_ring as spr
252+
(base_ring, base_ring_automorphism, names, sparse) = key
253+
254+
# We check if the twisting morphism has finite order
255+
if base_ring in Fields():
256+
try:
257+
order = base_ring_automorphism.order()
258+
if order is not Infinity:
259+
return spr.SkewPolynomialRing_finite_order(base_ring, base_ring_automorphism, names, sparse)
260+
except AttributeError:
261+
pass
262+
263+
# We fallback to generic implementation
264+
return spr.SkewPolynomialRing_general(base_ring, base_ring_automorphism, names, sparse)
265+
266+
SkewPolynomialRing = SkewPolynomialRingFactory("SkewPolynomialRing")

0 commit comments

Comments
 (0)