Examples of sets

class sage.categories.examples.sets_cat.PrimeNumbers

Bases: sage.structure.unique_representation.UniqueRepresentation, sage.structure.parent.Parent

An example of parent in the category of sets: the set of prime numbers.

The elements are represented as plain integers in \(\ZZ\) (facade implementation).

This is a minimal implementations. For more advanced examples of implementations, see also:

sage: P = Sets().example("facade")
sage: P = Sets().example("inherits")
sage: P = Sets().example("wrapper")

EXAMPLES:

sage: P = Sets().example()
sage: P(12)
Traceback (most recent call last):
...
AssertionError: 12 is not a prime number
sage: a = P.an_element()
sage: a.parent()
Integer Ring
sage: x = P(13); x
13
sage: type(x)
<type 'sage.rings.integer.Integer'>
sage: x.parent()
Integer Ring
sage: 13 in P
True
sage: 12 in P
False
sage: y = x+1; y
14
sage: type(y)
<type 'sage.rings.integer.Integer'>

sage: TestSuite(P).run(verbose=True)
running ._test_an_element() . . . pass
running ._test_category() . . . pass
running ._test_elements() . . .
  Running the test suite of self.an_element()
  running ._test_category() . . . pass
  running ._test_eq() . . . pass
  running ._test_nonzero_equal() . . . pass
  running ._test_not_implemented_methods() . . . pass
  running ._test_pickling() . . . pass
  pass
running ._test_elements_eq_reflexive() . . . pass
running ._test_elements_eq_symmetric() . . . pass
running ._test_elements_eq_transitive() . . . pass
running ._test_elements_neq() . . . pass
running ._test_eq() . . . pass
running ._test_not_implemented_methods() . . . pass
running ._test_pickling() . . . pass
running ._test_some_elements() . . . pass
an_element()

Implements Sets.ParentMethods.an_element().

TESTS:

sage: P = Sets().example()
sage: x = P.an_element(); x
47
sage: x.parent()
Integer Ring
element_class

alias of Integer

class sage.categories.examples.sets_cat.PrimeNumbers_Abstract

Bases: sage.structure.unique_representation.UniqueRepresentation, sage.structure.parent.Parent

This class shows how to write a parent while keeping the choice of the datastructure for the children open. Different class with fixed datastructure will then be constructed by inheriting from PrimeNumbers_Abstract.

This is used by:

sage: P = Sets().example(“facade”) sage: P = Sets().example(“inherits”) sage: P = Sets().example(“wrapper”)
class Element

Bases: sage.structure.element.Element

INPUT:

  • parent - a SageObject
is_prime()

Returns if a prime number is prime = True !

EXAMPLES:

sage: P = Sets().example("inherits")
sage: x = P.an_element()
sage: P.an_element().is_prime()
True
next()

Returns the next prime number

EXAMPLES:

sage: P = Sets().example("inherits")
sage: P.an_element().next()
53
PrimeNumbers_Abstract.an_element()

Implements Sets.ParentMethods.an_element().

TESTS:

sage: P = Sets().example("inherits")
sage: x = P.an_element(); x
47
sage: x.parent()
Set of prime numbers
PrimeNumbers_Abstract.next(i)

Returns the next prime number

EXAMPLES:

sage: P = Sets().example("inherits")
sage: x = P.next(P.an_element()); x
53
sage: x.parent()
Set of prime numbers
PrimeNumbers_Abstract.some_elements()

Returns some prime numbers

EXAMPLES:

