Non Negative Integers - Sets

Skip to content
Back to top
View this page
Edit this page

# Non Negative Integers

class sage.sets.non_negative_integers.NonNegativeIntegers(category=None)

  Bases: `UniqueRepresentation`, `Parent`

  The enumerated set of nonnegative integers.

  This class implements the set of nonnegative integers, as an enumerated set (see `InfiniteEnumeratedSets`).

  EXAMPLES:

  Sage

      sage: NN = NonNegativeIntegers()
      sage: NN
      Non negative integers
      sage: NN.cardinality()
      +Infinity
      sage: TestSuite(NN).run()
      sage: NN.list()
      Traceback (most recent call last):
      ...
      NotImplementedError: cannot list an infinite set
      sage: NN.element_class
      <... 'sage.rings.integer.Integer'>
      sage: it = iter(NN)
      sage: [next(it), next(it), next(it), next(it), next(it)]
      [0, 1, 2, 3, 4]
      sage: NN.first()
      0

  Python

      >>> from sage.all import *
      >>> NN = NonNegativeIntegers()
      >>> NN
      Non negative integers
      >>> NN.cardinality()
      +Infinity
      >>> TestSuite(NN).run()
      >>> NN.list()
      Traceback (most recent call last):
      ...
      NotImplementedError: cannot list an infinite set
      >>> NN.element_class
      <... 'sage.rings.integer.Integer'>
      >>> it = iter(NN)
      >>> [next(it), next(it), next(it), next(it), next(it)]
      [0, 1, 2, 3, 4]
      >>> NN.first()
      0

  Currently, this is just a “facade” parent; namely its elements are plain Sage `Integers` with `Integer Ring` as parent:

  Sage

      sage: x = NN(15); type(x)
      <... 'sage.rings.integer.Integer'>
      sage: x.parent()
      Integer Ring
      sage: x+3
      18

  [Python]

      >>> from sage.all import *
      >>> x = NN(Integer(15)); type(x)
      <... 'sage.rings.integer.Integer'>
      >>> x.parent()
      Integer Ring
      >>> x+Integer(3)
      18

  In a later version, there will be an option to specify whether the elements should have `Integer Ring` or `Non negative integers` as parent:

  Sage

      sage: NN = NonNegativeIntegers(facade = False) # todo: not implemented
      sage: x = NN(5)                                # todo: not implemented
      sage: x.parent()                               # todo: not implemented
      Non negative integers

  Python

      >>> from sage.all import *
      >>> NN = NonNegativeIntegers(facade = False) # todo: not implemented
      >>> x = NN(Integer(5))                                # todo: not implemented
      >>> x.parent()                               # todo: not implemented
      Non negative integers

  This runs generic sanity checks on `NN`:

  Sage

      sage: TestSuite(NN).run()

  [Python]

      >>> from sage.all import *
      >>> TestSuite(NN).run()

  TODO: do not use `NN` any more in the doctests for `NonNegativeIntegers`.

  Element

    alias of `Integer`

  an_element()

    EXAMPLES:

    Sage

        sage: NonNegativeIntegers().an_element()
        42

    Python

        >>> from sage.all import *
        >>> NonNegativeIntegers().an_element()
        42

  from_integer

    alias of `Integer`

  next(o)

    EXAMPLES:

    Sage

        sage: NN = NonNegativeIntegers()
        sage: NN.next(3)
        4

    Python

        >>> from sage.all import *
        >>> NN = NonNegativeIntegers()
        >>> NN.next(Integer(3))
        4

  some_elements()

    EXAMPLES:

    Sage

        sage: NonNegativeIntegers().some_elements()
        [0, 1, 3, 42]

    Python

        >>> from sage.all import *
        >>> NonNegativeIntegers().some_elements()
        [0, 1, 3, 42]

  unrank(rnk)

    EXAMPLES:

    Sage

        sage: NN = NonNegativeIntegers()
        sage: NN.unrank(100)
        100

    Python

        >>> from sage.all import *
        >>> NN = NonNegativeIntegers()
        >>> NN.unrank(Integer(100))
        100