Skip to content

Commit f263ac2

Browse files
committed
gave implimentation priorty over impl and resolved lint errors
1 parent d503739 commit f263ac2

File tree

1 file changed

+75
-67
lines changed

1 file changed

+75
-67
lines changed

src/sage/rings/finite_rings/finite_field_constructor.py

Lines changed: 75 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,11 @@
174174
# ****************************************************************************
175175

176176
from collections import defaultdict
177-
from sage.structure.category_object import normalize_names, certify_names
177+
178178
from sage.rings.polynomial.polynomial_element import Polynomial
179+
179180
from sage.rings.integer import Integer
181+
from sage.structure.category_object import certify_names, normalize_names
180182

181183
try:
182184
# We don't late import this because this means trouble with the Givaro library
@@ -227,9 +229,9 @@ class FiniteFieldFactory(UniqueFactory):
227229
``modulus="primitive"`` to get a primitive polynomial. You
228230
may not specify a modulus if you do not specify a variable name.
229231
230-
- ``impl`` or ``implementation`` -- (optional) a string specifying the implementation of
231-
the finite field. Both ``impl`` and ``implementation`` are accepted (``implementation``
232-
is an alias for ``impl``). Possible values are:
232+
- ``implementation`` or ``impl`` -- (optional) a string specifying the implementation of
233+
the finite field. Both ``implementation`` and ``impl`` are accepted (``impl``
234+
is supported for backwards compatibility, but ``implementation`` is preferred). Possible values are:
233235
234236
- ``'modn'`` -- ring of integers modulo `p` (only for prime fields)
235237
@@ -242,9 +244,9 @@ class FiniteFieldFactory(UniqueFactory):
242244
for extension fields)
243245
244246
- ``elem_cache`` -- (default: order < 500) cache all elements to
245-
avoid creation time; ignored unless ``impl='givaro'``
247+
avoid creation time; ignored unless ``implementation='givaro'``
246248
247-
- ``repr`` -- (default: ``'poly'``) ignored unless ``impl='givaro'``;
249+
- ``repr`` -- (default: ``'poly'``) ignored unless ``implementation='givaro'``;
248250
controls the way elements are printed to the user:
249251
250252
- 'log': repr is
@@ -464,13 +466,13 @@ class FiniteFieldFactory(UniqueFactory):
464466
465467
TESTS:
466468
467-
Check that :issue:`16934` has been fixed::
469+
Check that :issue:`16934` has been fixed::
468470
469-
sage: k1.<a> = GF(17^14, impl='pari')
470-
sage: _ = a/2
471-
sage: k2.<a> = GF(17^14, impl='pari')
472-
sage: k1 is k2
473-
True
471+
sage: k1.<a> = GF(17^14, implementation='pari')
472+
sage: _ = a/2
473+
sage: k2.<a> = GF(17^14, implementation='pari')
474+
sage: k1 is k2
475+
True
474476
475477
Check that :issue:`21433` has been fixed::
476478
@@ -512,9 +514,9 @@ def __init__(self, *args, **kwds):
512514
super().__init__(*args, **kwds)
513515

