Path: blob/master/src/sage/sets/non_negative_integers.py
8817 views
"""1Non Negative Integers2"""3#*****************************************************************************4# Copyright (C) 2009 Florent Hivert <[email protected]>5#6# Distributed under the terms of the GNU General Public License (GPL)7# http://www.gnu.org/licenses/8#*****************************************************************************910from sage.structure.parent import Parent11from sage.categories.infinite_enumerated_sets import InfiniteEnumeratedSets12from sage.structure.unique_representation import UniqueRepresentation13from sage.rings.integer import Integer1415class NonNegativeIntegers(UniqueRepresentation, Parent):16r"""17The enumerated set of non negative integers.1819This class implements the set of non negative integers, as an20enumerated set (see :class:`InfiniteEnumeratedSets21<sage.categories.infinite_enumerated_sets.InfiniteEnumeratedSets>`).2223EXAMPLES::2425sage: NN = NonNegativeIntegers()26sage: NN27Non negative integers28sage: NN.cardinality()29+Infinity30sage: TestSuite(NN).run()31sage: NN.list()32Traceback (most recent call last):33...34NotImplementedError: infinite list35sage: NN.element_class36<type 'sage.rings.integer.Integer'>37sage: it = iter(NN)38sage: [it.next(), it.next(), it.next(), it.next(), it.next()]39[0, 1, 2, 3, 4]40sage: NN.first()4104243Currently, this is just a "facade" parent; namely its elements are44plain Sage ``Integers`` with ``Integer Ring`` as parent::4546sage: x = NN(15); type(x)47<type 'sage.rings.integer.Integer'>48sage: x.parent()49Integer Ring50sage: x+351185253In a later version, there will be an option to specify whether the54elements should have ``Integer Ring`` or ``Non negative integers``55as parent::5657sage: NN = NonNegativeIntegers(facade = False) # todo: not implemented58sage: x = NN(5) # todo: not implemented59sage: x.parent() # todo: not implemented60Non negative integers6162This runs generic sanity checks on ``NN``::6364sage: TestSuite(NN).run()6566TODO: do not use ``NN`` any more in the doctests for67``NonNegativeIntegers``.68"""6970def __init__(self, category=None):71"""72TESTS::7374sage: NN = NonNegativeIntegers()75sage: NN76Non negative integers77sage: NN.category()78Category of facade infinite enumerated sets79sage: TestSuite(NN).run()80"""81from sage.rings.integer_ring import ZZ82Parent.__init__(self, facade = ZZ, category = InfiniteEnumeratedSets().or_subcategory(category) )8384def _repr_(self):85"""86TESTS::8788sage: NonNegativeIntegers() # indirect doctest89Non negative integers90"""91return "Non negative integers"9293def __contains__(self, elt):94"""95EXAMPLES::9697sage: NN = NonNegativeIntegers()98sage: 1 in NN99True100sage: -1 in NN101False102sage: x in NN103False104sage: None in NN105False106"""107try:108i = Integer(elt)109return i >= Integer(0) and i == elt110except TypeError:111return False112113def _element_constructor_(self, i):114"""115Constructs an element of self from an integer, testing that116this integer is indeed non negative.117118EXAMPLES::119120sage: NN = NonNegativeIntegers()121sage: NN._element_constructor_(42)12242123sage: NN._element_constructor_(-5)124Traceback (most recent call last):125...126ValueError: Value -5 in not in Non negative integers.127sage: NN._element_constructor_(x)128Traceback (most recent call last):129...130ValueError: Value x in not in Non negative integers.131132This is used upon coercion attempts::133134sage: n = NN(42); n # indirect doctest13542136sage: type(n)137<type 'sage.rings.integer.Integer'>138sage: n.parent()139Integer Ring140sage: NN(-1)141Traceback (most recent call last):142...143ValueError: Value -1 in not in Non negative integers.144145For fast construction of elements without tests, please use146instead ``from_integer``::147148sage: NN.from_integer(42)14942150sage: NN.from_integer(-5) # Don't do that at home kids!151-5152"""153if i in self:154return self.from_integer(i)155else:156raise ValueError, "Value %s in not in %s."%(i, self)157158from_integer = Integer159160Element = Integer161162def __iter__(self):163"""164EXAMPLES::165166sage: NN = NonNegativeIntegers()167sage: g = iter(NN)168sage: g.next(), g.next(), g.next(), g.next()169(0, 1, 2, 3)170"""171i = 0172while True:173yield self.from_integer(i)174i += 1175# Uncomment the following two lines to catch infinite loops when debugging176#if i > 200:177# raise ValueError, "Infinite loop during DEBUG! TODO: remove me"178179def an_element(self):180"""181EXAMPLES::182183sage: NonNegativeIntegers().an_element()18442185"""186return self.from_integer(Integer(42))187188def some_elements(self):189"""190EXAMPLES::191192sage: NonNegativeIntegers().some_elements()193[0, 1, 3, 42]194"""195return [Integer(0), Integer(1), Integer(3), Integer(42)]196197def next(self, o):198"""199EXAMPLES::200201sage: NN = NonNegativeIntegers()202sage: NN.next(3)2034204"""205return self.from_integer(o+1)206207def unrank(self, rnk):208"""209EXAMPLES::210211sage: NN = NonNegativeIntegers()212sage: NN.unrank(100)213100214"""215return self.from_integer(rnk)216217218