On 22 Jun., 11:25, Franco Saliola <sali...@gmail.com> wrote:
> Hello,
>
> Here is a quick description of what is below: Subclasses of Element
> complain that no sorting algorithm is defined even when all the rich
> comparison methods have been implemented. Bug?
>
> In the code sample below, C is a class that inherits from Element and
> implements all the rich comparison methods.
>
> {{{
> sage: class C(sage.structure.element.Element):
> ...       def __init__(self, a):
> ...           self.a = a
> ...       def __repr__(self):
> ...           return str(self.a)
> ...       def __eq__(self, other):
> ...           return self.a == other.a
> ...       def __ne__(self, other):
> ...           return self.a != other.a
> ...       def __lt__(self, other):
> ...           return self.a < other.a
> ...       def __le__(self, other):
> ...           return self.a <= other.a
> ...       def __gt__(self, other):
> ...           return self.a > other.a
> ...       def __ge__(self, other):
> ...           return self.a >= other.a
>
> }}}
>
> In theory, the cmp function should use the rich comparison methods for
> comparisions. However, since __cmp__ has also been implemented (it is
> implemented by Element), cmp bypasses all the rich comparison methods and
> calls __cmp__ directly. This leads to an error since Element requires
> subclasses to define a sorting algorithm:
>
> {{{
> sage: a = C('a')
> sage: b = C('b')
>
> sage: a < b
> True
>
> sage: cmp(a,b)
> ------------------------------------------------------------
> Traceback (most recent call last):
>   File "<ipython console>", line 1, in <module>
>   File "element.pyx", line 648, in
> sage.structure.element.Element.__cmp__ (sage/structure/element.c:6064)
>   File "element.pyx", line 561, in sage.structure.element.Element._cmp
> (sage/structure/element.c:5135)
>   File "element.pyx", line 663, in
> sage.structure.element.Element._cmp_c_impl
> (sage/structure/element.c:6239)
> NotImplementedError: BUG: sort algorithm for elements of 'None' not 
> implemented
>
> }}}
>
> And this leads to problems with sorting via the cmp function:
>
> {{{
> sage: sorted([b,a])
> [a, b]
>
> sage: sorted([b,a], cmp=cmp)
> ------------------------------------------------------------
> Traceback (most recent call last):
>   File "<ipython console>", line 1, in <module>
>   File "element.pyx", line 648, in
> sage.structure.element.Element.__cmp__ (sage/structure/element.c:6064)
>   File "element.pyx", line 561, in sage.structure.element.Element._cmp
> (sage/structure/element.c:5135)
>   File "element.pyx", line 663, in
> sage.structure.element.Element._cmp_c_impl
> (sage/structure/element.c:6239)
> NotImplementedError: BUG: sort algorithm for elements of 'None' not 
> implemented
>
> }}}
>
> One can get around this by implementing __cmp__ as well as the rich
> comparison methods, but I'm wondering whether it might be better for
> Element to check for and use the rich comparison methods before freaking
> out.
>
> Is there a good reason (speed? efficiency?) for this behaviour? Should I
> open a ticket?
>
> (For the record, Nicolas tells me that the new category theory code does
> not fix this issue.)
>
> Franco
>
> --

Some weeks ago I proposed a change to the element.pyx code. Beside all
discussions whether certain objects should be comparable or not, the
standard implementation of richcmp immediately calls __cmp__. This
renders impossible to python classes using both comparisons, the
partial order via __richcmp__ etc. and the total oder via __cmp__.
One solution, which also passes all tests for 4.0.2, is to prefer
_cmp_ and _richcmp_ implementations, which basically is the same
approach used for _add_ etc. . And indeed everything in element.pyx is
written to behave like that except two functions. The following patch
modifies them and admits for example representations of dictionaries
with keys, which have not worked before - the reason why I've started
thinking about this.
http://www.matha.rwth-aachen.de/~martin/element.pyx.patch

May this help, too, Franco?

Martin
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to sage-devel@googlegroups.com
To unsubscribe from this group, send email to 
sage-devel-unsubscr...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sage-devel
URLs: http://www.sagemath.org
-~----------~----~----~----~------~----~------~--~---

Reply via email to