Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
r""" 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.
.. SEEALSO::
:mod:`.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`
.. MATH::
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\}.
""" #***************************************************************************** # Copyright (C) 2012 Florent Hivert <florent.hivert at lri.fr> # # Distributed under the terms of the GNU General Public License (GPL) # http://www.gnu.org/licenses/ #*****************************************************************************
SetFactory, ParentWithSetFactory, TopMostParentPolicy)
r""" An example of set factory, for sets of pairs of integers.
.. SEEALSO::
:mod:`.set_factories` for an introduction to set factories. """ r""" 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`` -- where ``a`` is an integer (default to ``None``). - ``y=b`` -- where ``b`` is an integer (default to ``None``). - ``policy`` -- the policy passed to the created set.
.. SEEALSO::
:class:`.set_factories.SetFactoryPolicy`
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 of :class:`XYPairsFactory`.
TESTS::
sage: TestSuite(P).run()
"""
r""" Add constraints to the set ``cons`` as per :meth:`SetFactory.add_constraints<.set_factories.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 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) """
"was {} now {}".format(['x', 'y'][i], res[i], v))
return ()
def _default_policy(self): r""" TESTS::
sage: from sage.structure.set_factories_example import XYPairsFactory sage: XYPairs = XYPairsFactory() sage: XYPairs._default_policy Set factory policy for <class 'sage.structure.set_factories_example.XYPair'> with parent AllPairs[=Factory for XY pairs(())] """
""" TESTS::
sage: from sage.structure.set_factories_example import XYPairs sage: XYPairs # indirect doctest Factory for XY pairs """
r""" 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) """ """ TESTS::
sage: from sage.structure.set_factories_example import XYPairs sage: P = XYPairs(); p = P.list()[0] sage: TestSuite(p).run() """ raise ValueError("Value must be of length 2")
r""" This parent shows how one can use set factories together with :class:`DisjointUnionEnumeratedSets`.
It is constructed as the disjoint union (:class:`DisjointUnionEnumeratedSets`) of :class:`Pairs_Y` parents:
.. MATH::
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 :meth:`pairs_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)] """ r""" TESTS::
sage: from sage.structure.set_factories_example import XYPairs sage: TestSuite(XYPairs()).run() """ category=EnumeratedSets().Finite()) LazyFamily(range(MAX), self.pairs_y), facade=True, keepkey=False, category=self.category())
r""" Construct the parent for the disjoint union
Construct a parent in :class:`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.
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 """
r""" TESTS::
sage: from sage.structure.set_factories_example import XYPairs sage: XYPairs() # indirect doctest AllPairs """
r""" 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) """
r""" 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)] """ r""" TESTS::
sage: from sage.structure.set_factories_example import XYPairs sage: TestSuite(XYPairs(0)).run() """ category=EnumeratedSets().Finite())
""" TESTS::
sage: from sage.structure.set_factories_example import XYPairs sage: XYPairs(x=1) {(1, b) | b in range(5)} """
r""" TESTS::
sage: from sage.structure.set_factories_example import XYPairs sage: P = XYPairs(x=0); P.an_element() (0, 0) """
r""" 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 """
r""" TESTS::
sage: from sage.structure.set_factories_example import XYPairs sage: list(XYPairs(x=1)) [(1, 0), (1, 1), (1, 2), (1, 3), (1, 4)] """
r""" The set of pairs `(0, y), (1, y), ..., (4, y)`.
It is constructed as the disjoint union (:class:`DisjointUnionEnumeratedSets`) of :class:`SingletonPair` parents:
.. MATH::
S^y := \bigcup_{i = 0,1,..., 4} S_i^y
.. SEEALSO::
:class:`AllPairs` for how to properly construct :class:`DisjointUnionEnumeratedSets` using :class:`~sage.structure.set_factories.ParentWithSetFactory`.
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)] """ r""" TESTS::
sage: from sage.structure.set_factories_example import XYPairs sage: TestSuite(XYPairs(y=1)).run() """ category=EnumeratedSets().Finite()) self, LazyFamily(range(MAX), self.single_pair), facade=True, keepkey=False, category=self.category()) # TODO remove and fix disjoint union.
""" TESTS::
sage: from sage.structure.set_factories_example import XYPairs sage: XYPairs(y=1) {(a, 1) | a in range(5)} """
r""" TESTS::
sage: from sage.structure.set_factories_example import XYPairs sage: XYPairs(y=1).an_element() (0, 1) """
r""" Construct the singleton pair parent
Construct a singleton pair for ``(self.y, letter)`` as a facade parent for ``self``.
.. SEEALSO::
:class:`AllPairs` for how to properly construct :class:`DisjointUnionEnumeratedSets` using :class:`~sage.structure.set_factories.ParentWithSetFactory`.
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 """
r""" 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 """
r""" TESTS::
sage: from sage.structure.set_factories_example import XYPairs sage: P = XYPairs(0,1); P.list() [(0, 1)] """ r""" TESTS::
sage: from sage.structure.set_factories_example import XYPairs sage: TestSuite(XYPairs(0,1)).run() """ category=EnumeratedSets().Finite())
""" TESTS::
sage: from sage.structure.set_factories_example import XYPairs sage: XYPairs(x=2, y=1) {(2, 1)} """
r""" 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 """
r""" TESTS::
sage: from sage.structure.set_factories_example import XYPairs sage: list(XYPairs(0,1)) [(0, 1)] """ |