Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Implement RealSet comparisons using __richcmp__
Browse files Browse the repository at this point in the history
  • Loading branch information
jdemeyer committed Jun 16, 2017
1 parent 49b6f76 commit d343d9d
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions src/sage/sets/real_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,17 @@ class RealSet.
- Volker Braun (2013-06-22): Rewrite
"""

########################################################################
#*****************************************************************************
# Copyright (C) 2013 Volker Braun <vbraun.name@gmail.com>
#
# Distributed under the terms of the GNU General Public License (GPL)
# as published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
# http://www.gnu.org/licenses/
########################################################################

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
# http://www.gnu.org/licenses/
#*****************************************************************************

from sage.structure.richcmp import richcmp, richcmp_method
from sage.structure.parent import Parent
from sage.structure.unique_representation import UniqueRepresentation
from sage.categories.sets_cat import Sets
Expand All @@ -70,6 +72,7 @@ class RealSet.
from sage.rings.infinity import infinity, minus_infinity


@richcmp_method
class InternalRealInterval(UniqueRepresentation, Parent):
"""
A real interval.
Expand Down Expand Up @@ -295,7 +298,7 @@ def upper_open(self):
"""
return not self._upper_closed

def __cmp__(self, other):
def __richcmp__(self, other, op):
"""
Intervals are sorted by lower bound, then upper bound
Expand All @@ -321,8 +324,9 @@ def __cmp__(self, other):
sage: RealSet((0, 1),[1, 1],(1, 2))
(0, 2)
"""
return cmp([self._lower, not self._lower_closed, self._upper, self._upper_closed],
[other._lower, not other._lower_closed, other._upper, other._upper_closed])
x = (self._lower, not self._lower_closed, self._upper, self._upper_closed)
y = (other._lower, not other._lower_closed, other._upper, other._upper_closed)
return richcmp(x, y, op)

element_class = LazyFieldElement

Expand Down Expand Up @@ -599,6 +603,7 @@ def contains(self, x):
return False


@richcmp_method
class RealSet(UniqueRepresentation, Parent):

@staticmethod
Expand Down Expand Up @@ -679,7 +684,7 @@ def __init__(self, *intervals):
Parent.__init__(self, category = Sets())
self._intervals = intervals

def __cmp__(self, other):
def __richcmp__(self, other, op):
"""
Intervals are sorted by lower bound, then upper bound
Expand All @@ -700,9 +705,11 @@ def __cmp__(self, other):
sage: I1 == I1
True
"""
if not isinstance(other, RealSet):
return NotImplemented
# note that the interval representation is normalized into a
# unique form
return cmp(self._intervals, other._intervals)
return richcmp(self._intervals, other._intervals, op)

def __iter__(self):
"""
Expand Down

0 comments on commit d343d9d

Please sign in to comment.