Path: blob/master/src/sage/rings/finite_rings/homset.py
8820 views
"""1Homset for Finite Fields23This is the set of all field homomorphisms between two finite fields.45EXAMPLES::67sage: R.<t> = ZZ[]8sage: E.<a> = GF(25, modulus = t^2 - 2)9sage: F.<b> = GF(625)10sage: H = Hom(E, F)11sage: f = H([4*b^3 + 4*b^2 + 4*b]); f12Ring morphism:13From: Finite Field in a of size 5^214To: Finite Field in b of size 5^415Defn: a |--> 4*b^3 + 4*b^2 + 4*b16sage: f(2)17218sage: f(a)194*b^3 + 4*b^2 + 4*b20sage: len(H)21222sage: [phi(2*a)^2 for phi in Hom(E, F)]23[3, 3]2425We can also create endomorphisms::2627sage: End(E)28Automorphism group of Finite Field in a of size 5^229sage: End(GF(7))[0]30Ring endomorphism of Finite Field of size 731Defn: 1 |--> 132sage: H = Hom(GF(7), GF(49, 'c'))33sage: H[0](2)34235"""3637from sage.rings.homset import RingHomset_generic38from sage.rings.finite_rings.hom_finite_field import FiniteFieldHomomorphism_generic39from sage.rings.integer import Integer40from sage.structure.sequence import Sequence4142class FiniteFieldHomset(RingHomset_generic):43"""44Set of homomorphisms with domain a given finite field.45"""46# def __init__(self, R, S, category=None):47# if category is None:48# from sage.categories.finite_fields import FiniteFields49# category = FiniteFields()50# RingHomset_generic.__init__(self, R, S, category)5152def __call__(self, im_gens, check=True):53"""54Construct the homomorphism defined by ``im_gens``.5556EXAMPLES::5758sage: R.<t> = ZZ[]59sage: E.<a> = GF(25, modulus = t^2 - 2)60sage: F.<b> = GF(625)61sage: End(E)62Automorphism group of Finite Field in a of size 5^263sage: list(Hom(E, F))64[Ring morphism:65From: Finite Field in a of size 5^266To: Finite Field in b of size 5^467Defn: a |--> 4*b^3 + 4*b^2 + 4*b,68Ring morphism:69From: Finite Field in a of size 5^270To: Finite Field in b of size 5^471Defn: a |--> b^3 + b^2 + b]72sage: [phi(2*a)^2 for phi in Hom(E, F)]73[3, 3]74sage: End(GF(7))[0]75Ring endomorphism of Finite Field of size 776Defn: 1 |--> 177sage: H = Hom(GF(7), GF(49, 'c'))78sage: H[0](2)79280sage: Hom(GF(49, 'c'), GF(7)).list()81[]82sage: Hom(GF(49, 'c'), GF(81, 'd')).list()83[]84sage: H = Hom(GF(9, 'a'), GF(81, 'b'))85sage: H == loads(dumps(H))86True87"""88if isinstance(im_gens, FiniteFieldHomomorphism_generic):89return self._coerce_impl(im_gens)90try:91return FiniteFieldHomomorphism_generic(self, im_gens, check=check)92except (NotImplementedError, ValueError), err:93try:94return self._coerce_impl(im_gens)95except TypeError:96raise TypeError, "images do not define a valid homomorphism"9798def _coerce_impl(self, x):99"""100Coercion of other morphisms.101102EXAMPLES::103104sage: k.<a> = GF(25)105sage: l.<b> = GF(625)106sage: H = Hom(k, l)107sage: G = loads(dumps(H))108sage: H == G109True110sage: H is G # this should change eventually111False112sage: G.coerce(list(H)[0]) # indirect doctest113Ring morphism:114From: Finite Field in a of size 5^2115To: Finite Field in b of size 5^4116Defn: a |--> 4*b^3 + 4*b^2 + 4*b + 3117"""118if not isinstance(x, FiniteFieldHomomorphism_generic):119raise TypeError120if x.parent() is self:121return x122if x.parent() == self:123return FiniteFieldHomomorphism_generic(self, x.im_gens())124raise TypeError125126def _repr_(self):127"""128Return a string represention of ``self``.129130EXAMPLES::131132sage: Hom(GF(4, 'a'), GF(16, 'b'))._repr_()133'Set of field embeddings from Finite Field in a of size 2^2 to Finite Field in b of size 2^4'134sage: Hom(GF(4, 'a'), GF(4, 'c'))._repr_()135'Set of field embeddings from Finite Field in a of size 2^2 to Finite Field in c of size 2^2'136sage: Hom(GF(4, 'a'), GF(4, 'a'))._repr_()137'Automorphism group of Finite Field in a of size 2^2'138"""139D = self.domain()140C = self.codomain()141if C == D:142return "Automorphism group of %s"%D143else:144return "Set of field embeddings from %s to %s"%(D, C)145146def is_aut(self):147"""148Check if ``self`` is an automorphism149150EXAMPLES::151152sage: Hom(GF(4, 'a'), GF(16, 'b')).is_aut()153False154sage: Hom(GF(4, 'a'), GF(4, 'c')).is_aut()155False156sage: Hom(GF(4, 'a'), GF(4, 'a')).is_aut()157True158"""159return self.domain() == self.codomain()160161def order(self):162"""163Return the order of this set of field homomorphisms.164165EXAMPLES::166167sage: K.<a> = GF(125)168sage: End(K)169Automorphism group of Finite Field in a of size 5^3170sage: End(K).order()1713172sage: L.<b> = GF(25)173sage: Hom(L, K).order() == Hom(K, L).order() == 0174True175"""176try:177return self.__order178except AttributeError:179pass180n = len(self.list())181self.__order = n182return n183184def list(self):185"""186Return a list of all the elements in this set of field homomorphisms.187188EXAMPLES::189190sage: K.<a> = GF(25)191sage: End(K)192Automorphism group of Finite Field in a of size 5^2193sage: list(End(K))194[Ring endomorphism of Finite Field in a of size 5^2195Defn: a |--> 4*a + 1,196Ring endomorphism of Finite Field in a of size 5^2197Defn: a |--> a]198sage: L.<z> = GF(7^6)199sage: [g for g in End(L) if (g^3)(z) == z]200[Ring endomorphism of Finite Field in z of size 7^6201Defn: z |--> 5*z^4 + 5*z^3 + 4*z^2 + 3*z + 1,202Ring endomorphism of Finite Field in z of size 7^6203Defn: z |--> 3*z^5 + 5*z^4 + 5*z^2 + 2*z + 3,204Ring endomorphism of Finite Field in z of size 7^6205Defn: z |--> z]206207TESTS:208209Check that :trac:`11390` is fixed::210211sage: K = GF(1<<16,'a'); L = GF(1<<32,'b')212sage: K.Hom(L)[0]213Ring morphism:214From: Finite Field in a of size 2^16215To: Finite Field in b of size 2^32216Defn: a |--> b^29 + b^27 + b^26 + b^23 + b^21 + b^19 + b^18 + b^16 + b^14 + b^13 + b^11 + b^10 + b^9 + b^8 + b^7 + b^6 + b^5 + b^2 + b217"""218try:219return self.__list220except AttributeError:221pass222D = self.domain()223C = self.codomain()224if D.characteristic() == C.characteristic() and Integer(D.degree()).divides(Integer(C.degree())):225f = D.modulus()226g = C['x'](f)227r = g.roots()228v = [self(D.hom(a, C)) for a, _ in r]229v = Sequence(v, immutable=True, cr=True)230else:231v = Sequence([], immutable=True, cr=False)232self.__list = v233return v234235def __getitem__(self, n):236"""237EXAMPLES::238239sage: H = Hom(GF(32, 'a'), GF(1024, 'b'))240sage: H[1]241Ring morphism:242From: Finite Field in a of size 2^5243To: Finite Field in b of size 2^10244Defn: a |--> b^7 + b^5245sage: H[2:4]246[247Ring morphism:248From: Finite Field in a of size 2^5249To: Finite Field in b of size 2^10250Defn: a |--> b^8 + b^6 + b^2,251Ring morphism:252From: Finite Field in a of size 2^5253To: Finite Field in b of size 2^10254Defn: a |--> b^9 + b^7 + b^6 + b^5 + b^4255]256"""257return self.list()[n]258259def index(self, item):260"""261Return the index of ``self``.262263EXAMPLES::264265sage: K.<z> = GF(1024)266sage: g = End(K)[3]267sage: End(K).index(g) == 3268True269"""270return self.list().index(item)271272def _an_element_(self):273"""274Return an element of ``self``.275276TESTS::277278sage: Hom(GF(3^3, 'a'), GF(3^6, 'b')).an_element()279Ring morphism:280From: Finite Field in a of size 3^3281To: Finite Field in b of size 3^6282Defn: a |--> 2*b^5 + 2*b^4283284sage: Hom(GF(3^3, 'a'), GF(3^2, 'c')).an_element()285Traceback (most recent call last):286...287EmptySetError: no homomorphisms from Finite Field in a of size 3^3 to Finite Field in c of size 3^2288289.. TODO::290291Use a more sophisticated algorithm; see also :trac:`8751`.292293"""294K = self.domain()295L = self.codomain()296if K.degree() == 1:297return L.coerce_map_from(K)298elif not K.degree().divides(L.degree()):299from sage.categories.sets_cat import EmptySetError300raise EmptySetError('no homomorphisms from %s to %s' % (K, L))301return K.hom([K.modulus().any_root(L)])302303304from sage.structure.sage_object import register_unpickle_override305register_unpickle_override('sage.rings.finite_field_morphism', 'FiniteFieldHomset', FiniteFieldHomset)306307308