#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: Vincent Delecroix | Reviewers:
Report Upstream: N/A | Work issues:
Branch: | Commit:
u/vdelecroix/18305 | 7749fe882a88d0978413e9c58b9d2660e0de23a6
Dependencies: #17890 | Stopgaps:
-------------------------------------+-------------------------------------
Changes (by {'newvalue': u'Vincent Delecroix', 'oldvalue': ''}):
* commit: => 7749fe882a88d0978413e9c58b9d2660e0de23a6
* dependencies: #17890, #18303 => #17890
* branch: => u/vdelecroix/18305
* author: => Vincent Delecroix
Old 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
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__`
With the current branch:
- make everything needed for rich comparisons available from Python: the
branch adds `py_rich_to_bool` and `py_rich_to_bool_sgn` and `op_EQ`,
`op_NE`, `op_LT`, `op_LE`, `op_GE` and `op_GT` in
`sage.structure.sage_object`
- six elements are modified to use either `_richcmp_` or `_cmp_` instead
of `__eq__`/`__ne__`/`__lt__`, etc (and #18303 takes care of
`sage.rings.qqbar`)
Still to do or discuss:
- update the documentation in `sage.structure.Element`
- decide what should we do with respect to `x < y` when this makes no
sense (some elements do raise a `TypeError`)
- remove the test in the default `Element._cmp_` since an element
implementing `__cmp__` wants to avoid coercion (?)
--
Comment:
I was able to modify six Python elements to use coercion for their
comparisons. The code is simplified.
In order to do that, I had to modify slightly the implementation of
`_richcmp` to not catch a `TypeError` when it calls `_richcmp_`. We should
precise what elements should do when `==` and `!=` makes sense but not
`<`, `<=`, `>=`, `>`. Is `TypeError` appropriate?
Vincent
----
Last 10 new commits:
||[http://git.sagemath.org/sage.git/commit/?id=04570b3dfd194fea8c2437c8717c3dfb8704b4af
04570b3]||{{{Fix bad doctest in etaproducts}}}||
||[http://git.sagemath.org/sage.git/commit/?id=3976f2cc4ec59c9f978d9d02c7f697c81025c4ef
3976f2c]||{{{Add pointers for special uses of __richcmp__}}}||
||[http://git.sagemath.org/sage.git/commit/?id=ce7e4f1ee5d3d4be67c9698c0b8ac35dad6f06a7
ce7e4f1]||{{{merge u/jdemeyer/ticket/17890 in Sage-6.7.beta3}}}||
||[http://git.sagemath.org/sage.git/commit/?id=203820b5abc9510e0071d26a395f58b055420a3e
203820b]||{{{Trac 18305: py_rich_to_bool(_sgn) and operators}}}||
||[http://git.sagemath.org/sage.git/commit/?id=c6d3e235b15e56386e461f66a110a105f123ac5d
c6d3e23]||{{{Trac 18305: use `_cmp_` for alternating sign matrices}}}||
||[http://git.sagemath.org/sage.git/commit/?id=f3e46e39b80dc63100e2f1e05e6e707d7f7a4357
f3e46e3]||{{{Trac 18305: use _cmp_ for ideals of finite dimensional
algebras}}}||
||[http://git.sagemath.org/sage.git/commit/?id=b0c39b0b8acd10c88c6f4955ae78ca07fdace704
b0c39b0]||{{{Trac 18305: _richcmp_ for indexed monoids and groups}}}||
||[http://git.sagemath.org/sage.git/commit/?id=220a1406914312030bfb6dea1994f91247d6f69e
220a140]||{{{Trac 18305: _richcmp_ for Newton polygons}}}||
||[http://git.sagemath.org/sage.git/commit/?id=042b6fe0fae411b924a09521aab7163731c2e588
042b6fe]||{{{Trac 18305: modify Element._richcmp so that _richcmp_ can
raise errors}}}||
||[http://git.sagemath.org/sage.git/commit/?id=7749fe882a88d0978413e9c58b9d2660e0de23a6
7749fe8]||{{{Trac 18305: _richcmp_ for `finite dimensional algebra
elements}}}||
--
Ticket URL: <http://trac.sagemath.org/ticket/18305#comment:4>
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.