514516
def create_key_and_extra_args(self, order, name=None, modulus=None, names=None,
515-
impl=None, proof=None,
516-
check_prime=True, check_irreducible=True,
517-
prefix=None, repr=None, elem_cache=None,
517+
implementation: str | None = None, proof=None,
518+
check_prime: bool = True, check_irreducible: bool = True,
519+
prefix: str | None = None, repr: str | None = None, elem_cache: bool | None = None,
518520
**kwds):
519521
"""
520522
EXAMPLES::
@@ -538,27 +540,27 @@ def create_key_and_extra_args(self, order, name=None, modulus=None, names=None,
538540
Moreover, ``repr`` and ``elem_cache`` are ignored when not
539541
using givaro::
540542
541-
sage: GF.create_key_and_extra_args(16, 'a', impl='ntl', repr='poly') # needs sage.libs.ntl
543+
sage: GF.create_key_and_extra_args(16, 'a', implementation='ntl', repr='poly') # needs sage.libs.ntl
542544
((16, ('a',), x^4 + x + 1, 'ntl', 2, 4, True, None, None, None, True, True), {})
543-
sage: GF.create_key_and_extra_args(16, 'a', impl='ntl', elem_cache=False) # needs sage.libs.ntl
545+
sage: GF.create_key_and_extra_args(16, 'a', implementation='ntl', elem_cache=False) # needs sage.libs.ntl
544546
((16, ('a',), x^4 + x + 1, 'ntl', 2, 4, True, None, None, None, True, True), {})
545-
sage: GF(16, impl='ntl') is GF(16, impl='ntl', repr='foo') # needs sage.libs.ntl
547+
sage: GF(16, implementation='ntl') is GF(16, implementation='ntl', repr='foo') # needs sage.libs.ntl
546548
True
547549
548550
We handle extra arguments for the givaro finite field and
549551
create unique objects for their defaults::
550552
551-
sage: GF(25, impl='givaro') is GF(25, impl='givaro', repr='poly') # needs sage.libs.linbox
553+
sage: GF(25, implementation='givaro') is GF(25, implementation='givaro', repr='poly') # needs sage.libs.linbox
552554
True
553-
sage: GF(25, impl='givaro') is GF(25, impl='givaro', elem_cache=True) # needs sage.libs.linbox
555+
sage: GF(25, implementation='givaro') is GF(25, implementation='givaro', elem_cache=True) # needs sage.libs.linbox
554556
True
555-
sage: GF(625, impl='givaro') is GF(625, impl='givaro', elem_cache=False) # needs sage.libs.linbox
557+
sage: GF(625, implementation='givaro') is GF(625, implementation='givaro', elem_cache=False) # needs sage.libs.linbox
556558
True
557559
558-
Both ``impl`` and ``implementation`` are accepted (``implementation`` is
559-
an alias for ``impl``)::
560+
Both ``implementation`` and ``impl`` are accepted (``impl`` is
561+
supported for backwards compatibility, but ``implementation`` is preferred)::
560562
561-
sage: GF(25, impl='givaro') is GF(25, implementation='givaro') # needs sage.libs.linbox
563+
sage: GF(25, implementation='givaro') is GF(25, impl='givaro') # needs sage.libs.linbox
562564
True
563565
sage: GF(25, implementation='givaro') # needs sage.libs.linbox
564566
Finite Field in z2 of size 5^2
@@ -572,8 +574,7 @@ def create_key_and_extra_args(self, order, name=None, modulus=None, names=None,
572574
573575
We explicitly take ``structure`` and ``prec`` attributes
574576
for compatibility with :class:`~sage.categories.pushout.AlgebraicExtensionFunctor`
575-
but we ignore them as they are not used, see :issue:`21433`.
576-
The ``implementation`` parameter is accepted as an alias for ``impl``::
577+
but we ignore them as they are not used, see :issue:`21433`::
577578
578579
sage: GF.create_key_and_extra_args(9, 'a', structure=None) # needs sage.libs.linbox
579580
((9, ('a',), x^2 + 2*x + 2, 'givaro', 3, 2, True, None, 'poly', True, True, True), {})
@@ -653,27 +654,32 @@ def create_key_and_extra_args(self, order, name=None, modulus=None, names=None,
653654
sage: GF(7^2, names=['aa'])
654655
Finite Field in aa of size 7^2
655656
"""
656-
# Handle 'implementation' parameter: map it to 'impl' for backward compatibility
657-
if 'implementation' in kwds and kwds['implementation'] is not None:
658-
if impl is not None:
657+
# Handle 'impl' parameter: extract from kwds for backward compatibility
658+
impl = None
659+
if 'impl' in kwds and kwds['impl'] is not None:
660+
if implementation is not None:
659661
raise ValueError("Cannot specify both 'impl' and 'implementation'")
660-
impl = kwds.pop('implementation')
662+
impl = kwds.pop('impl')
663+
664+
# Use impl if provided, otherwise use implementation
665+
if impl is not None:
666+
implementation = impl
661667

662668
for key, val in kwds.items():
663-
if key not in ['structure', 'implementation', 'prec', 'embedding', 'latex_names']:
669+
if key not in ['structure', 'impl', 'prec', 'embedding', 'latex_names']:
664670
raise TypeError("create_key_and_extra_args() got an unexpected keyword argument '%s'" % key)
665671
if not (val is None or isinstance(val, list) and all(c is None for c in val)):
666672
raise NotImplementedError("ring extension with prescribed %s is not implemented" % key)
667673

