Ticket #3738: 3738-4-coerce_name_fixes.patch

File 3738-4-coerce_name_fixes.patch, 34.6 KB (added by robertwb, 11 years ago)
  • sage/categories/action.pyx

    # HG changeset patch
    # User Robert Bradshaw <robertwb@math.washington.edu>
    # Date 1217319876 25200
    # Node ID 42946824190e651af18848bcf3cf0f7c92cfe479
    # Parent  9f227080425a18f8fe886b70929693e96331ed09
    Fix various naming/signature changes in the from the new model.
    
    diff -r 9f227080425a -r 42946824190e sage/categories/action.pyx
    a b cdef class InverseAction(Action): 
    120120
    121121    TESTS:
    122122    This illustrates a shortcoming in the current coercion model.
    123     See the comments in _call_c below.
     123    See the comments in _call_ below.
    124124   
    125125        sage: x = polygen(QQ,'x')
    126126        sage: a = 2*x^2+2; a
  • sage/categories/morphism.pyx

    diff -r 9f227080425a -r 42946824190e sage/categories/morphism.pyx
    a b cdef class Morphism(Element): 
    150150            return self._call_with_args(x, args, kwds)
    151151
    152152    cpdef Element _call_(self, x):
    153         raise NotImplementedError
     153        raise NotImplementedError, type(self)
    154154
    155155    cpdef Element _call_with_args(self, x, args=(), kwds={}):
    156156        if len(args) == 0 and len(kwds) == 0:
  • sage/rings/complex_double.pyx

    diff -r 9f227080425a -r 42946824190e sage/rings/complex_double.pyx
    a b cdef class FloatToCDF(Morphism): 
    17101710            from sage.structure.parent import Set_PythonType
    17111711            R = Set_PythonType(R)
    17121712        Morphism.__init__(self, Hom(R, CDF))
    1713     cdef Element _call_c(self, x):
    1714         # Override this _call_c rather than _call_c_impl because a may not be an Element
     1713    cpdef Element _call_(self, x):
    17151714        cdef ComplexDoubleElement z = <ComplexDoubleElement>PY_NEW(ComplexDoubleElement)
    17161715        z._complex = gsl_complex_rect(PyFloat_AsDouble(x), 0)
    17171716        return z
  • sage/rings/integer_mod.pyx

    diff -r 9f227080425a -r 42946824190e sage/rings/integer_mod.pyx
    a b cdef class IntegerMod_hom(Morphism): 
    28392839        Morphism.__init__(self, parent)
    28402840        self.zero = self._codomain(0)
    28412841        self.modulus = self._codomain._pyx_order
    2842     cdef Element _call_c_impl(self, Element x):
     2842    cpdef Element _call_(self, x):
    28432843        return IntegerMod(self.codomain(), x)
    28442844
    28452845cdef class IntegerMod_to_IntegerMod(IntegerMod_hom):
    cdef class IntegerMod_to_IntegerMod(Inte 
    28632863        import sage.categories.homset
    28642864        IntegerMod_hom.__init__(self, sage.categories.homset.Hom(R, S))
    28652865       
    2866     cdef Element _call_c_impl(self, Element x):
     2866    cpdef Element _call_(self, x):
    28672867        cdef IntegerMod_abstract a
    28682868        if PY_TYPE_CHECK(x, IntegerMod_int):
    28692869            return (<IntegerMod_int>self.zero)._new_c((<IntegerMod_int>x).ivalue % self.modulus.int32)
    cdef class Integer_to_IntegerMod(Integer 
    28952895        import sage.categories.homset
    28962896        IntegerMod_hom.__init__(self, sage.categories.homset.Hom(integer_ring.ZZ, R))
    28972897
    2898     cdef Element _call_c_impl(self, Element x):
     2898    cpdef Element _call_(self, x):
    28992899        cdef IntegerMod_abstract a
    29002900        cdef Py_ssize_t res
    29012901        if self.modulus.table is not None:
    cdef class Int_to_IntegerMod(IntegerMod_ 
    29322932        from sage.structure.parent import Set_PythonType
    29332933        IntegerMod_hom.__init__(self, sage.categories.homset.Hom(Set_PythonType(int), R))
    29342934
    2935     cdef Element _call_c(self, x):
     2935    cpdef Element _call_(self, x):
    29362936        cdef IntegerMod_abstract a
    29372937        cdef long res = PyInt_AS_LONG(x)
    29382938        if PY_TYPE_CHECK(self.zero, IntegerMod_gmp):
  • sage/rings/morphism.pxd

    diff -r 9f227080425a -r 42946824190e sage/rings/morphism.pxd
    a b cdef class RingMap(Morphism): 
    99
    1010cdef class RingMap_lift(RingMap):
    1111    cdef Ring S
    12     cdef Element _call_c_impl(self, Element x)
    13     cdef _update_slots(self, _slots)
    14     cdef _extra_slots(self, _slots)
    1512
    1613cdef class RingHomomorphism(RingMap):
    1714    cdef RingMap _lift
    18     cdef _update_slots(self, _slots)
    19     cdef _extra_slots(self, _slots)
    2015
    2116cdef class RingHomomorphism_coercion(RingHomomorphism):
    22     cdef Element _call_c_impl(self, Element x)
     17    pass
    2318
    2419cdef class RingHomomorphism_im_gens(RingHomomorphism):
    2520    cdef __im_gens
    26     cdef Element _call_c_impl(self, Element x)
    27     cdef _update_slots(self, _slots)
    28     cdef _extra_slots(self, _slots)
    2921
    3022cdef class RingHomomorphism_cover(RingHomomorphism):
    31     cdef Element _call_c_impl(self, Element x)
     23    pass
    3224
    3325cdef class RingHomomorphism_from_quotient(RingHomomorphism):
    3426    cdef phi
    35     cdef Element _call_c_impl(self, Element x)
    36     cdef _update_slots(self, _slots)
    37     cdef _extra_slots(self, _slots)
    38  No newline at end of file
  • sage/rings/morphism.pyx

    diff -r 9f227080425a -r 42946824190e sage/rings/morphism.pyx
    a b cdef class RingMap_lift(RingMap): 
    391391    def _repr_defn(self):
    392392        return "Choice of lifting map"
    393393
    394     cdef Element _call_c_impl(self, Element x):
     394    cpdef Element _call_(self, x):
    395395        return self.S._coerce_c(x.lift())
    396396
    397397cdef class RingHomomorphism(RingMap):
    cdef class RingHomomorphism_coercion(Rin 
    516516    def _repr_type(self):
    517517        return "Ring Coercion"
    518518
    519     cdef Element _call_c_impl(self, Element x):
     519    cpdef Element _call_(self, x):
    520520        return self.codomain()._coerce_(x)
    521521
    522522import sage.structure.all
    cdef class RingHomomorphism_im_gens(Ring 
    605605        return '\n'.join(['%s |--> %s'%(D.gen(i), ig[i]) for\
    606606                       i in range(D.ngens())])
    607607           
    608     cdef Element _call_c_impl(self, Element x):
     608    cpdef Element _call_(self, x):
    609609        return x._im_gens_(self.codomain(), self.im_gens())
    610610
    611611cdef class RingHomomorphism_cover(RingHomomorphism):
    cdef class RingHomomorphism_cover(RingHo 
    626626    def __init__(self, parent):
    627627        RingHomomorphism.__init__(self, parent)
    628628
    629     cdef Element _call_c_impl(self, Element x):
     629    cpdef Element _call_(self, x):
    630630        return self.codomain()(x)
    631631
    632632    def _repr_defn(self):
    cdef class RingHomomorphism_from_quotien 
    745745        return '\n'.join(['%s |--> %s'%(D.gen(i), ig[i]) for\
    746746                          i in range(D.ngens())])
    747747
    748     cdef Element _call_c_impl(self, Element x):
     748    cpdef Element _call_(self, x):
    749749        return self.phi(self.lift(x))
    750750       
    751751
  • sage/rings/number_field/number_field_element_quadratic.pyx

    diff -r 9f227080425a -r 42946824190e sage/rings/number_field/number_field_element_quadratic.pyx
    a b cdef class Q_to_quadratic_field_element( 
    11501150        self.zero_element.D = K._D
    11511151       
    11521152
    1153     cdef Element _call_c_impl(self, Element x):
     1153    cpdef Element _call_(self, x):
    11541154        cdef NumberFieldElement_quadratic y = self.zero_element._new()
    11551155        y.D = self.zero_element.D
    11561156        mpz_set(y.a, mpq_numref((<Rational>x).value))
  • sage/rings/rational.pyx

    diff -r 9f227080425a -r 42946824190e sage/rings/rational.pyx
    a b cdef class Z_to_Q(Morphism): 
    17321732        import sage.categories.homset
    17331733        Morphism.__init__(self, sage.categories.homset.Hom(integer_ring.ZZ, rational_field.QQ))
    17341734       
    1735     cdef Element _call_c_impl(self, Element x):
     1735    cpdef Element _call_(self, x):
    17361736        cdef Rational rat
    17371737        rat = <Rational> PY_NEW(Rational)
    17381738        mpq_set_z(rat.value, (<integer.Integer>x).value)
    cdef class int_to_Q(Morphism): 
    17481748        from sage.structure.parent import Set_PythonType
    17491749        Morphism.__init__(self, sage.categories.homset.Hom(Set_PythonType(int), rational_field.QQ))
    17501750
    1751     cdef Element _call_c(self, a):
    1752         # Override this _call_c rather than _call_c_impl because a is not an Element
     1751    cpdef Element _call_(self, a):
    17531752        cdef Rational rat
    17541753        rat = <Rational> PY_NEW(Rational)
    17551754        mpq_set_si(rat.value, PyInt_AS_LONG(a), 1)
  • sage/rings/real_double.pyx

    diff -r 9f227080425a -r 42946824190e sage/rings/real_double.pyx
    a b cdef class ToRDF(Morphism): 
    17291729            from sage.structure.parent import Set_PythonType
    17301730            R = Set_PythonType(R)
    17311731        Morphism.__init__(self, Hom(R, RDF))
    1732     cdef Element _call_c(self, x):
    1733         # Override this _call_c rather than _call_c_impl because a may not be an Element
     1732    cpdef Element _call_(self, x):
    17341733        cdef RealDoubleElement r = <RealDoubleElement>PY_NEW(RealDoubleElement)
    17351734        r._value = PyFloat_AsDouble(x)
    17361735        return r
  • sage/rings/residue_field.pxd

    diff -r 9f227080425a -r 42946824190e sage/rings/residue_field.pxd
    a b cdef class ResidueFieldHomomorphism(Ring 
    66    pass
    77
    88cdef class NFResidueFieldHomomorphism(ResidueFieldHomomorphism):
    9     cdef Element _call_c_impl(self, Element x)
    109    cdef object im_gen
    1110    cdef object p
  • sage/rings/residue_field.pyx

    diff -r 9f227080425a -r 42946824190e sage/rings/residue_field.pyx
    a b cdef class NFResidueFieldHomomorphism(Re 
    556556        self.p = p
    557557        ResidueFieldHomomorphism.__init__(self,Hom(p.number_field().maximal_order(), k, Rings())) # should eventually change to p.order()
    558558
    559     cdef Element _call_c_impl(self, Element x):
     559    cpdef Element _call_(self, x):
    560560        """
    561561        Applies this morphism to an element
    562562
  • sage/structure/coerce.pyx

    diff -r 9f227080425a -r 42946824190e sage/structure/coerce.pyx
    a b cdef class CoercionModel_cache_maps(Coer 
    963963            if PY_TYPE_CHECK(S, type):
    964964                S = Set_PythonType(S)
    965965               
    966             if action.left_domain() == R and action.right_domain() == S:
    967                 # Non-unique parents
    968                 if fix:
    969                     if action.left_domain() is not R:
    970                         action = PrecomposedAction(action, action.left_domain().coerce_map_from(R), None)
    971                     if action.right_domain() is not S:
    972                         action = PrecomposedAction(action, None, action.right_domain().coerce_map_from(S))
     966            # Non-unique parents
     967            if fix and action.left_domain() is not R and action.left_domain() == R:
     968                action = PrecomposedAction(action, action.left_domain().coerce_map_from(R), None)
     969            if fix and action.right_domain() is not S and action.right_domain() == S:
     970                action = PrecomposedAction(action, None, action.right_domain().coerce_map_from(S))
     971           
    973972            if action.left_domain() is not R or action.right_domain() is not S:
    974973                raise RuntimeError, """There is a BUG in the coercion model:
    975974                Action found for R %s S does not have the correct domains
  • sage/structure/element.pyx

    diff -r 9f227080425a -r 42946824190e sage/structure/element.pyx
    a b cdef class Element(sage_object.SageObjec 
    526526        return not self
    527527
    528528    def _cmp_(left, right):
    529         return left._cmp(right)
     529        return left._cmp_c_impl(right)
    530530
    531531    cdef _cmp(left, right):
    532532        """
    cdef class Element(sage_object.SageObjec 
    536536        cdef int r
    537537        if not have_same_parent(left, right):
    538538            try:
    539                 _left, _right = coercion_model.canonical_coercion_c(left, right)
     539                _left, _right = coercion_model.canonical_coercion(left, right)
    540540                if PY_IS_NUMERIC(_left):
    541541                    return cmp(_left, _right)
    542542                else:
    cdef class Element(sage_object.SageObjec 
    547547                    r = -1
    548548                return r
    549549        else:
     550            if HAS_DICTIONARY(left):
     551                left_cmp = left._cmp_
     552                if PY_TYPE_CHECK(left_cmp, MethodType):
     553                    # it must have been overriden
     554                    return left_cmp(right)
     555               
    550556            return left._cmp_c_impl(right)
    551557
    552558    def _richcmp_(left, right, op):
    cdef class Element(sage_object.SageObjec 
    560566        cdef int r
    561567        if not have_same_parent(left, right):
    562568            try:
    563                 _left, _right = coercion_model.canonical_coercion_c(left, right)
     569                _left, _right = coercion_model.canonical_coercion(left, right)
    564570                if PY_IS_NUMERIC(_left):
    565571                    return _rich_to_bool(op, cmp(_left, _right))
    566572                else:
    cdef class ModuleElement(Element): 
    709715                return _add_c(<ModuleElement>left, <ModuleElement>right)
    710716               
    711717        global coercion_model
    712         return coercion_model.bin_op_c(left, right, add)
     718        return coercion_model.bin_op(left, right, add)
    713719       
    714720    cdef ModuleElement _add_c(left, ModuleElement right):
    715721        """
    cdef class ModuleElement(Element): 
    758764                return _add_c(<ModuleElement>self, <ModuleElement>right)
    759765        else:
    760766            global coercion_model
    761             return coercion_model.bin_op_c(self, right, iadd)
     767            return coercion_model.bin_op(self, right, iadd)
    762768                       
    763769    def _iadd_(self, right):
    764770        return self._iadd_c_impl(right)
    cdef class ModuleElement(Element): 
    781787            else:
    782788                return _sub_c(<ModuleElement>left, <ModuleElement>right)
    783789        global coercion_model
    784         return coercion_model.bin_op_c(left, right, sub)
     790        return coercion_model.bin_op(left, right, sub)
    785791
    786792    cdef ModuleElement _sub_c(left, ModuleElement right):
    787793        """
    cdef class ModuleElement(Element): 
    829835                return _sub_c(<ModuleElement>self, <ModuleElement>right)
    830836        else:
    831837            global coercion_model
    832             return coercion_model.bin_op_c(self, right, isub)
     838            return coercion_model.bin_op(self, right, isub)
    833839                       
    834840    def _isub_(self, right):
    835841        return self._isub_c_impl(right)
    cdef class ModuleElement(Element): 
    873879        # default implementation is to try multiplying by -1.
    874880        global coercion_model
    875881        if self._parent._base is None:
    876             return coercion_model.bin_op_c(-1, self, mul)
     882            return coercion_model.bin_op(-1, self, mul)
    877883        else:
    878             return coercion_model.bin_op_c(self._parent._base(-1), self, mul)
     884            return coercion_model.bin_op(self._parent._base(-1), self, mul)
    879885       
    880886
    881887    def _neg_(ModuleElement self):
    cdef class ModuleElement(Element): 
    894900            raise arith_error_message(left, right, mul)
    895901        # Always do this
    896902        global coercion_model
    897         return coercion_model.bin_op_c(left, right, mul)
     903        return coercion_model.bin_op(left, right, mul)
    898904   
    899905    def __imul__(left, right):
    900906        if have_same_parent(left, right):
    901907             raise TypeError
    902908        # Always do this
    903909        global coercion_model
    904         return coercion_model.bin_op_c(left, right, imul)
     910        return coercion_model.bin_op(left, right, imul)
    905911   
    906912    cdef ModuleElement _multiply_by_scalar(self, right):
    907913        # self * right,  where right need not be a ring element in the base ring
    cdef class MonoidElement(Element): 
    10811087        if have_same_parent(left, right):
    10821088            return (<MonoidElement>left)._mul_c(<MonoidElement>right)
    10831089        try:
    1084             return coercion_model.bin_op_c(left, right, mul)
     1090            return coercion_model.bin_op(left, right, mul)
    10851091        except TypeError, msg:
    10861092            if isinstance(left, (int, long)) and left==1:
    10871093                return right
    cdef class MultiplicativeGroupElement(Mo 
    11981204        if have_same_parent(left, right):
    11991205            return left._div_(right)
    12001206        global coercion_model
    1201         return coercion_model.bin_op_c(left, right, div)
     1207        return coercion_model.bin_op(left, right, div)
    12021208   
    12031209    cdef MultiplicativeGroupElement _div_c(self, MultiplicativeGroupElement right):
    12041210        """
    cdef class RingElement(ModuleElement): 
    13731379            else:
    13741380                return _mul_c(<RingElement>self, <RingElement>right)
    13751381        global coercion_model
    1376         return coercion_model.bin_op_c(self, right, mul)
     1382        return coercion_model.bin_op(self, right, mul)
    13771383       
    13781384    cdef RingElement _mul_c(self, RingElement right):
    13791385        """
    cdef class RingElement(ModuleElement): 
    14111417                return _mul_c(<RingElement>left, <RingElement>right)
    14121418
    14131419        global coercion_model
    1414         return coercion_model.bin_op_c(left, right, imul)
     1420        return coercion_model.bin_op(left, right, imul)
    14151421       
    14161422    def _imul_(self, right):
    14171423        return self._imul_c_impl(right)
    cdef class RingElement(ModuleElement): 
    14671473    def __truediv__(self, right):
    14681474        # in sage all divs are true
    14691475        if not PY_TYPE_CHECK(self, Element):
    1470             return coercion_model.bin_op_c(self, right, div)
     1476            return coercion_model.bin_op(self, right, div)
    14711477        return self.__div__(right)
    14721478
    14731479    def __div__(self, right):
    cdef class RingElement(ModuleElement): 
    14811487            else:
    14821488                return _div_c(<RingElement>self, <RingElement>right)
    14831489        global coercion_model
    1484         return coercion_model.bin_op_c(self, right, div)
     1490        return coercion_model.bin_op(self, right, div)
    14851491           
    14861492
    14871493    cdef RingElement _div_c(self, RingElement right):
    cdef class RingElement(ModuleElement): 
    15261532            else:
    15271533                return _div_c(<RingElement>self, <RingElement>right)
    15281534        global coercion_model
    1529         return coercion_model.bin_op_c(self, right, idiv)
     1535        return coercion_model.bin_op(self, right, idiv)
    15301536
    15311537    def _idiv_(self, right):
    15321538        return self._idiv_c_impl(right)
    cdef class Vector(ModuleElement): 
    17171723            return (<Vector>left)._dot_product_c(<Vector>right)
    17181724        # Always do this
    17191725        global coercion_model
    1720         return coercion_model.bin_op_c(left, right, imul)
     1726        return coercion_model.bin_op(left, right, imul)
    17211727
    17221728   
    17231729    def __mul__(left, right):
    cdef class Vector(ModuleElement): 
    18861892            return (<Vector>left)._dot_product_c(<Vector>right)
    18871893        # Always do this
    18881894        global coercion_model
    1889         return coercion_model.bin_op_c(left, right, imul)
     1895        return coercion_model.bin_op(left, right, imul)
    18901896   
    18911897    cdef Element _dot_product_c(Vector left, Vector right):
    18921898        if left._degree != right._degree:
    18931899            raise TypeError, "incompatible degrees"
    18941900        elif left._parent._base is not right._parent._base:
    18951901            global coercion_model
    1896             left, right = coercion_model.canonical_coercion_c(left, right)
     1902            left, right = coercion_model.canonical_coercion(left, right)
    18971903        if HAS_DICTIONARY(left):
    18981904            return left._dot_product(right)
    18991905        else:
    cdef class Vector(ModuleElement): 
    19101916            raise TypeError, "incompatible degrees"
    19111917        elif left._parent._base is not right._parent._base:
    19121918            global coercion_model
    1913             left, right = coercion_model.canonical_coercion_c(left, right)
     1919            left, right = coercion_model.canonical_coercion(left, right)
    19141920        if HAS_DICTIONARY(left):
    19151921            return left._pairwise_product(right)
    19161922        else:
    cdef class Matrix(AlgebraElement): 
    19781984            return (<Matrix>left)._matrix_times_matrix_c_impl(<Matrix>right)
    19791985        else:
    19801986            global coercion_model
    1981             return coercion_model.bin_op_c(left, right, imul)
     1987            return coercion_model.bin_op(left, right, imul)
    19821988   
    19831989    def __mul__(left, right):
    19841990        """
    cdef class Matrix(AlgebraElement): 
    21462152            return (<Matrix>left)._matrix_times_matrix_c_impl(<Matrix>right)
    21472153        else:
    21482154            global coercion_model
    2149             return coercion_model.bin_op_c(left, right, mul)
     2155            return coercion_model.bin_op(left, right, mul)
    21502156       
    21512157    cdef Vector _vector_times_matrix_c_impl(matrix_right, Vector vector_left):
    21522158        raise TypeError
    cdef class PrincipalIdealDomainElement(D 
    21942200        Returns the least common multiple of self and right.
    21952201        """
    21962202        if not PY_TYPE_CHECK(right, Element) or not ((<Element>right)._parent is self._parent):
    2197             return coercion_model.bin_op_c(self, right, lcm)
     2203            return coercion_model.bin_op(self, right, lcm)
    21982204        return self._lcm(right)
    21992205
    22002206    def gcd(self, right):
    cdef class PrincipalIdealDomainElement(D 
    22022208        Returns the gcd of self and right, or 0 if both are 0.
    22032209        """
    22042210        if not PY_TYPE_CHECK(right, Element) or not ((<Element>right)._parent is self._parent):
    2205             return coercion_model.bin_op_c(self, right, gcd)
     2211            return coercion_model.bin_op(self, right, gcd)
    22062212        return self._gcd(right)
    22072213
    22082214    def xgcd(self, right):
    cdef class PrincipalIdealDomainElement(D 
    22172223        obtain minimal cofactors.
    22182224        """
    22192225        if not PY_TYPE_CHECK(right, Element) or not ((<Element>right)._parent is self._parent):
    2220             return coercion_model.bin_op_c(self, right, xgcd)
     2226            return coercion_model.bin_op(self, right, xgcd)
    22212227        return self._xgcd(right)
    22222228
    22232229
    include "coerce.pxi" 
    26942700#
    26952701#################################################################################
    26962702
    2697 def canonical_coercion(x, y):
     2703cpdef canonical_coercion(x, y):
    26982704    """
    26992705    canonical_coercion(x,y) is what is called before doing an
    27002706    arithmetic operation between x and y.  It returns a pair (z,w)
    def canonical_coercion(x, y): 
    27092715        [0 1])
    27102716    """
    27112717    global coercion_model
    2712     return coercion_model.canonical_coercion_c(x,y)
     2718    return coercion_model.canonical_coercion(x,y)
    27132719
    2714 def canonical_base_coercion(x, y):
     2720cpdef bin_op(x, y, op):
    27152721    global coercion_model
    2716     return coercion_model.canonical_base_coercion(x,y)
     2722    return coercion_model.bin_op(x,y,op)
    27172723
    2718 def bin_op(x, y, op):
    2719     global coercion_model
    2720     return coercion_model.bin_op_c(x,y,op)
    2721 
    2722    
    2723 
    2724 cdef bin_op_c(x, y, op):
    2725     """
    2726     Compute x op y, where coercion of x and y works according to
    2727     SAGE's coercion rules.
    2728 
    2729     AUTHOR:
    2730 
    2731         Gonzalo Tornaria (2007-06-20) - write test cases and fix them
    2732 
    2733     TEST CASES:
    2734 
    2735         sage: x, y = var('x, y')
    2736         sage: parent(ZZ[x](x) / ZZ(2))
    2737         Univariate Polynomial Ring in x over Rational Field
    2738         sage: parent(QQ(1/2)+ZZ[x](x))
    2739         Univariate Polynomial Ring in x over Rational Field
    2740         sage: parent(QQ(1/2)*ZZ[x](x))
    2741         Univariate Polynomial Ring in x over Rational Field
    2742         sage: parent(ZZ[x](x) / QQ(2))
    2743         Univariate Polynomial Ring in x over Rational Field
    2744         sage: parent(Mod(1,5)+ZZ[x](x))
    2745         Univariate Polynomial Ring in x over Ring of integers modulo 5
    2746         sage: parent(Mod(1,5)*ZZ[x](x))
    2747         Univariate Polynomial Ring in x over Ring of integers modulo 5
    2748         sage: parent(ZZ[x](x) / Mod(1,5))
    2749         Univariate Polynomial Ring in x over Ring of integers modulo 5
    2750         sage: parent(QQ(1/2) + Mod(1,5))
    2751         Traceback (most recent call last):
    2752         ...
    2753         TypeError: unsupported operand parent(s) for '+': 'Rational Field' and 'Ring of integers modulo 5'
    2754         sage: parent(QQ(1/2) * Mod(1,5))
    2755         Traceback (most recent call last):
    2756         ...
    2757         TypeError: unsupported operand parent(s) for '*': 'Rational Field' and 'Ring of integers modulo 5'
    2758         sage: parent(ZZ[x](x)+ZZ[y](y))
    2759         Traceback (most recent call last):
    2760         ...
    2761         TypeError: unsupported operand parent(s) for '+': 'Univariate Polynomial Ring in x over Integer Ring' and 'Univariate Polynomial Ring in y over Integer Ring'
    2762         sage: parent(ZZ[x](x)*ZZ[y](y))
    2763         Traceback (most recent call last):
    2764         ...
    2765         TypeError: unsupported operand parent(s) for '*': 'Univariate Polynomial Ring in x over Integer Ring' and 'Univariate Polynomial Ring in y over Integer Ring'
    2766         sage: parent(ZZ[x](x)+QQ[y](y))
    2767         Traceback (most recent call last):
    2768         ...
    2769         TypeError: unsupported operand parent(s) for '+': 'Univariate Polynomial Ring in x over Integer Ring' and 'Univariate Polynomial Ring in y over Rational Field'
    2770         sage: parent(ZZ[x](x)*QQ[y](y))
    2771         Traceback (most recent call last):
    2772         ...
    2773         TypeError: unsupported operand parent(s) for '*': 'Univariate Polynomial Ring in x over Integer Ring' and 'Univariate Polynomial Ring in y over Rational Field'
    2774         sage: parent(QQ[x](x)+ZZ[y](y))
    2775         Traceback (most recent call last):
    2776         ...
    2777         TypeError: unsupported operand parent(s) for '+': 'Univariate Polynomial Ring in x over Rational Field' and 'Univariate Polynomial Ring in y over Integer Ring'
    2778         sage: parent(QQ[x](x)*ZZ[y](y))
    2779         Traceback (most recent call last):
    2780         ...
    2781         TypeError: unsupported operand parent(s) for '*': 'Univariate Polynomial Ring in x over Rational Field' and 'Univariate Polynomial Ring in y over Integer Ring'
    2782         sage: parent(QQ(1/2)+matrix(ZZ,2,2,[1,2,3,4]))
    2783         Full MatrixSpace of 2 by 2 dense matrices over Rational Field
    2784         sage: parent(QQ(1/2)*matrix(ZZ,2,2,[1,2,3,4]))
    2785         Full MatrixSpace of 2 by 2 dense matrices over Rational Field
    2786         sage: parent(QQ[x](1/2)+matrix(ZZ[x],2,2,[1,2,3,4]))
    2787         Full MatrixSpace of 2 by 2 dense matrices over Univariate Polynomial Ring in x over Rational Field
    2788         sage: parent(QQ[x](1/2)*matrix(ZZ[x],2,2,[1,2,3,4]))
    2789         Full MatrixSpace of 2 by 2 dense matrices over Univariate Polynomial Ring in x over Rational Field
    2790         sage: parent(QQ(1/2)+matrix(ZZ[x],2,2,[1,2,3,4]))
    2791         Full MatrixSpace of 2 by 2 dense matrices over Univariate Polynomial Ring in x over Rational Field
    2792         sage: parent(matrix(ZZ[x],2,2,[1,2,3,4])+QQ(1/2))
    2793         Full MatrixSpace of 2 by 2 dense matrices over Univariate Polynomial Ring in x over Rational Field
    2794         sage: parent(QQ(1/2)*matrix(ZZ[x],2,2,[1,2,3,4]))
    2795         Full MatrixSpace of 2 by 2 dense matrices over Univariate Polynomial Ring in x over Rational Field
    2796         sage: parent(matrix(ZZ[x],2,2,[1,2,3,4])*QQ(1/2))
    2797         Full MatrixSpace of 2 by 2 dense matrices over Univariate Polynomial Ring in x over Rational Field
    2798     """
    2799     # Try canonical element coercion.
    2800     try:
    2801         x1, y1 = canonical_coercion_c(x, y)
    2802         return op(x1,y1)       
    2803     except TypeError, msg:
    2804         #print msg  # this can be useful for debugging.
    2805         if op is add or op is sub:
    2806             return addsub_op_c(x, y, op)
    2807         if op is mul or op is div:
    2808             return muldiv_op_c(x, y, op)
    2809         raise TypeError, arith_error_message(x,y,op)
    2810 
    2811 cdef addsub_op_c(x, y, op):
    2812 
    2813     # Try base extending one object by the parent of the other object
    2814     # raise an error if both are defined
    2815 
    2816     nr = 0
    2817     if  PY_TYPE_CHECK(x, RingElement):
    2818         try:
    2819             val = op(x, y.base_extend_recursive((<RingElement>x)._parent))
    2820             nr += 1
    2821         except (TypeError, AttributeError), msg:
    2822             pass
    2823     # Also try to base extending the left object by the parent of the right
    2824     if  PY_TYPE_CHECK(y, RingElement):
    2825         try:
    2826             val = op(x.base_extend_recursive((<RingElement>y)._parent), y)
    2827             nr += 1
    2828         except (TypeError, AttributeError), msg:
    2829             pass
    2830     if nr == 1:
    2831         return val
    2832 
    2833     raise TypeError, arith_error_message(x,y,op)
    2834 
    2835 
    2836 cdef muldiv_op_c(x, y, op):
    2837     # If the op is multiplication, then some other algebra multiplications
    2838     # may be defined
    2839 
    2840     if op is div and PY_TYPE_CHECK(y, RingElement):
    2841         y = y.__invert__()
    2842    
    2843     # 2. Try scalar multiplication.
    2844     # No way to multiply x and y using the ``coerce into a canonical
    2845     # parent'' rule.
    2846     # The next rule to try is scalar multiplication by coercing
    2847     # into the base ring.
    2848     cdef bint x_is_modelt, y_is_modelt
    2849 
    2850     y_is_modelt = PY_TYPE_CHECK(y, ModuleElement)
    2851     if y_is_modelt:
    2852         # First try to coerce x into the base ring of y if y is an element.
    2853         try:
    2854             R = (<ModuleElement> y)._parent._base
    2855             if R is None:
    2856                 raise RuntimeError, "base of '%s' must be set to a ring (but it is None)!"%((<ModuleElement> y)._parent)
    2857             x = (<Parent>R)._coerce_c(x)
    2858             return (<ModuleElement> y)._rmul_c(x)     # the product x * y
    2859         except TypeError, msg:
    2860             pass
    2861 
    2862     x_is_modelt = PY_TYPE_CHECK(x, ModuleElement)
    2863     if x_is_modelt:
    2864         # That did not work.  Try to coerce y into the base ring of x.
    2865         try:
    2866             R = (<ModuleElement> x)._parent._base
    2867             if R is None:
    2868                 raise RuntimeError, "base of '%s' must be set to a ring (but it is None)!"%((<ModuleElement> x)._parent)           
    2869             y = (<Parent> R)._coerce_c(y)
    2870             return (<ModuleElement> x)._lmul_c(y)    # the product x * y
    2871         except TypeError:
    2872             pass
    2873 
    2874     if y_is_modelt and x_is_modelt:
    2875         # 3. Both canonical coercion failed, but both are module elements.
    2876         # Try base extending the right object by the parent of the left
    2877 
    2878         try:
    2879             return addsub_op_c(x, y, mul)
    2880         except TypeError:
    2881             pass
    2882 
    2883 #        ## TODO -- WORRY -- only unambiguous if one succeeds!
    2884 #        nr = 0
    2885 #        if  PY_TYPE_CHECK(x, RingElement):
    2886 #            try:
    2887 #                val = operator.mul(x, y.base_extend((<RingElement>x)._parent))
    2888 #                nr += 1
    2889 #            except (TypeError, AttributeError), msg:
    2890 #                pass
    2891 #        # Also try to base extending the left object by the parent of the right
    2892 #        if  PY_TYPE_CHECK(y, RingElement):
    2893 #            try:
    2894 #                val =  operator.mul(x.base_extend((<Element>y)._parent), y)
    2895 #                nr += 1
    2896 #            except (TypeError, AttributeError), msg:
    2897 #                pass
    2898 #        if nr == 1:
    2899 #            return val
    2900 
    2901     # 4. Try _l_action or _r_action.
    2902     # Test to see if an _r_action or _l_action is
    2903     # defined on either side.
    2904     try:
    2905         return x._l_action(y)
    2906     except (AttributeError, TypeError):   
    2907         pass
    2908     try:
    2909         return y._r_action(x)
    2910     except (AttributeError, TypeError):
    2911         pass
    2912 
    2913     raise TypeError, arith_error_message(x,y,op)
    29142724
    29152725def coerce(Parent p, x):
    29162726    try:
    def coerce_cmp(x,y): 
    29222732    global coercion_model
    29232733    cdef int c
    29242734    try:
    2925         x, y = coercion_model.canonical_coercion_c(x, y)
     2735        x, y = coercion_model.canonical_coercion(x, y)
    29262736        return cmp(x,y)       
    29272737    except TypeError:
    29282738        c = cmp(type(x), type(y))
    cdef class CoercionModel: 
    29462756
    29472757import coerce
    29482758cdef CoercionModel coercion_model = coerce.CoercionModel_cache_maps()
    2949 
    2950 # for now while I'm merging in base extension code
    2951 cdef canonical_coercion_c(x, y):
    2952     return coercion_model.canonical_coercion_c(x,y)
    29532759
    29542760def get_coercion_model():
    29552761    """
  • sage/structure/parent_old.pyx

    diff -r 9f227080425a -r 42946824190e sage/structure/parent_old.pyx
    a b cdef class Parent(parent.Parent): 
    229229        from sage.categories.action import Action, PrecomposedAction
    230230        from sage.categories.morphism import Morphism
    231231        from sage.categories.homset import Hom
    232         from coerce import LeftModuleAction, RightModuleAction
     232        from coerce_actions import LeftModuleAction, RightModuleAction
    233233        cdef Parent R
    234234        for action in self._action_list:
    235235            if PY_TYPE_CHECK(action, Action):
    cdef class Parent(parent.Parent): 
    266266               
    267267
    268268        if op is operator.mul and PY_TYPE_CHECK(S, Parent):
    269             from coerce import LeftModuleAction, RightModuleAction, LAction, RAction
     269            from coerce_actions import LeftModuleAction, RightModuleAction, LAction, RAction
    270270            # Actors define _l_action_ and _r_action_
    271271            # Acted-on elements define _lmul_ and _rmul_
    272272           
  • sage/structure/wrapper_parent.pxd

    diff -r 9f227080425a -r 42946824190e sage/structure/wrapper_parent.pxd
    a b from sage.structure.parent cimport Paren 
    11from sage.structure.parent cimport Parent
    22from sage.categories.morphism cimport Morphism
    33from sage.structure.element cimport AlgebraElement, Element, ModuleElement, RingElement
    4 
    5 cdef class StealMorphism(Morphism):
    6     cdef Element _call_c_impl(self, Element x)
    74
    85cdef class WrapperParent_model0(Parent):
    96    cdef Parent R
    cdef class WrapperParent_model1(Parent): 
    1310
    1411cdef class WrapperElement(AlgebraElement):
    1512    cdef Element val
    16     cdef _richcmp_c_impl(left, Element right, int op)
    17     cdef int _cmp_c_impl(left, Element right) except -2
    18     cdef base_extend_c_impl(self, Parent R)
    19     cdef ModuleElement _add_c_impl(self, ModuleElement right)
    20     cdef ModuleElement _sub_c_impl(self, ModuleElement right)
    21     cdef ModuleElement _neg_c_impl(self)                   
    22     cdef ModuleElement _lmul_c_impl(self, RingElement right)
    23     cdef ModuleElement _rmul_c_impl(self, RingElement left)
    2413    cdef RingElement coerce_to_base_ring(self, x)
    25     cdef ModuleElement _lmul_nonscalar_c_impl(left, right)
    26     cdef ModuleElement _rmul_nonscalar_c_impl(right, left)
    27     cdef RingElement _mul_c_impl(self, RingElement right)
    28     cdef RingElement _div_c_impl(self, RingElement right)
  • sage/structure/wrapper_parent.pyx

    diff -r 9f227080425a -r 42946824190e sage/structure/wrapper_parent.pyx
    a b cdef class StealMorphism(Morphism): 
    5959    def _repr_type(self):
    6060        return "Steal"
    6161
    62     cdef Element _call_c_impl(self, Element x):
     62    cpdef Element _call_(self, x):
    6363        """
    6464        Applies this morphism to the element x.
    6565