sage: P = Sets().example("inherits")
sage: P.some_elements()
[47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
class sage.categories.examples.sets_cat.PrimeNumbers_Facade

Bases: sage.categories.examples.sets_cat.PrimeNumbers_Abstract

An example of parent in the category of sets: the set of prime numbers.

In this alternative implementation, the elements are represented as plain integers in \(\ZZ\) (facade implementation).

EXAMPLES:

sage: P = Sets().example("facade")
sage: P(12)
Traceback (most recent call last):
...
ValueError: 12 is not a prime number
sage: a = P.an_element()
sage: a.parent()
Integer Ring
sage: x = P(13); x
13
sage: type(x)
<type 'sage.rings.integer.Integer'>
sage: x.parent()
Integer Ring
sage: 13 in P
True
sage: 12 in P
False
sage: y = x+1; y
14
sage: type(y)
<type 'sage.rings.integer.Integer'>

sage: z = P.next(x); z
17
sage: type(z)
<type 'sage.rings.integer.Integer'>
sage: z.parent()
Integer Ring

The disadvantage of this implementation is that the element doesn’t know that they are primes so that prime testing is slow:

sage: pf = Sets().example("facade").an_element()
sage: timeit("pf.is_prime()") #    random
625 loops, best of 3: 4.1 us per loop

compared to the other implementations where prime testing is only done if needed during the construction of the element. Then the elements themselve “know” that they are prime:

sage: pw = Sets().example("wrapper").an_element()
sage: timeit("pw.is_prime()")    # random
625 loops, best of 3: 859 ns per loop

sage: pi = Sets().example("inherits").an_element()
sage: timeit("pw.is_prime()")    # random
625 loops, best of 3: 854 ns per loop

And moreover, the next methods for the element does not exists:

sage: pf.next()
Traceback (most recent call last):
...
AttributeError: 'sage.rings.integer.Integer' object has no attribute 'next'

whereas:

sage: pw.next()
53
sage: pi.next()
53

TESTS:

sage: TestSuite(P).run(verbose = True)
running ._test_an_element() . . . pass
running ._test_category() . . . pass
running ._test_elements() . . .
  Running the test suite of self.an_element()
  running ._test_category() . . . pass
  running ._test_eq() . . . pass
  running ._test_nonzero_equal() . . . pass
  running ._test_not_implemented_methods() . . . pass
  running ._test_pickling() . . . pass
  pass
running ._test_elements_eq_reflexive() . . . pass
running ._test_elements_eq_symmetric() . . . pass
running ._test_elements_eq_transitive() . . . pass
running ._test_elements_neq() . . . pass
running ._test_eq() . . . pass
running ._test_not_implemented_methods() . . . pass
running ._test_pickling() . . . pass
running ._test_some_elements() . . . pass
element_class

alias of Integer

class sage.categories.examples.sets_cat.PrimeNumbers_Inherits

Bases: sage.categories.examples.sets_cat.PrimeNumbers_Abstract

An example of parent in the category of sets: the set of prime numbers. In this implementation, the element are stored as object of a new class which inherits from the class Integer (technically IntegerWrapper).

EXAMPLES:

sage: P = Sets().example("inherits")
sage: P
Set of prime numbers
sage: P(12)
Traceback (most recent call last):
...
ValueError: 12 is not a prime number
sage: a = P.an_element()
sage: a.parent()
Set of prime numbers
sage: x = P(13); x
13
sage: x.is_prime()
True
sage: type(x)
<class 'sage.categories.examples.sets_cat.PrimeNumbers_Inherits_with_category.element_class'>
sage: x.parent()
Set of prime numbers
sage: P(13) in P
True
sage: y = x+1; y
14
sage: type(y)
<type 'sage.rings.integer.Integer'>
sage: y.parent()
Integer Ring
sage: type(P(13)+P(17))
<type 'sage.rings.integer.Integer'>
sage: type(P(2)+P(3))
<type 'sage.rings.integer.Integer'>

sage: z = P.next(x); z
17
sage: type(z)
<class 'sage.categories.examples.sets_cat.PrimeNumbers_Inherits_with_category.element_class'>
sage: z.parent()
Set of prime numbers

sage: TestSuite(P).run(verbose=True)
running ._test_an_element() . . . pass
running ._test_category() . . . pass
running ._test_elements() . . .
  Running the test suite of self.an_element()
  running ._test_category() . . . pass
  running ._test_eq() . . . pass
  running ._test_not_implemented_methods() . . . pass
  running ._test_pickling() . . . pass
  pass
running ._test_elements_eq_reflexive() . . . pass
running ._test_elements_eq_symmetric() . . . pass
running ._test_elements_eq_transitive() . . . pass
running ._test_elements_neq() . . . pass
running ._test_eq() . . . pass
running ._test_not_implemented_methods() . . . pass
running ._test_pickling() . . . pass
running ._test_some_elements() . . . pass

See also:

sage: P = Sets().example("facade")
sage: P = Sets().example("inherits")
sage: P = Sets().example("wrapper")
class Element(parent, p)

Bases: sage.rings.integer.IntegerWrapper, sage.categories.examples.sets_cat.PrimeNumbers_Abstract.Element

TESTS:

sage: P = Sets().example("inherits")
sage: P(12)
Traceback (most recent call last):
...
ValueError: 12 is not a prime number
sage: x = P(13); type(x)
<class 'sage.categories.examples.sets_cat.PrimeNumbers_Inherits_with_category.element_class'>
sage: x.parent() is P
True
class sage.categories.examples.sets_cat.PrimeNumbers_Wrapper

Bases: sage.categories.examples.sets_cat.PrimeNumbers_Abstract

An example of parent in the category of sets: the set of prime numbers.

In this second alternative implementation, the prime integer are stored as a attribute of a sage object by inheriting from ElementWrapper. In this case we need to ensure conversion and coercion from this parent and its element to ZZ and Integer.

EXAMPLES:

sage: P = Sets().example("wrapper")
sage: P(12)
Traceback (most recent call last):
...
ValueError: 12 is not a prime number
sage: a = P.an_element()
sage: a.parent()
Set of prime numbers (wrapper implementation)
sage: x = P(13); x
13
sage: type(x)
<class 'sage.categories.examples.sets_cat.PrimeNumbers_Wrapper_with_category.element_class'>
sage: x.parent()
Set of prime numbers (wrapper implementation)
sage: 13 in P
True
sage: 12 in P
False
sage: y = x+1; y
14
sage: type(y)
<type 'sage.rings.integer.Integer'>

sage: z = P.next(x); z
17
sage: type(z)
<class 'sage.categories.examples.sets_cat.PrimeNumbers_Wrapper_with_category.element_class'>
sage: z.parent()
Set of prime numbers (wrapper implementation)

TESTS:

sage: TestSuite(P).run()
class Element

Bases: sage.structure.element_wrapper.ElementWrapper, sage.categories.examples.sets_cat.PrimeNumbers_Abstract.Element

EXAMPLES:

sage: from sage.structure.element_wrapper import DummyParent
sage: a = ElementWrapper(DummyParent("A parent"), 1)

TESTS:

sage: TestSuite(a).run(skip = "_test_category")

sage: a = ElementWrapper(1, DummyParent("A parent"))
doctest:...: DeprecationWarning: the first argument must be a parent
See http://trac.sagemath.org/14519 for details.

Note

ElementWrapper is not intended to be used directly, hence the failing category test.

PrimeNumbers_Wrapper.ElementWrapper

alias of ElementWrapper

Previous topic

Examples of semigroups in cython

Next topic

Examples of parents endowed with multiple realizations

This Page