diff --git a/src/sage/combinat/yang_baxter_graph.py b/src/sage/combinat/yang_baxter_graph.py index 33552e4cc46..3fb02420f27 100644 --- a/src/sage/combinat/yang_baxter_graph.py +++ b/src/sage/combinat/yang_baxter_graph.py @@ -15,6 +15,7 @@ # # https://www.gnu.org/licenses/ # **************************************************************************** +from copy import copy from sage.graphs.digraph import DiGraph from sage.structure.sage_object import SageObject @@ -45,8 +46,8 @@ def YangBaxterGraph(partition=None, root=None, operators=None): OUTPUT: either: - - :class:`YangBaxterGraph_partition` -- if partition is defined - - :class:`YangBaxterGraph_generic` -- if partition is ``None`` + - :class:`YangBaxterGraph_partition` -- if partition is defined + - :class:`YangBaxterGraph_generic` -- if partition is ``None`` EXAMPLES: @@ -108,8 +109,7 @@ def YangBaxterGraph(partition=None, root=None, operators=None): """ if partition is None: return YangBaxterGraph_generic(root=root, operators=operators) - else: - return YangBaxterGraph_partition(partition=Partition(partition)) + return YangBaxterGraph_partition(partition=Partition(partition)) # *********** General class for Yang-Baxter Graphs *********** @@ -203,7 +203,7 @@ def _digraph(self): digraph.add_edge(u, v, l) return digraph - def __hash__(self): + def __hash__(self) -> int: r""" TESTS:: @@ -236,7 +236,7 @@ def __eq__(self, other) -> bool: sage: Y3.__eq__(Y2) False """ - return type(self) is type(other) and self._digraph == other._digraph + return isinstance(other, YangBaxterGraph_generic) and self._digraph == other._digraph def __ne__(self, other) -> bool: r""" @@ -311,7 +311,6 @@ def __copy__(self): sage: Y == B True """ - from copy import copy Y = self.__class__(self._root, self._operators) Y._digraph = copy(self._digraph) return Y @@ -421,9 +420,7 @@ def vertices(self, sort=False) -> list: sage: Y.vertices(sort=True) [(0, 2, 1, 0), (2, 0, 1, 0), (2, 1, 0, 0)] """ - if sort: - return sorted(self) - return list(self) + return sorted(self) if sort else list(self) def edges(self): r""" @@ -505,7 +502,6 @@ def relabel_vertices(self, v, relabel_operator, inplace=True): sage: Y.vertices(sort=True) [(1, 2, 3, 4), (2, 1, 3, 4), (2, 3, 1, 4)] """ - from copy import copy relabelling = self.vertex_relabelling_dict(v, relabel_operator) Y = self if inplace else copy(self) Y._root = relabelling[Y._root] @@ -540,11 +536,7 @@ def relabel_edges(self, edge_dict, inplace=True): sage: Y.edges() [((0, 2, 1, 0), (2, 0, 1, 0), 17), ((2, 0, 1, 0), (2, 1, 0, 0), 27)] """ - if inplace: - Y = self - else: - from copy import copy - Y = copy(self) + Y = self if inplace else copy(self) digraph = Y._digraph for u, v in digraph.edges(sort=False, labels=False): digraph.set_edge_label(u, v, edge_dict[u, v]) @@ -614,7 +606,6 @@ def __copy__(self): sage: Y == B True """ - from copy import copy Y = self.__class__(self._partition) Y._digraph = copy(self._digraph) return Y @@ -634,7 +625,7 @@ def _digraph(self): [((0, 1, 0), (1, 0, 0), Swap positions 0 and 1)] """ digraph = super()._digraph - for (u, v, op) in digraph.edges(sort=True): + for u, v, op in digraph.edges(): digraph.set_edge_label(u, v, SwapOperator(op.position())) return digraph @@ -754,11 +745,10 @@ def relabel_vertices(self, v, inplace=True): Y._digraph.relabel(relabelling, inplace=inplace) Y._vertex_ordering = Y._digraph.vertices(sort=True) return - else: - from copy import copy - Y = copy(self) - Y._root = relabelling[Y._root] - return Y._digraph.relabel(relabelling, inplace=inplace) + + Y = copy(self) + Y._root = relabelling[Y._root] + return Y._digraph.relabel(relabelling, inplace=inplace) # ------------- Some Yang-Baxter operators ------------------ @@ -777,7 +767,7 @@ def __init__(self, i): """ self._position = i - def __hash__(self): + def __hash__(self) -> int: r""" TESTS:: @@ -925,9 +915,8 @@ def __call__(self, u): j = i + 1 if u[i] < u[j]: v = list(u) - (v[j], v[i]) = (v[i], v[j]) + v[j], v[i] = v[i], v[j] if isinstance(u, Permutation): return Permutation(v) return type(u)(v) - else: - return u + return u