Hi Florent,
On 2012-05-08, Florent Hivert <
Florent...@lri.fr> wrote:
> But it is never called. I'd like to have confirmation that adding to all
> vectors classes (following sage/structure/element.pyx line 881 (5.0.rc0))
>
> def __richcmp__(left, right, int op):
> return (<Element>left)._richcmp(right, op)
>
> is the correct solution.
Yes, the following advices given in some comment in
sage/structure/element.pyx are still valid (and, I agree, difficult
to keep in mind):
####################################################################
# For a derived Cython class, you **must** put the following in
# your subclasses, in order for it to take advantage of the
# above generic comparison code. You must also define
# either _cmp_c_impl (if your subclass is totally ordered),
# _richcmp_c_impl (if your subclass is partially ordered), or both
# (if your class has both a total order and a partial order;
# then the total order will be available with cmp(), and the partial
# order will be available with the relation operators; in this case
# you must also define __cmp__ in your subclass).
# This is simply how Python works.
#
# For a *Python* class just define __cmp__ as always.
# But note that when this gets called you can assume that
# both inputs have identical parents.
#
# If your __cmp__ methods are not getting called, verify that the
# canonical_coercion(x,y) is not throwing errors.
#
####################################################################
IIRC, the problem is that (by design of Cython) it is not possible to
inherit __(rich)cmp__ if there is also a __hash__ method. Anyway,
copying the code for __(rich)cmp__ from sage/structure/element.pyx is
necessary, inheritance wouldn't work, as I've learnt the hard way...
And then, _cmp_c_impl or _richcmp_c_impl are supposed to provide the
actual implementation of comparison.
> Removing the offending __richcmp__ in
> FreeModuleElement seems to solve the problem once for all for all rings but
> introduce a lot of other problems.
What other problems emerge? Given the comment from sage/structure/element.pyx, I think
that method should be renamed into _richcmp_c_impl.
> Also it is very likely that the same problem is occuring in different places
> (not only the various vector classes) and I'm not completely sure where to
> look. I'd rather have an automatic way to find all the places where this fails
> and also add the check to TestSuite if possible. Does anyone have a suggestion
> for that ?
I am not sure what you are trying to do. Do you want to detect Cython
classes that inherit from sage.structure.element.Element and do not copy
their __cmp__ method as they should? I am not sure how this could be
done without source inspection.
Cheers,
Simon