An example of set factory¶

The goal of this module is to exemplify the use of set factories. Note that the code is intentionally kept minimal; many things and in particular several iterators could be written in a more efficient way.

set_factories for an introduction to set factories, their specifications, and examples of their use and implementation based on this module.

We describe here a factory used to construct the set $$S$$ of couples $$(x,y)$$ with $$x$$ and $$y$$ in $$I:=\{0,1,2,3,4\}$$, together with the following subsets, where $$(a, b)\in S$$

\begin{align}\begin{aligned}S_a := \{(x,y) \in S \mid x = a\},\\S^b := \{(x,y) \in S \mid y = b\},\\S_a^b := \{(x,y) \in S \mid x = a, y = b\}.\end{aligned}\end{align}
class sage.structure.set_factories_example.AllPairs(policy)

This parent shows how one can use set factories together with DisjointUnionEnumeratedSets.

It is constructed as the disjoint union (DisjointUnionEnumeratedSets) of Pairs_Y parents:

$S := \bigcup_{i = 0,1,..., 4} S^y$

Warning

When writing a parent P as a disjoint union of a family of parents P_i, the parents P_i must be constructed as facade parents for P. As a consequence, it should be passed P.facade_policy() as policy argument. See the source code of pairs_y() for an example.

check_element(el, check)
pairs_y(letter)

Construct the parent for the disjoint union

Construct a parent in Pairs_Y as a facade parent for self.

This is an internal function which should be hidden from the user (typically under the name _pairs_y. We put it here for documentation.

class sage.structure.set_factories_example.PairsX_(x, policy)

The set of pairs $$(x, 0), (x, 1), ..., (x, 4)$$.

an_element()
check_element(el, check)
class sage.structure.set_factories_example.Pairs_Y(y, policy)

The set of pairs $$(0, y), (1, y), ..., (4, y)$$.

It is constructed as the disjoint union (DisjointUnionEnumeratedSets) of SingletonPair parents:

$S^y := \bigcup_{i = 0,1,..., 4} S_i^y$
an_element()
check_element(el, check)
single_pair(letter)

Construct the singleton pair parent

Construct a singleton pair for (self.y, letter) as a facade parent for self.

class sage.structure.set_factories_example.SingletonPair(x, y, policy)
check_element(el, check)
class sage.structure.set_factories_example.XYPair(parent, value, check=True)

A class for Elements $$(x,y)$$ with $$x$$ and $$y$$ in $$\{0,1,2,3,4\}$$.

EXAMPLES:

sage: from sage.structure.set_factories_example import XYPair
sage: p = XYPair(Parent(), (0,1)); p
(0, 1)
sage: p = XYPair(Parent(), (0,8))
Traceback (most recent call last):
...
ValueError: numbers must be in range(5)

sage.structure.set_factories_example.XYPairs = Factory for XY pairs
class sage.structure.set_factories_example.XYPairsFactory

An example of set factory, for sets of pairs of integers.

set_factories for an introduction to set factories.

add_constraints(cons, args_opts)

Add constraints to the set cons as per SetFactory.add_constraints.

This is a crude implementation for the sake of the demonstration which should not be taken as an example.

EXAMPLES:

sage: from sage.structure.set_factories_example import XYPairs