Skip to content

Commit b85f026

Browse files
committed
Accept both 'implementation' and 'impl' parameters for GF()
1 parent 5c8d9e9 commit b85f026

File tree

1 file changed

+50
-27
lines changed

1 file changed

+50
-27
lines changed

src/sage/rings/finite_rings/finite_field_constructor.py

Lines changed: 50 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@
174174
# ****************************************************************************
175175

176176
from collections import defaultdict
177+
from typing import Optional
177178
from sage.structure.category_object import normalize_names, certify_names
178179
from sage.rings.polynomial.polynomial_element import Polynomial
179180
from sage.rings.integer import Integer
@@ -227,7 +228,7 @@ class FiniteFieldFactory(UniqueFactory):
227228
``modulus="primitive"`` to get a primitive polynomial. You
228229
may not specify a modulus if you do not specify a variable name.
229230
230-
- ``impl`` -- (optional) a string specifying the implementation of
231+
- ``implementation`` -- (optional) a string specifying the implementation of
231232
the finite field. Possible values are:
232233
233234
- ``'modn'`` -- ring of integers modulo `p` (only for prime fields)
@@ -241,9 +242,12 @@ class FiniteFieldFactory(UniqueFactory):
241242
for extension fields)
242243
243244
- ``elem_cache`` -- (default: order < 500) cache all elements to
244-
avoid creation time; ignored unless ``impl='givaro'``
245+
avoid creation time; ignored unless ``implementation='givaro'``
245246
246-
- ``repr`` -- (default: ``'poly'``) ignored unless ``impl='givaro'``;
247+
- ``impl`` -- (deprecated, use ``implementation`` instead) for backwards
248+
compatibility, accepts the same values as ``implementation``
249+
250+
- ``repr`` -- (default: ``'poly'``) ignored unless ``implementation='givaro'``;
247251
controls the way elements are printed to the user:
248252
249253
- 'log': repr is
@@ -465,9 +469,9 @@ class FiniteFieldFactory(UniqueFactory):
465469
466470
Check that :issue:`16934` has been fixed::
467471
468-
sage: k1.<a> = GF(17^14, impl='pari')
472+
sage: k1.<a> = GF(17^14, implementation='pari')
469473
sage: _ = a/2
470-
sage: k2.<a> = GF(17^14, impl='pari')
474+
sage: k2.<a> = GF(17^14, implementation='pari')
471475
sage: k1 is k2
472476
True
473477
@@ -511,8 +515,8 @@ def __init__(self, *args, **kwds):
511515
super().__init__(*args, **kwds)
512516

513517
def create_key_and_extra_args(self, order, name=None, modulus=None, names=None,
514-
impl=None, proof=None,
515-
check_prime=True, check_irreducible=True,
518+
implementation: Optional[str] = None, proof=None,
519+
check_prime: bool = True, check_irreducible: bool = True,
516520
prefix=None, repr=None, elem_cache=None,
517521
**kwds):
518522
"""
@@ -537,21 +541,21 @@ def create_key_and_extra_args(self, order, name=None, modulus=None, names=None,
537541
Moreover, ``repr`` and ``elem_cache`` are ignored when not
538542
using givaro::
539543
540-
sage: GF.create_key_and_extra_args(16, 'a', impl='ntl', repr='poly') # needs sage.libs.ntl
544+
sage: GF.create_key_and_extra_args(16, 'a', implementation='ntl', repr='poly') # needs sage.libs.ntl
541545
((16, ('a',), x^4 + x + 1, 'ntl', 2, 4, True, None, None, None, True, True), {})
542-
sage: GF.create_key_and_extra_args(16, 'a', impl='ntl', elem_cache=False) # needs sage.libs.ntl
546+
sage: GF.create_key_and_extra_args(16, 'a', implementation='ntl', elem_cache=False) # needs sage.libs.ntl
543547
((16, ('a',), x^4 + x + 1, 'ntl', 2, 4, True, None, None, None, True, True), {})
544-
sage: GF(16, impl='ntl') is GF(16, impl='ntl', repr='foo') # needs sage.libs.ntl
548+
sage: GF(16, implementation='ntl') is GF(16, implementation='ntl', repr='foo') # needs sage.libs.ntl
545549
True
546550
547551
We handle extra arguments for the givaro finite field and
548552
create unique objects for their defaults::
549553
550-
sage: GF(25, impl='givaro') is GF(25, impl='givaro', repr='poly') # needs sage.libs.linbox
554+
sage: GF(25, implementation='givaro') is GF(25, implementation='givaro', repr='poly') # needs sage.libs.linbox
551555
True
552-
sage: GF(25, impl='givaro') is GF(25, impl='givaro', elem_cache=True) # needs sage.libs.linbox
556+
sage: GF(25, implementation='givaro') is GF(25, implementation='givaro', elem_cache=True) # needs sage.libs.linbox
553557
True
554-
sage: GF(625, impl='givaro') is GF(625, impl='givaro', elem_cache=False) # needs sage.libs.linbox
558+
sage: GF(625, implementation='givaro') is GF(625, implementation='givaro', elem_cache=False) # needs sage.libs.linbox
555559
True
556560
557561
We explicitly take ``structure``, ``implementation`` and ``prec`` attributes
@@ -561,6 +565,16 @@ def create_key_and_extra_args(self, order, name=None, modulus=None, names=None,
561565
sage: GF.create_key_and_extra_args(9, 'a', structure=None) # needs sage.libs.linbox
562566
((9, ('a',), x^2 + 2*x + 2, 'givaro', 3, 2, True, None, 'poly', True, True, True), {})
563567
568+
The ``implementation`` parameter is preferred, but ``impl`` is accepted
569+
for backwards compatibility::
570+
571+
sage: GF.create_key_and_extra_args(9, 'a', implementation='givaro') # needs sage.libs.linbox
572+
((9, ('a',), x^2 + 2*x + 2, 'givaro', 3, 2, True, None, 'poly', True, True, True), {})
573+
sage: GF.create_key_and_extra_args(9, 'a', impl='givaro') # needs sage.libs.linbox
574+
((9, ('a',), x^2 + 2*x + 2, 'givaro', 3, 2, True, None, 'poly', True, True, True), {})
575+
sage: GF.create_key_and_extra_args(9, 'a', implementation='givaro', impl='ntl') # needs sage.libs.linbox
576+
((9, ('a',), x^2 + 2*x + 2, 'givaro', 3, 2, True, None, 'poly', True, True, True), {})
577+
564578
TESTS::
565579
566580
sage: GF((6, 1), 'a') # implicit doctest
@@ -637,11 +651,20 @@ def create_key_and_extra_args(self, order, name=None, modulus=None, names=None,
637651
Finite Field in aa of size 7^2
638652
"""
639653
for key, val in kwds.items():
640-
if key not in ['structure', 'implementation', 'prec', 'embedding', 'latex_names']:
654+
if key not in ['structure', 'implementation', 'prec', 'embedding', 'latex_names', 'impl']:
641655
raise TypeError("create_key_and_extra_args() got an unexpected keyword argument '%s'" % key)
642656
if not (val is None or isinstance(val, list) and all(c is None for c in val)):
643657
raise NotImplementedError("ring extension with prescribed %s is not implemented" % key)
644658

