Ticket #3738: 3738-6-coerce_tests_pass.patch

File 3738-6-coerce_tests_pass.patch, 19.2 KB (added by robertwb, 11 years ago)
  • sage/algebras/free_algebra.py

    # HG changeset patch
    # User Robert Bradshaw <robertwb@math.washington.edu>
    # Date 1217319879 25200
    # Node ID 8ac672aee580c7c988e8dc8b547fed563383ae48
    # Parent  d8c9400d07d71d25dea7cb8f604aee2dedb7c644
    All tests pass with new coercion model.
    
    diff -r d8c9400d07d7 -r 8ac672aee580 sage/algebras/free_algebra.py
    a b class FreeAlgebra_generic(Algebra): 
    328328        # any ring that coerces to the base ring of this free algebra.
    329329        return self._coerce_try(x, [self.base_ring()])
    330330
    331     def coerce_map_from(self, S):
     331    def coerce_map_from_impl(self, R):
    332332        if R is self.__monoid:
    333333            return True
    334334
    class FreeAlgebra_generic(Algebra): 
    340340                else:
    341341                    return False
    342342                   
    343         return Algebra.coerce_map_from(self, S)
     343        return Algebra.coerce_map_from_impl(self, R)
    344344
    345345    def gen(self,i):
    346346        """
  • sage/categories/homset.py

    diff -r d8c9400d07d7 -r 8ac672aee580 sage/categories/homset.py
    a b class Homset(Set_generic): 
    249249    def __nonzero__(self):
    250250        return True
    251251
     252    def _generic_convert_map(self, S):
     253        if self._element_constructor is None:
     254            from sage.categories.morphism import CallMorphism
     255            from sage.categories.homset import Hom
     256            return CallMorphism(Hom(S, self))
     257        else:
     258            return parent.Parent._generic_convert_map(self, S)
     259
    252260    def homset_category(self):
    253261        """
    254262        Return the category that this is a Hom in, i.e., this is
    class Homset(Set_generic): 
    279287              Defn:   Composite morphism:
    280288                      From: SymmetricGroup(4)
    281289                      To:   SymmetricGroup(6)
    282                       Defn:   Coercion morphism:
     290                      Defn:   Call morphism:
    283291                              From: SymmetricGroup(4)
    284292                              To:   SymmetricGroup(5)
    285293                            then
    class Homset(Set_generic): 
    287295                              From: SymmetricGroup(5)
    288296                              To:   SymmetricGroup(6)
    289297                    then
    290                       Coercion morphism:
     298                      Call morphism:
    291299                      From: SymmetricGroup(6)
    292300                      To:   SymmetricGroup(7)
    293301                     
  • sage/categories/morphism.pyx

    diff -r d8c9400d07d7 -r 8ac672aee580 sage/categories/morphism.pyx
    a b cdef class Morphism(Element): 
    246246            Composite morphism:
    247247              From: Integer Ring
    248248              To:   Real Double Field
    249               Defn:   Ring morphism:
     249              Defn:   Natural morphism:
    250250                      From: Integer Ring
    251251                      To:   Rational Field
    252252                    then
  • sage/categories/pushout.py

    diff -r d8c9400d07d7 -r 8ac672aee580 sage/categories/pushout.py
    a b def pushout(R, S): 
    396396#    print Ss
    397397   
    398398    if R_tower[-1][1] in Ss:
    399       Rs, Ss = Ss, Rs
    400       R_tower, S_tower = S_tower, R_tower
    401      
     399        Rs, Ss = Ss, Rs
     400        R_tower, S_tower = S_tower, R_tower
     401   
    402402    # look for join
    403403    if Ss[-1] in Rs:
    404404        if Rs[-1] == Ss[-1]:
    405             while Rs[-1] == Ss[-1]:
     405            while Rs and Ss and Rs[-1] == Ss[-1]:
    406406                Rs.pop()
    407407                Z = Ss.pop()
    408408        else:
  • sage/modules/free_module.py

    diff -r d8c9400d07d7 -r 8ac672aee580 sage/modules/free_module.py
    a b class FreeModule_submodule_with_basis_pi 
    35053505        if v.parent() is self:
    35063506            return x
    35073507        lc = E.linear_combination_of_rows(x)
    3508         if lc != v:
     3508        if lc != v and list(lc) != list(v):
    35093509            raise ArithmeticError, "vector is not in free module"
    35103510        return x
    35113511
  • sage/rings/extended_rational_field.py

    diff -r d8c9400d07d7 -r 8ac672aee580 sage/rings/extended_rational_field.py
    a b class ExtendedRationalField_class(_uniq0 
    148148        EXAMPLES:
    149149            sage: E = ExtendedRationalField
    150150            sage: E.coerce_map_from(ZZ) #indirect doctest
    151             Coercion morphism:
     151            Call morphism:
    152152              From: Integer Ring
    153153              To:   Extended Rational Field
    154154            sage: E.coerce_map_from(QQ) #indirect doctest
    class ExtendedRationalField_class(_uniq0 
    156156              From: Rational Field
    157157              To:   Extended Rational Field
    158158            sage: E.coerce_map_from(ExtendedIntegerRing)
    159             Coercion morphism:
     159            Call morphism:
    160160              From: Extended Integer Ring
    161161              To:   Extended Rational Field
    162162
  • sage/rings/finite_field_ext_pari.py

    diff -r d8c9400d07d7 -r 8ac672aee580 sage/rings/finite_field_ext_pari.py
    a b class FiniteField_ext_pari(FiniteField_g 
    374374            sage: k, a = FiniteField_ext_pari(5^2, 'a').objgen()
    375375            sage: k(R(2/3))
    376376            4
    377             sage: k(x^2)
    378             a + 3
     377            sage: k(x^2)
     378            a + 3
    379379            sage: R.<x> = GF(5)[]
    380380            sage: k(x^3-2*x+1)
    381381            2*a + 4
    class FiniteField_ext_pari(FiniteField_g 
    480480            if x.is_constant():
    481481                return self(x.constant_coefficient())
    482482            else:
    483                 return x(self.gen())
     483                return x.change_ring(self)(self.gen())
    484484
    485485        elif isinstance(x, str):
    486486            x = x.replace(self.variable_name(),'a')
  • sage/rings/finite_field_givaro.pyx

    diff -r d8c9400d07d7 -r 8ac672aee580 sage/rings/finite_field_givaro.pyx
    a b cdef class FiniteField_givaro(FiniteFiel 
    558558            if e.is_constant():
    559559                return self(e.constant_coefficient())
    560560            else:
    561                 return e(self.gen())
     561                return e.change_ring(self)(self.gen())
    562562
    563563        elif PY_TYPE_CHECK(e, Rational):
    564564            num = e.numer()
  • sage/rings/integer_mod_ring.py

    diff -r d8c9400d07d7 -r 8ac672aee580 sage/rings/integer_mod_ring.py
    a b class IntegerModRing_generic(quotient_ri 
    674674        elif S is integer_ring.ZZ:
    675675            return integer_mod.Integer_to_IntegerMod(self)
    676676        elif isinstance(S, IntegerModRing_generic):
     677            if isinstance(S, field.Field):
     678                return None
    677679            try:
    678680                return integer_mod.IntegerMod_to_IntegerMod(S, self)
    679681            except TypeError:
  • sage/rings/polynomial/laurent_polynomial_ring.py

    diff -r d8c9400d07d7 -r 8ac672aee580 sage/rings/polynomial/laurent_polynomial_ring.py
    a b class LaurentPolynomialRing_generic(Comm 
    464464            Composite morphism:
    465465              From: Rational Field
    466466              To:   Multivariate Laurent Polynomial Ring in x, y over Rational Field
    467               Defn:   Coercion morphism:
     467              Defn:   Call morphism:
    468468                      From: Rational Field
    469469                      To:   Multivariate Polynomial Ring in x, y over Rational Field
    470470                    then
  • sage/rings/polynomial/polynomial_ring.py

    diff -r d8c9400d07d7 -r 8ac672aee580 sage/rings/polynomial/polynomial_ring.py
    a b class PolynomialRing_general(sage.algebr 
    212212                return x
    213213            elif P == self.base_ring():
    214214                return self([x])
     215            elif self.base_ring().has_coerce_map_from(P):
     216                return self([P(x)])
    215217        if hasattr(x, '_polynomial_'):
    216218            return x._polynomial_(self)       
    217219        if is_SingularElement(x) and self._has_singular:
  • sage/structure/coerce.pyx

    diff -r d8c9400d07d7 -r 8ac672aee580 sage/structure/coerce.pyx
    a b cdef class CoercionModel_cache_maps(Coer 
    206206        an integer and a rational:
    207207            sage: left_morphism, right_morphism = maps[ZZ, QQ]
    208208            sage: print left_morphism
    209             Ring morphism:
     209            Natural morphism:
    210210              From: Integer Ring
    211211              To:   Rational Field
    212212            sage: print right_morphism
    cdef class CoercionModel_cache_maps(Coer 
    367367
    368368            sage: cm.explain(ZZ['x'], QQ, operator.add)
    369369            Coercion on left operand via
    370                 Conversion morphism:
     370                Call morphism:
    371371                  From: Univariate Polynomial Ring in x over Integer Ring
    372372                  To:   Univariate Polynomial Ring in x over Rational Field
    373373            Coercion on right operand via
    374                 Conversion morphism:
     374                Call morphism:
    375375                  From: Rational Field
    376376                  To:   Univariate Polynomial Ring in x over Rational Field
    377377            Arithmetic performed after coercions.
    cdef class CoercionModel_cache_maps(Coer 
    719719            sage: cm = sage.structure.element.get_coercion_model()
    720720            sage: f, g = cm.coercion_maps(ZZ, QQ)
    721721            sage: print f
    722             Ring morphism:
     722            Natural morphism:
    723723              From: Integer Ring
    724724              To:   Rational Field
    725725            sage: print g
    cdef class CoercionModel_cache_maps(Coer 
    727727           
    728728            sage: f, g = cm.coercion_maps(ZZ['x'], QQ)
    729729            sage: print f
    730             Conversion morphism:
     730            Call morphism:
    731731              From: Univariate Polynomial Ring in x over Integer Ring
    732732              To:   Univariate Polynomial Ring in x over Rational Field
    733733            sage: print g
    734             Conversion morphism:
     734            Call morphism:
    735735              From: Rational Field
    736736              To:   Univariate Polynomial Ring in x over Rational Field
    737737             
    cdef class CoercionModel_cache_maps(Coer 
    747747                # in the new coercion model anyways.
    748748                # COERCE TODO: Enable it then.
    749749                homs = self.verify_coercion_maps(R, S, homs)
     750            else:
     751                if homs is not None:
     752                    x_map, y_map = homs
     753                    if x_map is not None and not isinstance(x_map, Morphism):
     754                        raise RuntimeError, "BUG in coercion model: coerce_map_from must return Morphism"
     755                    if y_map is not None and not isinstance(y_map, Morphism):
     756                        raise RuntimeError, "BUG in coercion model: coerce_map_from must return Morphism"
    750757            swap = None if homs is None else (homs[1], homs[0])
    751758            self._coercion_maps.set(R, S, None, homs)
    752759            self._coercion_maps.set(S, R, None, swap)
    cdef class CoercionModel_cache_maps(Coer 
    766773            sage: cm.verify_coercion_maps(ZZ, QQ, homs) == homs
    767774            Traceback (most recent call last):
    768775            ...
    769             RuntimeError: ('BUG in coercion model, codomains must be identical', Ring morphism:
     776            RuntimeError: ('BUG in coercion model, codomains must be identical', Natural morphism:
    770777              From: Integer Ring
    771               To:   Rational Field, Conversion morphism:
     778              To:   Rational Field, Call morphism:
    772779              From: Rational Field
    773780              To:   Real Field with 53 bits of precision)
    774781        """
    cdef class CoercionModel_cache_maps(Coer 
    832839
    833840        If there is a coercion map either direction, use that:
    834841            sage: cm.discover_coercion(ZZ, QQ)
    835             (Ring morphism:
     842            (Natural morphism:
    836843              From: Integer Ring
    837844              To:   Rational Field, None)
    838845            sage: cm.discover_coercion(RR, QQ)
    839846            (None,
    840              Conversion morphism:
     847             Call morphism:
    841848              From: Rational Field
    842849              To:   Real Field with 53 bits of precision)
    843850       
    844851        Otherwise, try and compute an appropriate cover:
    845852            sage: cm.discover_coercion(ZZ['x,y'], RDF)
    846             (Conversion morphism:
     853            (Call morphism:
    847854              From: Multivariate Polynomial Ring in x, y over Integer Ring
    848855              To:   Multivariate Polynomial Ring in x, y over Real Double Field,
    849              Conversion morphism:
     856             Call morphism:
    850857              From: Real Double Field
    851858              To:   Multivariate Polynomial Ring in x, y over Real Double Field)
    852859             
    cdef class CoercionModel_cache_maps(Coer 
    908915            sage: R.<x> = QQ['x']
    909916            sage: A = cm.get_action(R, ZZ, operator.div); A
    910917            Right inverse action by Rational Field on Univariate Polynomial Ring in x over Rational Field
    911             with precomposition on right by Ring morphism:
     918            with precomposition on right by Natural morphism:
    912919              From: Integer Ring
    913920              To:   Rational Field
    914921            sage: A(x+10, 5)
    cdef class CoercionModel_cache_maps(Coer 
    10181025        If op is division, look for action on right by inverse:
    10191026            sage: cm.discover_action(P, ZZ, operator.div)
    10201027            Right inverse action by Rational Field on Univariate Polynomial Ring in x over Integer Ring
    1021             with precomposition on right by Ring morphism:
     1028            with precomposition on right by Natural morphism:
    10221029              From: Integer Ring
    10231030              To:   Rational Field
    10241031        """
  • sage/structure/coerce_dict.pyx

    diff -r d8c9400d07d7 -r 8ac672aee580 sage/structure/coerce_dict.pyx
    a b cdef class TripleDict: 
    2121         types (tuple, list, etc.) map to the same item.
    2222       - Comparison is done using the 'is' rather than '==' operator.
    2323       
    24     In addition, there is the following difference which (unlike the above
    25     three) should probably be changed.
    26    
    27        - Its size is fixed at creation time, so no load-adjusting parameters
    28          are in place. One can re-size it by creating a new TripleDict
    29          passing in self as a parameter.
    30        
    3124    There are special cdef set/get methods for faster access.
    3225    It is bare-bones in the sense that not all dictionary methods are
    3326    implemented.
    3427   
    35     It is implemented as a list of lists (called buckets). The bucket
     28    It is implemented as a list of lists (hereafter called buckets). The bucket
    3629    is chosen according to a very simple hash based on the object pointer.
    3730    and each bucket is of the form [k1, k2, k3, value, k1, k2, k3, value, ...]
    3831    on which a linear search is performed.
    cdef class TripleDict: 
    115108            1
    116109        """
    117110        cdef int i
     111        self.threshold = threshold
    118112        self.buckets = [[] for i from 0 <= i <  size]
    119113        if data is not None:
    120114            for k, v in data.iteritems():
    cdef class TripleDict: 
    326320    def iteritems(self):
    327321        """
    328322        EXAMPLES:
    329             sage: from sage.structure.coerce_dict import TripleDict, TripleDictIter
     323            sage: from sage.structure.coerce_dict import TripleDict
    330324            sage: L = TripleDict(31)
    331325            sage: L[1,2,3] = None
    332326            sage: list(L.iteritems())
    333327            [((1, 2, 3), None)]
    334328        """
    335329        return TripleDictIter(self)
     330       
     331    def __reduce__(self):
     332        """
     333        Note that we don't expect equality as this class concerns itself with
     334        object identy rather than object equality.
    336335
     336        EXAMPLES:
     337            sage: from sage.structure.coerce_dict import TripleDict
     338            sage: L = TripleDict(31)
     339            sage: L[1,2,3] = True
     340            sage: loads(dumps(L)) == L
     341            False
     342            sage: list(loads(dumps(L)).iteritems())
     343            [((1, 2, 3), True)]
     344        """
     345        return TripleDict, (len(self.buckets), dict(self.iteritems()), self.threshold)
     346       
    337347
    338348cdef class TripleDictIter:
    339349    def __init__(self, pairs):
  • sage/structure/formal_sum.py

    diff -r d8c9400d07d7 -r 8ac672aee580 sage/structure/formal_sum.py
    a b class FormalSums_generic(Module): 
    105105           
    106106            sage: A = FormalSums(ZZ).get_action(QQ); A
    107107            Right scalar multiplication by Rational Field on Abelian Group of all Formal Finite Sums over Rational Field
    108             with precomposition on left by Coercion morphism:
     108            with precomposition on left by Call morphism:
    109109              From: Abelian Group of all Formal Finite Sums over Integer Ring
    110110              To:   Abelian Group of all Formal Finite Sums over Rational Field
    111111            sage: A = FormalSums(QQ).get_action(ZZ); A
  • sage/structure/parent.pyx

    diff -r d8c9400d07d7 -r 8ac672aee580 sage/structure/parent.pyx
    a b cdef class Parent(category_object.Catego 
    570570        return self.coerce_map_from(S) is not None
    571571       
    572572    cpdef coerce_map_from(self, S):
     573        cdef morphism.Morphism mor
    573574        if S is self:
    574575            from sage.categories.homset import Hom
    575576            return Hom(self, self).identity()
    576577
    577578        elif isinstance(S, Set_PythonType_class):
    578             return self.coerce_map_from_c(S._type)
     579            return self.coerce_map_from(S._type)
    579580        if self._coerce_from_hash is None: # this is because parent.__init__() does not always get called
    580581            self.init_coerce()
    581582        cdef object ret
    cdef class Parent(category_object.Catego 
    604605            #        # mor = None
    605606            if mor is not None:
    606607                # NOTE: this line is what makes the coercion detection stateful
    607                 self._coerce_from_list.append(mor)
     608                # self._coerce_from_list.append(mor)
     609                pass
    608610            self._coerce_from_hash[S] = mor
    609611            _unregister_pair(self, S)
    610612            return mor
    cdef class Parent(category_object.Catego 
    864866                # maybe there is a more clever way of detecting ZZ than importing here...
    865867                from sage.rings.integer_ring import ZZ
    866868                from sage.rings.ring import Ring
    867                 if S is ZZ and not PY_TYPE_CHECK(self, Ring) and not self.has_coerce_map_from(ZZ):
     869                if S is ZZ and not self.has_coerce_map_from(ZZ):
    868870                    #print "IntegerMulAction"
    869871                    from sage.structure.coerce_actions import IntegerMulAction
    870872                    action = IntegerMulAction(S, self, not self_on_left)