# Ticket #14464: trac-14464-cleanup_of_yang-baxter-fc.patch

File trac-14464-cleanup_of_yang-baxter-fc.patch, 21.1 KB (added by chapoton, 9 years ago)
• ## doc/en/reference/combinat/index.rst

```# HG changeset patch
# User Frederic Chapoton <chapoton at math.univ-lyon1.fr>
# Date 1366231072 -7200
# Node ID 5f66b9c4f0d2c6fa31de317977e18df693be924f
# Parent  f644a39d332f0b9e6cd4a3dc892826298df9969e
trac #14464 clean up of yang baxter graph

diff --git a/doc/en/reference/combinat/index.rst b/doc/en/reference/combinat/index.rst```
 a Combinatorics sage/combinat/enumeration_mod_permgroup sage/combinat/restricted_growth sage/combinat/yamanouchi sage/combinat/yang_baxter_graph sage/combinat/gelfand_tsetlin_patterns sage/combinat/graph_path sage/combinat/matrices/latin
• ## sage/combinat/yang_baxter_graph.py

`diff --git a/sage/combinat/yang_baxter_graph.py b/sage/combinat/yang_baxter_graph.py`
 a def YangBaxterGraph(partition=None, root The user needs to provide either ``partition`` or both ``root`` and ``operators``, where - ``partition`` - a partition of a positive integer - ``partition`` -- a partition of a positive integer - ``root`` - the root vertex - ``operator`` - a function that maps vertices u to a list of tuples of the form (v, l) where v is a successors of u and l is the label of the edge from u to v. - ``root`` -- the root vertex - ``operator`` - a function that maps vertices `u` to a list of tuples of the form `(v, l)` where `v` is a successor of `u` and `l` is the label of the edge from `u` to `v`. OUTPUT: - Either: - :class:`YangBaxterGraph_partition` - if partition is defined - :class:`YangBaxterGraph_generic` - if partition is None - :class:`YangBaxterGraph_generic` - if partition is ``None`` EXAMPLES: def YangBaxterGraph(partition=None, root sage: Y.vertices() [(2, 0, 1, 0), (2, 1, 0, 0), (0, 2, 1, 0)] The ``partition`` keyword is shorthand for the above construction. The ``partition`` keyword is a shorthand for the above construction. :: def YangBaxterGraph(partition=None, root The permutahedron can be realized as a Yang-Baxter graph. :: sage: from sage.combinat.yang_baxter_graph import SwapIncreasingOperator sage: swappers = [SwapIncreasingOperator(i) for i in range(3)] sage: Y = YangBaxterGraph(root=(1,2,3,4), operators=swappers); Y class YangBaxterGraph_generic(SageObject r""" A class to model the Yang-Baxter graph defined by ``root`` and ``operators``. INPUT: - ``root`` - the root vertex of the graph - ``root`` -- the root vertex of the graph - ``operators`` - a list of callables that map vertices to (new) - ``operators`` -- a list of callables that map vertices to (new) vertices. .. note:: .. NOTE:: This is a lazy implementation: the digraph is only computed when it is needed. EXAMPLES:: sage: from sage.combinat.yang_baxter_graph import SwapIncreasingOperator sage: ops = [SwapIncreasingOperator(i) for i in range(4)] sage: Y = YangBaxterGraph(root=(1,0,2,1,0), operators=ops); Y class YangBaxterGraph_generic(SageObject self._root = root self._operators = operators def _succesors(self, u): def _successors(self, u): r""" Return a list of tuples for the form `(op(u), op)`, where op is one of the operators defining self. is one of the operators defining ``self``. EXAMPLES:: sage: from sage.combinat.yang_baxter_graph import SwapIncreasingOperator sage: ops = [SwapIncreasingOperator(i) for i in range(4)] sage: Y = YangBaxterGraph(root=(1,0,2,1,0), operators=ops) sage: Y._succesors((1,0,2,1,0)) sage: Y._successors((1,0,2,1,0)) [((1, 2, 0, 1, 0), Swap-if-increasing at position 1)] """ successors = set() class YangBaxterGraph_generic(SageObject def __repr__(self): r""" EXAMPLES:: sage: from sage.combinat.yang_baxter_graph import SwapIncreasingOperator sage: ops = [SwapIncreasingOperator(i) for i in range(2)] sage: Y = YangBaxterGraph(root=(1,2,3), operators=ops) class YangBaxterGraph_generic(SageObject attribute. EXAMPLES:: sage: from sage.combinat.yang_baxter_graph import SwapIncreasingOperator sage: ops = [SwapIncreasingOperator(i) for i in range(2)] sage: Y = YangBaxterGraph(root=(1,2,3), operators=ops) class YangBaxterGraph_generic(SageObject queue = [self._root] while queue: u = queue.pop() for (v, l) in self._succesors(u): for (v, l) in self._successors(u): if v not in digraph: queue.append(v) digraph.add_edge(u, v, l) class YangBaxterGraph_generic(SageObject def __eq__(self, other): r""" EXAMPLES:: sage: from sage.combinat.yang_baxter_graph import SwapIncreasingOperator sage: ops = [SwapIncreasingOperator(i) for i in range(4)] sage: Y1 = YangBaxterGraph(root=(1,0,2,1,0), operators=ops) class YangBaxterGraph_generic(SageObject Test non-equality. EXAMPLES:: sage: from sage.combinat.yang_baxter_graph import SwapIncreasingOperator sage: ops = [SwapIncreasingOperator(i) for i in range(4)] sage: Y1 = YangBaxterGraph(root=(1,0,2,1,0), operators=ops) class YangBaxterGraph_generic(SageObject def __iter__(self): r""" Returns an iterator of the vertices in self. Returns an iterator of the vertices in ``self``. EXAMPLES:: sage: from sage.combinat.yang_baxter_graph import SwapIncreasingOperator sage: ops = [SwapIncreasingOperator(i) for i in range(4)] sage: Y = YangBaxterGraph(root=(1,0,2,1,0), operators=ops) class YangBaxterGraph_generic(SageObject def __len__(self): r""" Returns the number of vertices in self. Returns the number of vertices in ``self``. EXAMPLES:: sage: from sage.combinat.yang_baxter_graph import SwapIncreasingOperator sage: ops = [SwapIncreasingOperator(i) for i in range(4)] sage: Y = YangBaxterGraph(root=(1,0,2,1,0), operators=ops) class YangBaxterGraph_generic(SageObject def __copy__(self): r""" Returns a copy of self.  Note that :meth:`.copy` is now deprecated. Returns a copy of ``self``. EXAMPLES:: sage: from sage.combinat.yang_baxter_graph import SwapIncreasingOperator sage: ops = [SwapIncreasingOperator(i) for i in range(3)] sage: Y = YangBaxterGraph(root=(1,0,2,1,0), operators=ops); Y class YangBaxterGraph_generic(SageObject Y._digraph = copy(self._digraph) return Y def copy(self): r""" Returns a copy of self.  This method is deprecated. EXAMPLES:: sage: from sage.combinat.yang_baxter_graph import SwapIncreasingOperator sage: ops = [SwapIncreasingOperator(i) for i in range(3)] sage: Y = YangBaxterGraph(root=(1,0,2,1,0), operators=ops); Y Yang-Baxter graph with root vertex (1, 0, 2, 1, 0) sage: B = Y.copy(); B doctest:...: DeprecationWarning: the .copy() method is deprecated; please use the copy() function instead, for example, copy(g) See http://trac.sagemath.org/6522 for details. Yang-Baxter graph with root vertex (1, 0, 2, 1, 0) """ from copy import copy from sage.misc.superseded import deprecation deprecation(6522, "the .copy() method is deprecated; please use the copy() function instead, for example, copy(g)") return copy(self) def _edges_in_bfs(self): r""" Returns an iterator of the edges of the digraph traversed in a breadth-first search of the vertices beginning at ``self.root()``. EXAMPLES:: sage: from sage.combinat.yang_baxter_graph import SwapIncreasingOperator sage: ops = [SwapIncreasingOperator(i) for i in range(4)] sage: Y = YangBaxterGraph(root=(1,0,2,1,0), operators=ops) class YangBaxterGraph_generic(SageObject def root(self): r""" Returns the root vertex of ``self``. If ``self`` is the Yang-Baxter graph of the partition `[p_1,p_2,\dots,p_k]`, then this is the vertex `(p_k-1,p_k-2,\dots,0,\dots,p_1-1,p_1-2,\dots,0)`. Returns the root vertex of ``self``. If ``self`` is the Yang-Baxter graph of the partition `[p_1,p_2,\dots,p_k]`, then this is the vertex `(p_k-1,p_k-2,\dots,0,\dots,p_1-1,p_1-2,\dots,0)`. EXAMPLES:: sage: from sage.combinat.yang_baxter_graph import SwapIncreasingOperator sage: ops = [SwapIncreasingOperator(i) for i in range(4)] sage: Y = YangBaxterGraph(root=(1,0,2,1,0), operators=ops) class YangBaxterGraph_generic(SageObject Return the successors of the vertex ``v``. EXAMPLES:: sage: from sage.combinat.yang_baxter_graph import SwapIncreasingOperator sage: ops = [SwapIncreasingOperator(i) for i in range(4)] sage: Y = YangBaxterGraph(root=(1,0,2,1,0), operators=ops) class YangBaxterGraph_generic(SageObject sage: Y.successors((1, 2, 0, 1, 0)) [(2, 1, 0, 1, 0), (1, 2, 1, 0, 0)] """ return [a for (a,b) in self._succesors(v)] return [a for (a,b) in self._successors(v)] def plot(self, *args, **kwds): r""" Plots ``self`` as a digraph. EXAMPLES:: sage: from sage.combinat.yang_baxter_graph import SwapIncreasingOperator sage: ops = [SwapIncreasingOperator(i) for i in range(4)] sage: Y = YangBaxterGraph(root=(1,0,2,1,0), operators=ops) class YangBaxterGraph_generic(SageObject Returns the vertices of ``self``. EXAMPLES:: sage: from sage.combinat.yang_baxter_graph import SwapIncreasingOperator sage: ops = [SwapIncreasingOperator(i) for i in range(3)] sage: Y = YangBaxterGraph(root=(0,2,1,0), operators=ops) class YangBaxterGraph_generic(SageObject Returns the (labelled) edges of ``self``. EXAMPLES:: sage: from sage.combinat.yang_baxter_graph import SwapIncreasingOperator sage: ops = [SwapIncreasingOperator(i) for i in range(3)] sage: Y = YangBaxterGraph(root=(0,2,1,0), operators=ops) class YangBaxterGraph_generic(SageObject the object obtained from ``v`` by applying the ``relabel_operator`` along a path from the root to ``u``. Note that the root is paired with ``v``. INPUT: - ``v`` - an object - ``v`` -- an object - ``relabel_operator`` - function mapping a vertex and a label to - ``relabel_operator`` -- function mapping a vertex and a label to the image of the vertex OUTPUT: - dictionary pairing vertices with the corresponding image of ``v`` EXAMPLES:: sage: from sage.combinat.yang_baxter_graph import SwapIncreasingOperator sage: ops = [SwapIncreasingOperator(i) for i in range(3)] sage: Y = YangBaxterGraph(root=(0,2,1,0), operators=ops) class YangBaxterGraph_generic(SageObject sage: Y.vertex_relabelling_dict((1,2,3,4), relabel_operator) {(2, 0, 1, 0): (2, 1, 3, 4), (2, 1, 0, 0): (2, 3, 1, 4), (0, 2, 1, 0): (1, 2, 3, 4)} """ digraph = self._digraph relabelling = {self._root:v} for (u,w,i) in self._edges_in_bfs(): relabelling[w] = relabel_operator(i, relabelling[u]) class YangBaxterGraph_generic(SageObject r""" Relabel the vertices ``u`` of ``self`` by the object obtained from ``u`` by applying the ``relabel_operator`` to ``v`` along a path from ``self.root()`` to ``u``. Note that the ``self.root()`` is paired with ``v``. a path from ``self.root()`` to ``u``. Note that the ``self.root()`` is paired with ``v``. INPUT: - ``v`` - tuple, Permutation, CombinatorialObject - ``v`` -- tuple, Permutation, CombinatorialObject - ``inplace`` - if True, modifies self; otherwise returns a modified copy of self. - ``inplace`` -- if ``True``, modifies ``self``; otherwise returns a modified copy of ``self``. EXAMPLES:: class YangBaxterGraph_generic(SageObject def relabel_edges(self, edge_dict, inplace=True): r""" Relabel the edges of self. Relabel the edges of ``self``. INPUT: - ``edge_dict`` - a dictionary keyed by the (unlabelled) edges. - ``edge_dict`` -- a dictionary keyed by the (unlabelled) edges. EXAMPLES:: sage: from sage.combinat.yang_baxter_graph import SwapIncreasingOperator class YangBaxterGraph_generic(SageObject sage: Y.edges() [((0, 2, 1, 0), (2, 0, 1, 0), 17), ((2, 0, 1, 0), (2, 1, 0, 0), 27)] """ from copy import copy if inplace: Y = self else: from copy import copy Y = copy(self) digraph = Y._digraph for (u,v,i) in digraph.edges(): class YangBaxterGraph_partition(YangBaxt def __init__(self, partition): r""" A class to model the Yang-Baxter graph of a partition. The Yang-Baxter graph defined by a partition `[p_1,\dots,p_k]` is the labelled directed graph with vertex set obtained by bubble-sorting `(p_k-1,p_k-2,\dots,0,\dots,p_1-1,p_1-2,\dots,0)`; there is an arrow from `u` to `v` labelled by `i` if `v` is obtained by swapping the `i`-th and `(i+1)`-th elements of `u`. .. note:: This is a lazy implementation: the digraph is only computed when it is needed. class YangBaxterGraph_partition(YangBaxt def __repr__(self): r""" EXAMPLES:: sage: Y = YangBaxterGraph(partition=[3,2]) sage: Y.__repr__() 'Yang-Baxter graph of [3, 2], with top vertex (1, 0, 2, 1, 0)' class YangBaxterGraph_partition(YangBaxt def __copy__(self): r""" Returns a copy of self.  Note that :meth:`.copy` is now deprecated. Returns a copy of ``self``. EXAMPLES:: sage: Y = YangBaxterGraph(partition=[3,2]); Y Yang-Baxter graph of [3, 2], with top vertex (1, 0, 2, 1, 0) sage: B = copy(Y); B class YangBaxterGraph_partition(YangBaxt Y._digraph = copy(self._digraph) return Y def copy(self): r""" Returns a copy of self.  Note that :meth:`.copy` is now deprecated. EXAMPLES:: sage: Y = YangBaxterGraph(partition=[3,2]); Y Yang-Baxter graph of [3, 2], with top vertex (1, 0, 2, 1, 0) sage: B = Y.copy(); B doctest:...: DeprecationWarning: the .copy() method is deprecated; please use the copy() function instead, for example, copy(g) See http://trac.sagemath.org/6522 for details. Yang-Baxter graph of [3, 2], with top vertex (1, 0, 2, 1, 0) """ from copy import copy from sage.misc.superseded import deprecation deprecation(6522, "the .copy() method is deprecated; please use the copy() function instead, for example, copy(g)") return copy(self) @lazy_attribute def _digraph(self): r""" class YangBaxterGraph_partition(YangBaxt attribute. EXAMPLES:: sage: Y = YangBaxterGraph(partition=[2,1]) sage: Y._digraph Digraph on 2 vertices class YangBaxterGraph_partition(YangBaxt r""" Iterate over the vertices ``self``. NOTES: The vertices are first sorted using Python's sorted command. .. NOTE:: The vertices are first sorted using Python's sorted command. EXAMPLES: sage: Y = YangBaxterGraph(partition=[3,2]) class YangBaxterGraph_partition(YangBaxt INPUT: - ``i`` - positive integer between 1 and len(u)-1, inclusive - ``i`` -- positive integer between 1 and len(u)-1, inclusive - ``u`` - tuple, list, permutation, CombinatorialObject, .... - ``u`` -- tuple, list, permutation, CombinatorialObject, .... EXAMPLES:: sage: Y = YangBaxterGraph(partition=[3,1]) sage: from sage.combinat.yang_baxter_graph import SwapOperator sage: ops = [SwapOperator(i) for i in range(3)] class YangBaxterGraph_partition(YangBaxt def vertex_relabelling_dict(self, v): r""" Return a dictionary pairing vertices ``u`` of self with the object Return a dictionary pairing vertices ``u`` of ``self`` with the object obtained from ``v`` by applying transpositions corresponding to the edges labels along a path from the root to ``u``. Note that the root is paired with ``v``. edges labels along a path from the root to ``u``. Note that the root is paired with ``v``. INPUT: - ``v`` - an object - ``v`` -- an object OUTPUT: - dictionary pairing vertices with the corresponding image of v - dictionary pairing vertices with the corresponding image of ``v`` EXAMPLES:: sage: Y = YangBaxterGraph(partition=[3,1]) sage: Y.vertex_relabelling_dict((1,2,3,4)) {(2, 0, 1, 0): (2, 1, 3, 4), (2, 1, 0, 0): (2, 3, 1, 4), (0, 2, 1, 0): (1, 2, 3, 4)} class YangBaxterGraph_partition(YangBaxt INPUT: - ``v`` - tuple, Permutation, CombinatorialObject - ``inplace`` - if True, modifies self; otherwise returns a modified copy of self. - ``v`` -- tuple, Permutation, CombinatorialObject - ``inplace`` -- if ``True``, modifies ``self``; otherwise returns a modified copy of ``self``. EXAMPLES:: sage: Y = YangBaxterGraph(partition=[3,1]); Y Yang-Baxter graph of [3, 1], with top vertex (0, 2, 1, 0) sage: d = Y.relabel_vertices((1,2,3,4), inplace=False); d class SwapOperator(SageObject): graphs). EXAMPLES:: sage: from sage.combinat.yang_baxter_graph import SwapOperator sage: s3 = SwapOperator(3) sage: s3.__str__() class SwapOperator(SageObject): def __call__(self, u): r""" Return the object obtained from swapping the items in positions ``i`` and ``i+1`` of ``u``. ``i`` and ``i+1`` of ``u``. EXAMPLES:: sage: from sage.combinat.yang_baxter_graph import SwapOperator class SwapOperator(SageObject): def position(self): r""" Self is the operator that swaps positions ``i`` and ``i+1``. This ``self`` is the operator that swaps positions ``i`` and ``i+1``. This method returns ``i``. EXAMPLES:: class SwapIncreasingOperator(SwapOperato INPUT: - ``i`` - positive integer between ``1`` and ``len(u)-1``, inclusive - ``u`` - tuple, list, permutation, CombinatorialObject, .... - ``i`` -- positive integer between ``1`` and ``len(u)-1``, inclusive - ``u`` -- tuple, list, permutation, CombinatorialObject, .... EXAMPLES:: sage: Y = YangBaxterGraph(partition=[2,2]) sage: from sage.combinat.yang_baxter_graph import SwapIncreasingOperator sage: operators = [SwapIncreasingOperator(i) for i in range(3)]