# Ticket #9256: trac_9256-set_in_category-fh.patch

File trac_9256-set_in_category-fh.patch, 4.8 KB (added by hivert, 12 years ago)
• ## sage/sets/set.py

# HG changeset patch
# User Florent Hivert <Florent.Hivert@univ-rouen.fr>
# Date 1276811563 -7200
# Node ID 8e2b1fd3b26344b5ee2810c7bc26fc1155f82cd6
# Parent  52c6dfe1db84c935798b868504af2b30f4c5216a
#9256: Put Set in the category Sets()

diff --git a/sage/sets/set.py b/sage/sets/set.py
 a AUTHORS: - Mike Hansen (2007-3-25) - added differences and symmetric differences; fixed operators - Florent Hivert (2010-06-17) - Adapted to categories """ #***************************************************************************** from sage.structure.parent import Parent from sage.misc.latex import latex import sage.rings.infinity from sage.misc.misc import is_iterator from sage.categories.sets_cat import Sets from sage.categories.enumerated_sets import EnumeratedSets def Set(X): r""" def Set(X): sage: X {0, 1, 2, a, a + 1, a + 2, 2*a, 2*a + 1, 2*a + 2} sage: type(X) sage: Y = X.union(Set(QQ)) sage: Y Set-theoretic union of {0, 1, 2, a, a + 1, a + 2, 2*a, 2*a + 1, 2*a + 2} and Set of elements of Rational Field sage: type(Y) Usually sets can be used as dictionary keys. def Set(X): sage: Set(iter([1,2,3])) {1, 2, 3} sage: type(_) """ if is_Set(X): return X class Set_object(Set_generic): sage: latex(S) \left\{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18\right\} sage: loads(S.dumps()) == S True sage: TestSuite(S).run() sage: latex(Set(ZZ)) \Bold{Z} class Set_object(Set_generic): EXAMPLES:: sage: type(Set(QQ)) """ Parent.__init__(self, category=Sets()) self.__object = X def __hash__(self): class Set_object(Set_generic): """ return self.__object.__iter__() an_element = EnumeratedSets.ParentMethods.__dict__['_an_element_from_iterator'] def __contains__(self, x): """ Return True if $x$ is in self. class Set_object_enumerated(Set_object): {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18} sage: print latex(S) \left\{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18\right\} sage: loads(S.dumps()) == S True sage: TestSuite(S).run() """ Set_object.__init__(self, X) class Set_object_enumerated(Set_object): sage: type(X.set()) sage: type(X) """ return set(self.object()) class Set_object_union(Set_object): sage: latex(X) \Bold{Q}^{2} \cup \Bold{Z} sage: loads(X.dumps()) == X True sage: TestSuite(X).run() """ self.__X = X self.__Y = Y class Set_object_intersection(Set_object def __init__(self, X, Y): r""" EXAMPLES:: sage: S = Set(QQ^2) sage: T = Set(ZZ) sage: X = S.intersection(T); X class Set_object_intersection(Set_object sage: latex(X) \Bold{Q}^{2} \cap \Bold{Z} sage: loads(X.dumps()) == X True sage: X = Set(IntegerRange(100)).intersection(Primes()) sage: TestSuite(X).run() """ self.__X = X self.__Y = Y class Set_object_difference(Set_object): sage: latex(X) \Bold{Q} - \Bold{Z} sage: loads(X.dumps()) == X True sage: TestSuite(X).run() """ self.__X = X self.__Y = Y class Set_object_symmetric_difference(Se sage: latex(X) \Bold{Q} \bigtriangleup \Bold{Z} sage: loads(X.dumps()) == X True sage: TestSuite(X).run() """ self.__X = X self.__Y = Y