Ticket #5891: categories-enumeratedsets-nt.patch

File categories-enumeratedsets-nt.patch, 5.3 KB (added by nthiery, 10 years ago)
  • sage/sets/all.py

    diff --git a/sage/sets/all.py b/sage/sets/all.py
    a b  
    11from set import Set, is_Set, EnumeratedSet
    2 
     2from finite_enumerated_set import FiniteEnumeratedSet
    33from primes import Primes
    44
    55from family import Family
  • new file sage/sets/finite_enumerated_set.py

    diff --git a/sage/sets/finite_enumerated_set.py b/sage/sets/finite_enumerated_set.py
    new file mode 100644
    - +  
     1"""
     2Finite Enumerated Sets
     3"""
     4#*****************************************************************************
     5#  Copyright (C) 2009 Florent Hivert <Florent.Hivert@univ-rouen.fr>
     6#
     7#  Distributed under the terms of the GNU General Public License (GPL)
     8#
     9#    This code is distributed in the hope that it will be useful,
     10#    but WITHOUT ANY WARRANTY; without even the implied warranty of
     11#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     12#    General Public License for more details.
     13#
     14#  The full text of the GPL is available at:
     15#
     16#                  http://www.gnu.org/licenses/
     17#******************************************************************************
     18
     19from sage.structure.parent import Parent
     20from sage.structure.unique_representation import UniqueRepresentation
     21from sage.categories.finite_enumerated_sets import FiniteEnumeratedSets
     22from sage.rings.integer import Integer
     23
     24#################################################################   
     25class FiniteEnumeratedSet(UniqueRepresentation, Parent):
     26    """
     27    The class of finite enumerated set
     28   
     29    Returns the finite enumerated set with elements in ``elements`` where
     30    ``element`` is any (finite) iterable object. There is no check that
     31    ``elements`` does not contains twice the same value so that one can build
     32    multisets.
     33   
     34    EXAMPLES::
     35   
     36        sage: S = FiniteEnumeratedSet([1, 2, 3])
     37        sage: S
     38        {1, 2, 3}
     39        sage: S.list()
     40        [1, 2, 3]
     41        sage: S.cardinality()
     42        3
     43        sage: S.random_element()
     44        1
     45        sage: S.first()
     46        1
     47        sage: S.check()
     48
     49    Note that being and enumerated set, the result depend on the order:: 
     50       
     51        sage: S1 = FiniteEnumeratedSet((1, 2, 3))
     52        sage: S1
     53        {1, 2, 3}       
     54        sage: S1.list()
     55        [1, 2, 3]
     56        sage: S1 == S
     57        True
     58        sage: S2 = FiniteEnumeratedSet((2, 1, 3))
     59        sage: S2 == S
     60        False
     61    """
     62
     63    @staticmethod
     64    def __classcall__(cls, iterable):
     65        """
     66        TESTS::
     67       
     68            sage: S1 = FiniteEnumeratedSet([1, 2, 3])
     69            sage: S2 = FiniteEnumeratedSet((1, 2, 3))
     70            sage: S1 is S2
     71            True
     72        """
     73        return super(FiniteEnumeratedSet, cls).__classcall__(cls, tuple(iterable))
     74   
     75    def __init__(self, elements):
     76        """
     77        TESTS::
     78       
     79            sage: S = FiniteEnumeratedSet([1,2,3])
     80            sage: S == loads(dumps(S))
     81            True
     82        """
     83        self._elements = elements
     84        Parent.__init__(self, category = FiniteEnumeratedSets())
     85
     86    def __repr__(self):
     87        """
     88        TESTS::
     89       
     90            sage: S = FiniteEnumeratedSet([1,2,3])
     91            sage: repr(S)
     92            '{1, 2, 3}'
     93        """
     94        return "{"+str(self._elements)[1:-1] + '}'
     95
     96    def __contains__(self, x):
     97        """
     98        TESTS::
     99       
     100            sage: S = FiniteEnumeratedSet([1,2,3])
     101            sage: 1 in S
     102            True
     103            sage: 2 in S
     104            True
     105            sage: 4 in S
     106            False
     107            sage: ZZ in S
     108            False
     109        """
     110        return x in self._elements
     111
     112    def list(self):
     113        """
     114        TESTS::
     115       
     116            sage: S = FiniteEnumeratedSet([1,2,3])
     117            sage: S.list()
     118            [1, 2, 3]
     119        """
     120        return list(self._elements)
     121
     122    def an_element(self):
     123        """
     124        TESTS::
     125       
     126            sage: S = FiniteEnumeratedSet([1,2,3])
     127            sage: S.an_element()
     128            1
     129        """
     130        return self._elements[0]
     131   
     132    def cardinality(self):
     133        """
     134        TESTS::
     135       
     136            sage: S = FiniteEnumeratedSet([1,2,3])
     137            sage: S.cardinality()
     138            3
     139        """       
     140        return Integer(len(self._elements))
     141
  • sage/sets/set.py

    diff --git a/sage/sets/set.py b/sage/sets/set.py
    a b class Set_object_enumerated(Set_object): 
    603602        s = repr(self.set())
    604603        return "{" + s[5:-2] + "}"
    605604
     605    def list(self):
     606        """
     607        Return the elements of self, as a list
     608
     609        EXAMPLES::
     610       
     611            sage: X = Set(GF(8,'c'))
     612            sage: X
     613            {0, 1, c, c + 1, c^2, c^2 + 1, c^2 + c, c^2 + c + 1}
     614            sage: X.list()
     615            [0, 1, c, c + 1, c^2, c^2 + 1, c^2 + c, c^2 + c + 1]
     616            sage: type(X.list())
     617            <type 'list'>
     618        """
     619        # What should be the order of the result?
     620        # That of self.object()? Or the order given by set(self.object())?
     621        # Note: __getitem__ is currently implemented in term of this list
     622        # method, which is really innefficient ...
     623        return list(set(self.object()))
     624
    606625    def set(self):
    607626        """
    608627        Return the Python set object associated to this set.