#18305: Element comparison for Python classes
----------------------------------+------------------------
Reporter: vdelecroix | Owner:
Type: enhancement | Status: new
Priority: major | Milestone: sage-6.7
Component: coercion | Resolution:
Keywords: | Merged in:
Authors: | Reviewers:
Report Upstream: N/A | Work issues:
Branch: | Commit:
Dependencies: #17890, #18303 | Stopgaps:
----------------------------------+------------------------
Old description:
> In #17890 the comparisons of elements (i.e. through `__cmp__` and
> `__richcmp__`) has been simplified. In order for elements to take benefit
> of the coercion framekwork they should either implement
> `_cmp_(left,right)` and/or `_richcmp_(left,right,op)`.
>
> In particular, it is discouraged to implement `__eq__` and `__ne__` if
> coercion has to be involved. It would also be simpler if no element
> implements `__cmp__`.
>
> In this ticket:
> - make everything needed for rich comparisons available from Python
> (i.e. `Py_EQ`, `Py_NE`, etc as well as `rich_to_bool/rich_to_bool_sgn`)
> - we check the sage libraries for occurrences of
> `__eq__`/`__ne__`/`__cmp__` in elements and see whether these can be
> simplified
> - #18303 takes care of `sage.rings.qqbar`
> - update the documentation in `sage.structure.Element`
New description:
In #17890 the comparison of elements (i.e. `Element.__cmp__`) and rich
comparison of elements (i.e. `Element.__richcmp__`) have been simplified.
1 .If coercion has to be involved then the Python elements should
implement:
- `_richcmp_(left, right, op)` for rich comparisons (i.e. operators `<`,
`<=`, `==`, `!=`, `>=` and `>=`)
- `_cmp_(left, right)` for comparisons (i.e. when calling `cmp(x,y)`)
In these two methods, it can be assumed that both arguments `left` and
`right` are subclasses of `Element` with the same parent. If `_cmp_` makes
sense it is possible to implement it only. If an element wants to
implement `==` and `!=` only, then it should just raise a
`NotImplementedError` for other operators in `_richcmp_(left,right,op)`.
2 . If comparison needs to not avoid coercion, then:
- rich comparison is possible through `__eq__`, `__ne__`, `__lt__`, etc
(and all of these should be implemented)
- comparison is possible via `__cmp__`
In this ticket:
- make everything needed for rich comparisons available from Python (i.e.
`Py_EQ`, `Py_NE`, etc as well as `rich_to_bool/rich_to_bool_sgn`)
- check the sage libraries for occurrences of `__eq__`/`__ne__`/`__cmp__`
in elements and see whether these can be simplified
- #18303 takes care of `sage.rings.qqbar`
- update the documentation in `sage.structure.Element`
- remove the test in the default `Element._cmp_` since an element
implementing `__cmp__` wants to avoid coercion
--
Comment (by vdelecroix):
Another example: embedded number fields in `RR`. If you want that rich
comparisons coincide with the order on `RR` then `==` and `!=` are cheap
but not `<`, `<=`, `>` and `>=`.
Do you agree with the new description?
--
Ticket URL: <http://trac.sagemath.org/ticket/18305#comment:3>
Sage <http://www.sagemath.org>
Sage: Creating a Viable Open Source Alternative to Magma, Maple, Mathematica,
and MATLAB
--
You received this message because you are subscribed to the Google Groups
"sage-trac" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sage-trac.
For more options, visit https://groups.google.com/d/optout.