#5280: [with patch, positive review] problem with a subposet coming from an
order_filter
---------------------------+------------------------------------------------
 Reporter:  jhpalmieri     |       Owner:  somebody      
     Type:  defect         |      Status:  new           
 Priority:  minor          |   Milestone:  sage-combinat 
Component:  combinatorics  |    Keywords:                
 Reviewer:                 |      Author:  Franco Saliola
   Merged:                 |  
---------------------------+------------------------------------------------

Comment(by saliola):

 Replying to rlm:

 > Why is {{{__cmp__}}} returning 1 when elements are incomparable?
 Shouldn't it be raising an error instead?

 Here are a couple of reasons why it shouldn't.

 (1) {{{__cmp__}}} should never raise an error, otherwise you won't be able
 to
 sort a list of elements:

 {{{
 sage: class C(object):
 ...       def __cmp__(self, other):
 ...           raise ValueError, 'elements are incomparable'

 sage: sorted([C(), C()])
 ------------------------------------------------------------
 Traceback (most recent call last):
   File "<ipython console>", line 1, in <module>
   File "<ipython console>", line 3, in __cmp__
 ValueError: elements are incomparable
 }}}

 (2) All the rich comparisons have been implemented for {{{PosetElement}}},
 so
 {{{x<y}}} is handled by {{{x.__lt__(y)}}}. That is, the answer will be
 correct.


 So you might wonder why {{{__cmp__}}} even needs to be implemented.
 Shouldn't
 {{{cmp}}} just use the rich comparison methods to determine its value?

 In theory, yes. But {{{PosetElement}}} inherits from {{{Element}}}, which
 defines {{{__cmp__}}}. It seems to be that since {{{__cmp__}}} is not the
 default implementation ({{{object.__cmp__}}}), the {{{cmp}}} function
 ignores
 all the rich comparison operations and calls {{{__cmp__}}} directly. See
 the
 following example, which was adapted from
 [http://docs.sympy.org/_sources/python-comparisons.txt].

 {{{
 sage: class C_without_cmp(SageObject):
 ...       def __init__(self, a):
 ...           self.a = a
 ...       def __repr__(self):
 ...           return str(self.a)
 ...       def __eq__(self, o):
 ...           print "%s.__eq__(%s)" % (self.a, o.a)
 ...           return NotImplemented
 ...       def __ne__(self, o):
 ...           print "%s.__ne__(%s)" % (self.a, o.a)
 ...           return NotImplemented
 ...       def __lt__(self, o):
 ...           print "%s.__lt__(%s)" % (self.a, o.a)
 ...           return NotImplemented
 ...       def __le__(self, o):
 ...           print "%s.__le__(%s)" % (self.a, o.a)
 ...           return NotImplemented
 ...       def __gt__(self, o):
 ...           print "%s.__gt__(%s)" % (self.a, o.a)
 ...           return NotImplemented
 ...       def __ge__(self, o):
 ...           print "%s.__ge__(%s)" % (self.a, o.a)
 ...           return NotImplemented

 # cmp uses the rich comparison methods if no __cmp__ is found
 sage: a = C_without_cmp("a"); b = C_without_cmp("b")
 sage: cmp(a,b)
 a.__eq__(b)
 b.__eq__(a)
 b.__eq__(a)
 a.__eq__(b)
 a.__lt__(b)
 b.__gt__(a)
 b.__gt__(a)
 a.__lt__(b)
 a.__gt__(b)
 b.__lt__(a)
 b.__lt__(a)
 a.__gt__(b)
 1

 sage: class C_with_cmp(C_without_cmp):
 ...       def __cmp__(self, o):
 ...           print "%s.__cmp__(%s)" % (self.a, o.a)
 ...           return NotImplemented

 # cmp uses __cmp__, ignoring the rich comparison methods if it is defined
 sage: a = C_with_cmp("a"); b = C_with_cmp("b")
 sage: cmp(a,b)
 a.__cmp__(b)
 b.__cmp__(a)
 -1
 }}}

 This leads to the following error for posets, which is what this ticket is
 about.

 {{{
 sage: P = Poset([[1,2],[3],[3],[]])
 sage: sorted(P)
 [0, 1, 2, 3]
 sage: sorted(P, 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:6062)
   File "element.pyx", line 561, in sage.structure.element.Element._cmp
 (sage/structure/element.c:5133)
   File "element.pyx", line 663, in
 sage.structure.element.Element._cmp_c_impl (sage/structure/element.c:6237)
 NotImplementedError: BUG: sort algorithm for elements of 'Finite poset
 containing 4 elements' not implemented

 >
 
/home/saliola/Applications/sage-4.0.2-busted/local/bin/element.pyx(663)sage.structure.element.Element._cmp_c_impl
 (sage/structure/element.c:6237)()
 }}}

 So I implemented {{{__cmp__}}} for {{{PosetElement}}}.

 Are these satisfactory reasons?

-- 
Ticket URL: <http://trac.sagemath.org/sage_trac/ticket/5280#comment:9>
Sage <http://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 post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/sage-trac?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to