Changeset 3143:c76c107c050d


Ignore:
Timestamp:
02/26/07 16:11:16 (6 years ago)
Author:
William Stein <wstein@…>
Branch:
default
Parents:
3138:d41bd3f19adc (diff), 3142:acd8f46ec463 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

merge

Files:
2 edited

Legend:

Unmodified
Added
Removed
  • sage/structure/element.pyx

    r3074 r3143  
    315315        return PyBool_FromLong(self == self._parent(0)) 
    316316 
    317     def _richcmp_(left, right, op): 
    318         return left._richcmp(right, op) 
    319  
    320     cdef _richcmp(left, right, int op): 
    321         """ 
    322         Compare left and right.  
     317    def _cmp_(left, right): 
     318        return left._cmp(right) 
     319 
     320    cdef _cmp(left, right): 
     321        """ 
     322        Compare left and right. 
    323323        """ 
    324324        cdef int r 
     
    326326            try: 
    327327                _left, _right = canonical_coercion_c(left, right) 
    328                 r = cmp(_left, _right) 
     328                if PY_IS_NUMERIC(_left): 
     329                    return cmp(_left, _right) 
     330                else: 
     331                    return _left._cmp_(_right) 
    329332            except TypeError: 
    330333                r = cmp(type(left), type(right)) 
    331334                if r == 0: 
    332335                    r = -1 
     336                return r 
     337        else: 
     338            return left._cmp_c_impl(right) 
     339 
     340    def _richcmp_(left, right, op): 
     341        return left._richcmp(right, op) 
     342 
     343    cdef _richcmp(left, right, int op): 
     344        """ 
     345        Compare left and right, according to the comparison operator op. 
     346        """ 
     347        cdef int r 
     348        if not have_same_parent(left, right): 
     349            try: 
     350                _left, _right = canonical_coercion_c(left, right) 
     351                if PY_IS_NUMERIC(_left): 
     352                    r = cmp(_left, _right) 
     353                else: 
     354                    return _left._richcmp_(_right, op) 
     355            except TypeError: 
     356                r = cmp(type(left), type(right)) 
     357                if r == 0: 
     358                    r = -1 
    333359        else: 
    334360            if HAS_DICTIONARY(left):   # fast check 
    335361                r = left.__cmp__(right) 
    336362            else: 
    337                 r = left._cmp_c_impl(right) 
     363                return left._richcmp_c_impl(right, op) 
    338364 
    339365        return left._rich_to_bool(op, r) 
     
    357383    # your subclasses, in order for it to take advantage of the 
    358384    # above generic comparison code.  You must also define 
    359     # _cmp_c_impl. 
     385    # either _cmp_c_impl (if your subclass is totally ordered), 
     386    # _richcmp_c_impl (if your subclass is partially ordered), or both 
     387    # (if your class has both a total order and a partial order; 
     388    # then the total order will be available with cmp(), and the partial 
     389    # order will be available with the relation operators; in this case 
     390    # you must also define __cmp__ in your subclass). 
    360391    # This is simply how Python works. 
    361392    # 
     
    367398        return (<Element>left)._richcmp(right, op) 
    368399     
     400    #################################################################### 
     401    # If your subclass has both a partial order (available with the 
     402    # relation operators) and a total order (available with cmp()), 
     403    # you **must** put the following in your subclass. 
     404    #  
     405    # Note that in this case the total order defined by cmp() will 
     406    # not properly respect coercions. 
     407    #################################################################### 
     408    def __cmp__(left, right): 
     409        return (<Element>left)._cmp(right) 
     410 
     411    cdef _richcmp_c_impl(left, Element right, int op): 
     412        return left._rich_to_bool(op, left._cmp_c_impl(right)) 
     413 
    369414    cdef int _cmp_c_impl(left, Element right) except -2: 
    370415        ### For derived SAGEX code, you *MUST* ALSO COPY the __richcmp__ above 
    371416        ### into your class!!!  For Python code just use __cmp__. 
    372417        raise NotImplementedError, "BUG: sort algorithm for elements of '%s' not implemented"%right.parent() 
    373  
    374     def __cmp__(left, right): 
    375         return left._cmp_c_impl(right)   # default 
    376418 
    377419def is_ModuleElement(x): 
  • sage/structure/element.pyx

    r3141 r3143  
    18051805 
    18061806def canonical_coercion(x, y): 
     1807    """ 
     1808    canonical_coercion(x,y) is what is called before doing an 
     1809    arithmetic operation between x and y.  It returns a pair (z,w) 
     1810    such that z is got from x and w from y via canonical coercion and 
     1811    the parents of z and w are identical. 
     1812 
     1813    EXAMPLES: 
     1814        sage: A = Matrix([[0,1],[1,0]]) 
     1815        sage: canonical_coercion(A,1) 
     1816        ([0 1] 
     1817        [1 0], [1 0] 
     1818        [0 1]) 
     1819    """ 
    18071820    return canonical_coercion_c(x,y) 
    18081821 
Note: See TracChangeset for help on using the changeset viewer.