Ticket #9054: trac_9054_maps_refactor.patch

File trac_9054_maps_refactor.patch, 14.6 KB (added by saraedum, 6 years ago)

refactored maps class hieararchy

  • sage/rings/function_field/function_field.py

    exporting patch:
    # HG changeset patch
    # User Julian Rueth <julian.rueth@gmail.com>
    Trac 9054: refactored maps class hierarchy
    
    diff --git a/sage/rings/function_field/function_field.py b/sage/rings/function_field/function_field.py
    index f894165..093dbd6 100644
    a b class FunctionField(Field): 
    276276            return True
    277277        return False
    278278
    279     def hom(self, im_gens, base_morphism=None):
    280         """
    281         Create a homomorphism from self to another function field.
    282        
    283         INPUT:
    284 
    285            - ``im_gens`` -- a list of images of the generators of self
    286              and of successive base rings.
    287            
    288            - ``base_morphism`` -- (default: None) a homomorphism of
    289              the base ring, after the im_gens are used.  Thus if
    290              im_gens has length 2, then base_morphism should be a morphism
    291              from self.base_ring().base_ring().
    292 
    293         EXAMPLES::
    294        
    295         We create a rational function field, and a quadratic extension of it::
    296        
    297             sage: R.<x> = FunctionField(QQ); S.<y> = R[]
    298             sage: L.<y> = R.extension(y^2 - x^3 - 1)
    299 
    300         We make the field automorphism that sends y to -y::
    301        
    302             sage: f = L.hom(-y); f
    303             Morphism of function fields defined by y |--> -y
    304 
    305         Evaluation works::
    306        
    307             sage: f(y*x - 1/x)
    308             -x*y - 1/x
    309 
    310         We try to define an invalid morphism::
    311        
    312             sage: f = L.hom(y+1); f
    313             Traceback (most recent call last):
    314             ...
    315             ValueError: invalid morphism
    316 
    317         We make a morphism of the base rational function field::
    318        
    319             sage: phi = R.hom(x+1); phi
    320             Morphism of function fields defined by x |--> x + 1
    321             sage: phi(x^3 - 3)
    322             x^3 + 3*x^2 + 3*x - 2
    323             sage: (x+1)^3-3
    324             x^3 + 3*x^2 + 3*x - 2
    325 
    326         We make a morphism by specifying where the generators and the
    327         base generators go::
    328        
    329             sage: L.hom([-y, x])
    330             Morphism of function fields defined by y |--> -y,  x |--> x
    331 
    332         The usage of the keyword base_morphism is not implemented yet::
    333 
    334             sage: L.hom([-y, x-1], base_morphism=phi)
    335             Traceback (most recent call last):
    336             ...
    337             NotImplementedError: Function field homorphisms with optional argument base_morphism are not implemented yet. Please specify the images of the generators of the base fields manually.
    338            
    339                
    340         We make another extension of a rational function field::
    341        
    342             sage: R2.<t> = FunctionField(QQ); S2.<w> = R2[]
    343             sage: L2.<w> = R2.extension((4*w)^2 - (t+1)^3 - 1)
    344 
    345         We define a morphism, by giving the images of generators::
    346        
    347             sage: f = L.hom([4*w, t+1]); f
    348             Morphism of function fields defined by y |--> 4*w,  x |--> t + 1
    349 
    350         Evaluation works, as expected::
    351        
    352             sage: f(y+x)
    353             4*w + t + 1
    354             sage: f(x*y + x/(x^2+1))
    355             (4*t + 4)*w + (t + 1)/(t^2 + 2*t + 2)
    356 
    357         We make another extension of a rational function field::
    358 
    359             sage: R3.<yy> = FunctionField(QQ); S3.<xx> = R3[]
    360             sage: L3.<xx> = R3.extension(yy^2 - xx^3 - 1)
    361 
    362         This is the function field L with the generators exchanged. We define a morphism to L::
    363 
    364             sage: g = L3.hom([x,y]); g
    365             Morphism of function fields defined by xx |--> x, yy |--> y
    366 
    367         """
    368         if base_morphism is not None:
    369             raise NotImplementedError, "Function field homorphisms with optional argument base_morphism are not implemented yet. Please specify the images of the generators of the base fields manually." 
    370        
    371         if not isinstance(im_gens, (list,tuple)):
    372             im_gens = [im_gens]
    373         if len(im_gens) == 0:
    374             raise ValueError, "no images specified"
    375    
    376         if len(im_gens) > 1:
    377             base_morphism = self.base_field().hom(im_gens[1:], base_morphism)
    378 
    379         # the codomain of this morphism is the field containing all the im_gens
    380         codomain = im_gens[0].parent();
    381         if base_morphism is not None:
    382             if base_morphism.codomain().has_coerce_map_from(codomain):
    383                 codomain = base_morphism.codomain();
    384 
    385         return maps.FunctionFieldMorphism(self.Hom(codomain), im_gens[0], base_morphism)
    386 
    387279class FunctionField_polymod(FunctionField):
    388280    """
    389281    A function field defined by a univariate polynomial, as an
    class FunctionField_polymod(FunctionField): 
    917809        return self.order(d*self.gen(), check=False)
    918810   
    919811
     812    def hom(self, im_gens, base_morphism=None):
     813        """
     814        Create a homomorphism from self to another function field.
     815
     816        INPUT:
     817
     818           - ``im_gens`` -- a list of images of the generators of self
     819             and of successive base rings.
     820
     821           - ``base_morphism`` -- (default: None) a homomorphism of
     822             the base ring, after the im_gens are used.  Thus if
     823             im_gens has length 2, then base_morphism should be a morphism
     824             from self.base_ring().base_ring().
     825
     826        EXAMPLES:
     827
     828        We create a rational function field, and a quadratic extension of it::
     829
     830            sage: K.<x> = FunctionField(QQ); R.<y> = K[]
     831            sage: L.<y> = K.extension(y^2 - x^3 - 1)
     832
     833        We make the field automorphism that sends y to -y::
     834
     835            sage: f = L.hom(-y); f
     836            Morphism of function fields defined by y |--> -y
     837
     838        Evaluation works::
     839
     840            sage: f(y*x - 1/x)
     841            -x*y - 1/x
     842
     843        We try to define an invalid morphism::
     844
     845            sage: f = L.hom(y+1)
     846            Traceback (most recent call last):
     847            ...
     848            ValueError: invalid morphism
     849
     850        We make a morphism of the base rational function field::
     851
     852            sage: phi = K.hom(x+1); phi
     853            Morphism of function fields defined by x |--> x + 1
     854            sage: phi(x^3 - 3)
     855            x^3 + 3*x^2 + 3*x - 2
     856            sage: (x+1)^3-3
     857            x^3 + 3*x^2 + 3*x - 2
     858
     859        We make a morphism by specifying where the generators and the
     860        base generators go::
     861
     862            sage: L.hom([-y, x])
     863            Morphism of function fields defined by y |--> -y,  x |--> x
     864
     865        The usage of the keyword base_morphism is not implemented yet::
     866
     867            sage: L.hom([-y, x-1], base_morphism=phi)
     868            Traceback (most recent call last):
     869            ...
     870            NotImplementedError: Function field homorphisms with optional argument base_morphism are not implemented yet. Please specify the images of the generators of the base fields manually.
     871
     872        We make another extension of a rational function field::
     873
     874            sage: K2.<t> = FunctionField(QQ); R2.<w> = K2[]
     875            sage: L2.<w> = K2.extension((4*w)^2 - (t+1)^3 - 1)
     876
     877        We define a morphism, by giving the images of generators::
     878
     879            sage: f = L.hom([4*w, t+1]); f
     880            Morphism of function fields defined by y |--> 4*w,  x |--> t + 1
     881
     882        Evaluation works, as expected::
     883
     884            sage: f(y+x)
     885            4*w + t + 1
     886            sage: f(x*y + x/(x^2+1))
     887            (4*t + 4)*w + (t + 1)/(t^2 + 2*t + 2)
     888
     889        We make another extension of a rational function field::
     890
     891            sage: K3.<yy> = FunctionField(QQ); R3.<xx> = K3[]
     892            sage: L3.<xx> = K3.extension(yy^2 - xx^3 - 1)
     893
     894        This is the function field L with the generators exchanged. We define a morphism to L::
     895
     896            sage: g = L3.hom([x,y]); g
     897            Morphism of function fields defined by xx |--> x, yy |--> y
     898        """
     899        if base_morphism is not None:
     900            raise NotImplementedError, "Function field homorphisms with optional argument base_morphism are not implemented yet. Please specify the images of the generators of the base fields manually."
     901
     902        if not isinstance(im_gens, (list,tuple)):
     903            im_gens = [im_gens]
     904        if len(im_gens) == 0:
     905            raise ValueError, "no images specified"
     906
     907        if len(im_gens) > 1:
     908            base_morphism = self.base_field().hom(im_gens[1:], base_morphism)
     909
     910        # the codomain of this morphism is the field containing all the im_gens
     911        codomain = im_gens[0].parent();
     912        if base_morphism is not None:
     913            if base_morphism.codomain().has_coerce_map_from(codomain):
     914                codomain = base_morphism.codomain();
     915
     916        from maps import FunctionFieldMorphism_polymod
     917        return FunctionFieldMorphism_polymod(self.Hom(codomain), im_gens[0], base_morphism)
     918
    920919class RationalFunctionField(FunctionField):
    921920    """
    922921    A rational function field K(t) in one variable, over an arbitrary
  • sage/rings/function_field/maps.py

    diff --git a/sage/rings/function_field/maps.py b/sage/rings/function_field/maps.py
    index 88aad0e..64d998b 100644
    a b r""" 
    22
    33"""
    44
    5 from sage.categories.map import Map
     5from sage.categories.morphism import Morphism
     6from sage.rings.morphism import RingHomomorphism
    67from sage.categories.homset import Hom
    78
    89from function_field_order import FunctionFieldOrder
    910
    10 class FunctionFieldIsomorphism(Map):
     11class FunctionFieldIsomorphism(Morphism):
    1112    r"""
    12     A base class for various isomorphisms between function fields and
     13    A base class for isomorphisms between function fields and
    1314    vector spaces.
    1415
    1516    EXAMPLES::
    class MapVectorSpaceToFunctionField(FunctionFieldIsomorphism): 
    6667
    6768        sage: K.<x> = FunctionField(QQ); R.<y> = K[]; L.<Y> = K.extension(y^2 - x*y + 4*x^3)
    6869        sage: V, f, t = L.vector_space(); f
    69         Isomorphism map:
     70        Isomorphism morphism:
    7071          From: Vector space of dimension 2 over Rational function field in x over Rational Field
    7172          To:   Function field in Y defined by y^2 - x*y + 4*x^3
    7273    """
    class MapFunctionFieldToVectorSpace(FunctionFieldIsomorphism): 
    126127
    127128        sage: K.<x> = FunctionField(QQ); R.<y> = K[]; L.<Y> = K.extension(y^2 - x*y + 4*x^3)
    128129        sage: V, f, t = L.vector_space(); t
    129         Isomorphism map:
     130        Isomorphism morphism:
    130131          From: Function field in Y defined by y^2 - x*y + 4*x^3
    131132          To:   Vector space of dimension 2 over Rational function field in x over Rational Field
    132133    """
    class MapFunctionFieldToVectorSpace(FunctionFieldIsomorphism): 
    191192        w = v + [self._zero]*(self._n - len(v))
    192193        return self._V(w)
    193194
    194 
    195 ##########################################################################
    196 # Morphisms between function fields
    197 
    198 class FunctionFieldMorphism(Map):
    199     """
    200     EXAMPLES::
    201 
    202         sage: R.<x> = FunctionField(QQ); S.<y> = R[]
    203         sage: L.<y> = R.extension(y^2 - x^2)
    204         sage: f = L.hom(-y); f
    205         Morphism of function fields defined by y |--> -y   
    206     """
     195class FunctionFieldMorphism(RingHomomorphism):
    207196    def __init__(self, parent, im_gen, base_morphism):
    208         """
    209         EXAMPLES::
     197        RingHomomorphism.__init__(self, parent)
    210198
    211             sage: R.<x> = FunctionField(GF(7)); S.<y> = R[]
    212             sage: L.<y> = R.extension(y^3 + 6*x^3 + x); f = L.hom(y*2); f
    213             Morphism of function fields defined by y |--> 2*y
    214             sage: type(f)
    215             <class 'sage.rings.function_field.maps.FunctionFieldMorphism'>
    216             sage: factor(L.polynomial(), proof=False)
    217             y^3 + 6*x^3 + x
    218             sage: f(y).charpoly('y')
    219             y^3 + 6*x^3 + x
    220         """
    221199        self._im_gen = im_gen
    222200        self._base_morphism = base_morphism
    223         Map.__init__(self, parent)
    224         # Verify that the morphism is valid:
    225         R = self.codomain()['X']
    226         v = parent.domain().polynomial().list()
    227         if base_morphism is not None:
    228             v = [base_morphism(a) for a in v]
    229         f = R(v)
    230         if f(im_gen):
    231             raise ValueError, "invalid morphism"
    232    
     201
    233202    def is_injective(self):
    234203        """
     204        Returns True since homomorphisms of fields are injective.
     205
    235206        EXAMPLES::
    236207
    237208            sage: R.<x> = FunctionField(GF(7)); S.<y> = R[]
    class FunctionFieldMorphism(Map): 
    252223        """
    253224        return "Morphism of function fields defined by %s"%self._short_repr()
    254225
    255     def __nonzero__(self):
    256         """
    257         EXAMPLES::
    258 
    259             sage: R.<x> = FunctionField(GF(7)); S.<y> = R[]
    260             sage: L.<y> = R.extension(y^3 + 6*x^3 + x); f = L.hom(y*2)
    261             sage: f.__nonzero__()
    262             True
    263             sage: bool(f)
    264             True
    265         """
    266         return True
    267 
    268226    def _short_repr(self):
    269227        """
    270228        EXAMPLES::
    class FunctionFieldMorphism(Map): 
    278236        if self._base_morphism is not None:
    279237            a += ',  ' + self._base_morphism._short_repr()
    280238        return a
     239
     240class FunctionFieldMorphism_polymod(FunctionFieldMorphism):
     241    """
     242    EXAMPLES::
     243
     244        sage: R.<x> = FunctionField(QQ); S.<y> = R[]
     245        sage: L.<y> = R.extension(y^2 - x^2)
     246        sage: f = L.hom(-y); f
     247        Morphism of function fields defined by y |--> -y   
     248    """
     249    def __init__(self, parent, im_gen, base_morphism):
     250        """
     251        EXAMPLES::
     252
     253            sage: R.<x> = FunctionField(GF(7)); S.<y> = R[]
     254            sage: L.<y> = R.extension(y^3 + 6*x^3 + x); f = L.hom(y*2); f
     255            Morphism of function fields defined by y |--> 2*y
     256            sage: type(f)
     257            <class 'sage.rings.function_field.maps.FunctionFieldMorphism_polymod'>
     258            sage: factor(L.polynomial(), proof=False)
     259            y^3 + 6*x^3 + x
     260            sage: f(y).charpoly('y')
     261            y^3 + 6*x^3 + x
     262        """
     263        FunctionFieldMorphism.__init__(self, parent, im_gen, base_morphism)
     264        # Verify that the morphism is valid:
     265        R = self.codomain()['X']
     266        v = parent.domain().polynomial().list()
     267        if base_morphism is not None:
     268            v = [base_morphism(a) for a in v]
     269        f = R(v)
     270        if f(im_gen):
     271            raise ValueError, "invalid morphism"
    281272   
    282273    def _call_(self, x):
    283274        """
    class FunctionFieldMorphism_rational(FunctionFieldMorphism): 
    306297            sage: type(f)
    307298            <class 'sage.rings.function_field.maps.FunctionFieldMorphism_rational'>
    308299        """
    309         Map.__init__(self, parent)
    310         self._im_gen = im_gen
    311         self._base_morphism = None
     300        FunctionFieldMorphism.__init__(self, parent, im_gen, None)
    312301
    313302    def _call_(self, x):
    314303        """