668-
from sage.structure.proof.proof import WithProof
669674
from sage.structure.proof.all import arithmetic
675+
from sage.structure.proof.proof import WithProof
670676
if proof is None:
671677
proof = arithmetic()
672678
with WithProof('arithmetic', proof):
673679
if isinstance(order, tuple):
674680
if len(order) != 2:
675681
raise ValueError('wrong input for finite field constructor')
676-
p, n = map(Integer, order)
682+
p, n = [Integer(x) for x in order]
677683
if p < 2 or n < 1:
678684
raise ValueError("the order of a finite field must be a prime power")
679685
order = p**n
@@ -691,8 +697,8 @@ def create_key_and_extra_args(self, order, name=None, modulus=None, names=None,
691697
# note that we haven't tested p for primality
692698

693699
if n == 1:
694-
if impl is None:
695-
impl = 'modn'
700+
if implementation is None:
701+
implementation = 'modn'
696702
if name is not None:
697703
certify_names((name,) if isinstance(name, str) else name)
698704
name = ('x',) # Ignore name
@@ -716,20 +722,22 @@ def create_key_and_extra_args(self, order, name=None, modulus=None, names=None,
716722
check_irreducible = False
717723
name = normalize_names(1, name)
718724

719-
if impl is None:
725+
if implementation is None:
720726
if order < zech_log_bound and FiniteField_givaro is not None:
721-
impl = 'givaro'
727+
implementation = 'givaro'
722728
elif p == 2 and FiniteField_ntl_gf2e is not None:
723-
impl = 'ntl'
729+
implementation = 'ntl'
724730
else:
725-
impl = 'pari_ffelt'
731+
implementation = 'pari_ffelt'
726732

727733
# Determine modulus.
728734
# For the 'modn' implementation, we use the following
729735
# optimization which we also need to avoid an infinite loop:
730736
# a modulus of None is a shorthand for x-1.
731-
if modulus is not None or impl != 'modn':
732-
from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing
737+
if modulus is not None or implementation != 'modn':
738+
from sage.rings.polynomial.polynomial_ring_constructor import (
739+
PolynomialRing,
740+
)
733741
R = PolynomialRing(FiniteField(p), 'x')
734742
if modulus is None:
735743
modulus = R.irreducible_element(n)
@@ -746,15 +754,15 @@ def create_key_and_extra_args(self, order, name=None, modulus=None, names=None,
746754

747755
if modulus.degree() != n:
748756
raise ValueError("the degree of the modulus does not equal the degree of the field")
749-
# If modulus is x - 1 for impl="modn", set it to None
750-
if impl == 'modn' and modulus.list() == [-1,1]:
757+
# If modulus is x - 1 for implementation="modn", set it to None
758+
if implementation == 'modn' and modulus.list() == [-1,1]:
751759
modulus = None
752760
if modulus is None:
753761
check_irreducible = False
754762

755763
# Check extra arguments for givaro and setup their defaults
756764
# TODO: ntl takes a repr, but ignores it
757-
if impl == 'givaro':
765+
if implementation == 'givaro':
758766
if repr is None:
759767
repr = 'poly'
760768
if elem_cache is None:
@@ -764,7 +772,7 @@ def create_key_and_extra_args(self, order, name=None, modulus=None, names=None,
764772
repr = None
765773
elem_cache = None
766774

767-
return (order, name, modulus, impl, p, n, proof, prefix, repr, elem_cache, check_prime, check_irreducible), {}
775+
return (order, name, modulus, implementation, p, n, proof, prefix, repr, elem_cache, check_prime, check_irreducible), {}
768776

769777
def create_object(self, version, key, **kwds):
770778
"""
@@ -775,37 +783,37 @@ def create_object(self, version, key, **kwds):
775783
776784
We try to create finite fields with various implementations::
777785
778-
sage: k = GF(2, impl='modn')
779-
sage: k = GF(2, impl='givaro') # needs sage.libs.linbox
780-
sage: k = GF(2, impl='ntl') # needs sage.libs.ntl
781-
sage: k = GF(2, impl='pari')
786+
sage: k = GF(2, implementation='modn')
787+
sage: k = GF(2, implementation='givaro') # needs sage.libs.linbox
788+
sage: k = GF(2, implementation='ntl') # needs sage.libs.ntl
789+
sage: k = GF(2, implementation='pari')
782790
Traceback (most recent call last):
783791
...
784792
ValueError: the degree must be at least 2
785-
sage: k = GF(2, impl='supercalifragilisticexpialidocious')
793+
sage: k = GF(2, implementation='supercalifragilisticexpialidocious')
786794
Traceback (most recent call last):
787795
...
788796
ValueError: no such finite field implementation: 'supercalifragilisticexpialidocious'
789-
sage: k.<a> = GF(2^15, impl='modn')
797+
sage: k.<a> = GF(2^15, implementation='modn')
790798
Traceback (most recent call last):
791799
...
792800
ValueError: the 'modn' implementation requires a prime order
793-
sage: k.<a> = GF(2^15, impl='givaro') # needs sage.libs.linbox
794-
sage: k.<a> = GF(2^15, impl='ntl') # needs sage.libs.ntl
795-
sage: k.<a> = GF(2^15, impl='pari')
796-
sage: k.<a> = GF(3^60, impl='modn')
801+
sage: k.<a> = GF(2^15, implementation='givaro') # needs sage.libs.linbox
802+
sage: k.<a> = GF(2^15, implementation='ntl') # needs sage.libs.ntl
803+
sage: k.<a> = GF(2^15, implementation='pari')
804+
sage: k.<a> = GF(3^60, implementation='modn')
797805
Traceback (most recent call last):
798806
...
799807
ValueError: the 'modn' implementation requires a prime order
800-
sage: k.<a> = GF(3^60, impl='givaro') # needs sage.libs.linbox
808+
sage: k.<a> = GF(3^60, implementation='givaro') # needs sage.libs.linbox
801809
Traceback (most recent call last):
802810
...
803811
ValueError: q must be < 2^16
804-
sage: k.<a> = GF(3^60, impl='ntl') # needs sage.libs.ntl
812+
sage: k.<a> = GF(3^60, implementation='ntl') # needs sage.libs.ntl
805813
Traceback (most recent call last):
806814
...
807815
ValueError: q must be a 2-power
808-
sage: k.<a> = GF(3^60, impl='pari')
816+
sage: k.<a> = GF(3^60, implementation='pari')
809817
"""
810818
# IMPORTANT! If you add a new class to the list of classes
811819
# that get cached by this factor object, then you *must* add
@@ -821,7 +829,7 @@ def create_object(self, version, key, **kwds):
821829

822830
if len(key) == 5:
823831
# for backward compatibility of pickles (see trac 10975).
824-
order, name, modulus, impl, _ = key
832+
order, name, modulus, implementation, _ = key
825833
p, n = Integer(order).factor()[0]
826834
proof = True
827835
prefix = kwds.get('prefix', None)
@@ -832,18 +840,18 @@ def create_object(self, version, key, **kwds):
832840
check_prime = check_irreducible = False
833841
elif len(key) == 8:
834842
# For backward compatibility of pickles (see trac #21433)
835-
order, name, modulus, impl, _, p, n, proof = key
843+
order, name, modulus, implementation, _, p, n, proof = key
836844
prefix = kwds.get('prefix', None)
837845
# We can set the defaults here to be those for givaro
838846
# as they are otherwise ignored
839847
repr = kwds.get('repr', 'poly')
840848
elem_cache = kwds.get('elem_cache', (order < 500))
841849
check_prime = check_irreducible = False
842850
elif len(key) == 10:
843-
order, name, modulus, impl, p, n, proof, prefix, repr, elem_cache = key
851+
order, name, modulus, implementation, p, n, proof, prefix, repr, elem_cache = key
844852
check_prime = check_irreducible = False
845853
else:
846-
order, name, modulus, impl, p, n, proof, prefix, repr, elem_cache, check_prime, check_irreducible = key
854+
order, name, modulus, implementation, p, n, proof, prefix, repr, elem_cache, check_prime, check_irreducible = key
847855

848856
from sage.structure.proof.proof import WithProof
849857
with WithProof('arithmetic', proof):
@@ -852,7 +860,7 @@ def create_object(self, version, key, **kwds):
852860
if check_irreducible and not modulus.is_irreducible():
853861
raise ValueError("finite field modulus must be irreducible but it is not")
854862

855-
if impl == 'modn':
863+
if implementation == 'modn':
856864
if n != 1:
857865
raise ValueError("the 'modn' implementation requires a prime order")
858866
from .finite_field_prime_modn import FiniteField_prime_modn
@@ -867,15 +875,15 @@ def create_object(self, version, key, **kwds):
867875
# Otherwise, we would have to complicate all of their
868876
# constructors with check options.
869877
with WithProof('arithmetic', proof):
870-
if impl == 'givaro':
878+
if implementation == 'givaro':
871879
K = FiniteField_givaro(order, name, modulus, repr, elem_cache)
872-
elif impl == 'ntl':
880+
elif implementation == 'ntl':
873881
K = FiniteField_ntl_gf2e(order, name, modulus)
874-
elif impl == 'pari_ffelt' or impl == 'pari':
882+
elif implementation == 'pari_ffelt' or implementation == 'pari':
875883
from .finite_field_pari_ffelt import FiniteField_pari_ffelt
876884
K = FiniteField_pari_ffelt(p, modulus, name)
877885
else:
878-
raise ValueError("no such finite field implementation: %r" % impl)
886+
raise ValueError("no such finite field implementation: %r" % implementation)
879887

880888
# Temporary; see create_key_and_extra_args() above.
881889
if prefix is not None:

0 commit comments

Comments
 (0)