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.
See also
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\)
-
class
sage.structure.set_factories_example.
AllPairs
(policy)¶ Bases:
sage.structure.set_factories.ParentWithSetFactory
,sage.sets.disjoint_union_enumerated_sets.DisjointUnionEnumeratedSets
This parent shows how one can use set factories together with
DisjointUnionEnumeratedSets
.It is constructed as the disjoint union (
DisjointUnionEnumeratedSets
) ofPairs_Y
parents:\[S := \bigcup_{i = 0,1,..., 4} S^y\]Warning
When writing a parent
P
as a disjoint union of a family of parentsP_i
, the parentsP_i
must be constructed as facade parents forP
. As a consequence, it should be passedP.facade_policy()
as policy argument. See the source code ofpairs_y()
for an example.TESTS:
sage: from sage.structure.set_factories_example import XYPairs sage: P = XYPairs(); P.list() [(0, 0), (1, 0), (2, 0), (3, 0), (4, 0), (0, 1), (1, 1), (2, 1), (3, 1), (4, 1), (0, 2), (1, 2), (2, 2), (3, 2), (4, 2), (0, 3), (1, 3), (2, 3), (3, 3), (4, 3), (0, 4), (1, 4), (2, 4), (3, 4), (4, 4)]
-
check_element
(el, check)¶ TESTS:
sage: from sage.structure.set_factories_example import XYPairs sage: P = XYPairs() sage: P.check_element(P.an_element(), True) sage: XYPairs()((7, 0)) # indirect doctest Traceback (most recent call last): ... ValueError: numbers must be in range(5)
-
pairs_y
(letter)¶ Construct the parent for the disjoint union
Construct a parent in
Pairs_Y
as a facade parent forself
.This is an internal function which should be hidden from the user (typically under the name
_pairs_y
. We put it here for documentation.TESTS:
sage: from sage.structure.set_factories_example import XYPairs, XYPair sage: S = XYPairs() sage: S1 = S.pairs_y(1); S1 {(a, 1) | a in range(5)} sage: S.an_element().parent() AllPairs sage: from sage.structure.set_factories import SelfParentPolicy sage: selfpolicy = SelfParentPolicy(XYPairs, XYPair) sage: selfS = XYPairs(policy=selfpolicy) sage: selfS1 = selfS.pairs_y(1); selfS1 {(a, 1) | a in range(5)} sage: S.an_element().parent() is selfS False sage: selfS.an_element().parent() is selfS True
-
-
class
sage.structure.set_factories_example.
PairsX_
(x, policy)¶ Bases:
sage.structure.set_factories.ParentWithSetFactory
,sage.structure.unique_representation.UniqueRepresentation
The set of pairs \((x, 0), (x, 1), ..., (x, 4)\).
TESTS:
sage: from sage.structure.set_factories_example import XYPairs sage: P = XYPairs(0); P.list() [(0, 0), (0, 1), (0, 2), (0, 3), (0, 4)]
-
an_element
()¶ TESTS:
sage: from sage.structure.set_factories_example import XYPairs sage: P = XYPairs(x=0); P.an_element() (0, 0)
-
check_element
(el, check)¶ TESTS:
sage: from sage.structure.set_factories_example import XYPairs sage: P = XYPairs(x=1) sage: P.check_element(P.an_element(), True) sage: XYPairs(x=1)((0, 0)) # indirect doctest Traceback (most recent call last): ... ValueError: Wrong first coordinate
-
-
class
sage.structure.set_factories_example.
Pairs_Y
(y, policy)¶ Bases:
sage.structure.set_factories.ParentWithSetFactory
,sage.sets.disjoint_union_enumerated_sets.DisjointUnionEnumeratedSets
The set of pairs \((0, y), (1, y), ..., (4, y)\).
It is constructed as the disjoint union (
DisjointUnionEnumeratedSets
) ofSingletonPair
parents:\[S^y := \bigcup_{i = 0,1,..., 4} S_i^y\]See also
AllPairs
for how to properly constructDisjointUnionEnumeratedSets
usingParentWithSetFactory
.TESTS:
sage: from sage.structure.set_factories_example import XYPairs sage: P = XYPairs(y=1); P.list() [(0, 1), (1, 1), (2, 1), (3, 1), (4, 1)]
-
an_element
()¶ TESTS:
sage: from sage.structure.set_factories_example import XYPairs sage: XYPairs(y=1).an_element() (0, 1)
-
check_element
(el, check)¶ TESTS:
sage: from sage.structure.set_factories_example import XYPairs sage: P = XYPairs(y=1) sage: P.check_element(P.an_element(), True) sage: XYPairs(y=1)((1, 0)) # indirect doctest Traceback (most recent call last): ... ValueError: Wrong second coordinate
-
single_pair
(letter)¶ Construct the singleton pair parent
Construct a singleton pair for
(self.y, letter)
as a facade parent forself
.See also
AllPairs
for how to properly constructDisjointUnionEnumeratedSets
usingParentWithSetFactory
.TESTS:
sage: from sage.structure.set_factories_example import XYPairs sage: P = XYPairs(y=1) sage: P.single_pair(0) {(0, 1)} sage: P.single_pair(0).an_element().parent() AllPairs
-
-
class
sage.structure.set_factories_example.
SingletonPair
(x, y, policy)¶ Bases:
sage.structure.set_factories.ParentWithSetFactory
,sage.structure.unique_representation.UniqueRepresentation
TESTS:
sage: from sage.structure.set_factories_example import XYPairs sage: P = XYPairs(0,1); P.list() [(0, 1)]
-
check_element
(el, check)¶ TESTS:
sage: from sage.structure.set_factories_example import XYPairs sage: XYPairs(0,1).check_element(XYPairs()((0,1)), True) sage: XYPairs(0,1).check_element(XYPairs()((1,0)), True) Traceback (most recent call last): ... ValueError: Wrong coordinate sage: XYPairs(0,1)((1,1)) Traceback (most recent call last): ... ValueError: Wrong coordinate
-
-
class
sage.structure.set_factories_example.
XYPair
(parent, value, check=True)¶ Bases:
sage.structure.element_wrapper.ElementWrapper
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
(x=None, y=None, policy=None)¶ Construct the subset from constraints.
Consider the set \(S\) of couples \((x,y)\) with \(x\) and \(y\) in \(I:=\{0,1,2,3,4\}\). Returns the subsets of element of \(S\) satisfying some constraints.
INPUT:
x=a
– wherea
is an integer (default toNone
).y=b
– whereb
is an integer (default toNone
).policy
– the policy passed to the created set.
See also
EXAMPLES:
Let us first create the set factory:
sage: from sage.structure.set_factories_example import XYPairsFactory sage: XYPairs = XYPairsFactory()
One can then use the set factory to construct a set:
sage: P = XYPairs(); P.list() [(0, 0), (1, 0), (2, 0), (3, 0), (4, 0), (0, 1), (1, 1), (2, 1), (3, 1), (4, 1), (0, 2), (1, 2), (2, 2), (3, 2), (4, 2), (0, 3), (1, 3), (2, 3), (3, 3), (4, 3), (0, 4), (1, 4), (2, 4), (3, 4), (4, 4)]
Note
This function is actually the
__call__
method ofXYPairsFactory
.TESTS:
sage: TestSuite(P).run()
-
class
sage.structure.set_factories_example.
XYPairsFactory
¶ Bases:
sage.structure.set_factories.SetFactory
An example of set factory, for sets of pairs of integers.
See also
set_factories
for an introduction to set factories.-
add_constraints
(cons, args_opts)¶ Add constraints to the set
cons
as perSetFactory.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 sage: XYPairs.add_constraints((3,None), ((2,), {})) Traceback (most recent call last): ... ValueError: Duplicate value for constraints 'x': was 3 now 2 sage: XYPairs.add_constraints((), ((2,), {})) (2, None) sage: XYPairs.add_constraints((), ((2,), {'y':3})) (2, 3)
-