Changeset 3143:c76c107c050d
- Timestamp:
- 02/26/07 16:11:16 (6 years ago)
- 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. - Files:
-
- 2 edited
-
sage/structure/element.pyx (modified) (4 diffs)
-
sage/structure/element.pyx (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
sage/structure/element.pyx
r3074 r3143 315 315 return PyBool_FromLong(self == self._parent(0)) 316 316 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. 323 323 """ 324 324 cdef int r … … 326 326 try: 327 327 _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) 329 332 except TypeError: 330 333 r = cmp(type(left), type(right)) 331 334 if r == 0: 332 335 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 333 359 else: 334 360 if HAS_DICTIONARY(left): # fast check 335 361 r = left.__cmp__(right) 336 362 else: 337 r = left._cmp_c_impl(right)363 return left._richcmp_c_impl(right, op) 338 364 339 365 return left._rich_to_bool(op, r) … … 357 383 # your subclasses, in order for it to take advantage of the 358 384 # 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). 360 391 # This is simply how Python works. 361 392 # … … 367 398 return (<Element>left)._richcmp(right, op) 368 399 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 369 414 cdef int _cmp_c_impl(left, Element right) except -2: 370 415 ### For derived SAGEX code, you *MUST* ALSO COPY the __richcmp__ above 371 416 ### into your class!!! For Python code just use __cmp__. 372 417 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) # default376 418 377 419 def is_ModuleElement(x): -
sage/structure/element.pyx
r3141 r3143 1805 1805 1806 1806 def 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 """ 1807 1820 return canonical_coercion_c(x,y) 1808 1821
Note: See TracChangeset
for help on using the changeset viewer.