659+
# Accept both 'implementation' (preferred) and 'impl' (for backwards compatibility)
660+
# Priority: 'implementation' takes precedence over 'impl' if both are provided
661+
impl = kwds.get('impl')
662+
if implementation is not None:
663+
impl = implementation
664+
elif impl is None:
665+
# Keep impl as None, will be set to default later
666+
pass
667+
645668
from sage.structure.proof.proof import WithProof
646669
from sage.structure.proof.all import arithmetic
647670
if proof is None:
@@ -752,37 +775,37 @@ def create_object(self, version, key, **kwds):
752775
753776
We try to create finite fields with various implementations::
754777
755-
sage: k = GF(2, impl='modn')
756-
sage: k = GF(2, impl='givaro') # needs sage.libs.linbox
757-
sage: k = GF(2, impl='ntl') # needs sage.libs.ntl
758-
sage: k = GF(2, impl='pari')
778+
sage: k = GF(2, implementation='modn')
779+
sage: k = GF(2, implementation='givaro') # needs sage.libs.linbox
780+
sage: k = GF(2, implementation='ntl') # needs sage.libs.ntl
781+
sage: k = GF(2, implementation='pari')
759782
Traceback (most recent call last):
760783
...
761784
ValueError: the degree must be at least 2
762-
sage: k = GF(2, impl='supercalifragilisticexpialidocious')
785+
sage: k = GF(2, implementation='supercalifragilisticexpialidocious')
763786
Traceback (most recent call last):
764787
...
765788
ValueError: no such finite field implementation: 'supercalifragilisticexpialidocious'
766-
sage: k.<a> = GF(2^15, impl='modn')
789+
sage: k.<a> = GF(2^15, implementation='modn')
767790
Traceback (most recent call last):
768791
...
769792
ValueError: the 'modn' implementation requires a prime order
770-
sage: k.<a> = GF(2^15, impl='givaro') # needs sage.libs.linbox
771-
sage: k.<a> = GF(2^15, impl='ntl') # needs sage.libs.ntl
772-
sage: k.<a> = GF(2^15, impl='pari')
773-
sage: k.<a> = GF(3^60, impl='modn')
793+
sage: k.<a> = GF(2^15, implementation='givaro') # needs sage.libs.linbox
794+
sage: k.<a> = GF(2^15, implementation='ntl') # needs sage.libs.ntl
795+
sage: k.<a> = GF(2^15, implementation='pari')
796+
sage: k.<a> = GF(3^60, implementation='modn')
774797
Traceback (most recent call last):
775798
...
776799
ValueError: the 'modn' implementation requires a prime order
777-
sage: k.<a> = GF(3^60, impl='givaro') # needs sage.libs.linbox
800+
sage: k.<a> = GF(3^60, implementation='givaro') # needs sage.libs.linbox
778801
Traceback (most recent call last):
779802
...
780803
ValueError: q must be < 2^16
781-
sage: k.<a> = GF(3^60, impl='ntl') # needs sage.libs.ntl
804+
sage: k.<a> = GF(3^60, implementation='ntl') # needs sage.libs.ntl
782805
Traceback (most recent call last):
783806
...
784807
ValueError: q must be a 2-power
785-
sage: k.<a> = GF(3^60, impl='pari')
808+
sage: k.<a> = GF(3^60, implementation='pari')
786809
"""
787810
# IMPORTANT! If you add a new class to the list of classes
788811
# that get cached by this factor object, then you *must* add

0 commit comments

Comments
 (0)