Ticket #12477: trac_12477.patch

File trac_12477.patch, 2.3 KB (added by ncohen, 9 years ago)
  • sage/graphs/generic_graph.py

    # HG changeset patch
    # User Nathann Cohen <nathann.cohen@gmail.com>
    # Date 1328745389 -3600
    # Node ID 22e953f1d54e39c08548581e66e75dfd2e8460cf
    # Parent  954068583e87443e74d42c011ab63e1c35da3b2e
    Graph.relabel modifies the dictionary given as an input !
    
    diff --git a/sage/graphs/generic_graph.py b/sage/graphs/generic_graph.py
    a b  
    1490514905            [1, 2]
    1490614906            sage: G.get_pos()
    1490714907            {0: (0, 0), 1: (2, 0), 2: (3, 0), 3: (4, 0)}
    14908         """
     14908
     14909        Check that #12477 is fixed::
     14910
     14911            sage: g = Graph({1:[2,3]})
     14912            sage: rel = {1:'a', 2:'b'}
     14913            sage: g.relabel(rel)
     14914            sage: g.vertices()
     14915            [3, 'a', 'b']
     14916            sage: rel
     14917            {1: 'a', 2: 'b'}
     14918        """
     14919        from sage.groups.perm_gps.permgroup_element import PermutationGroupElement
     14920
     14921        # If perm is not a dictionary, we build one !
     14922
    1490914923        if perm is None:
    1491014924            verts = self.vertices() # vertices() returns a sorted list:
    1491114925            perm = {}; i = 0        # this guarantees consistent relabeling
    1491214926            for v in verts:
    1491314927                perm[v] = i
    1491414928                i += 1
    14915         if isinstance(perm, (list, tuple)):
     14929
     14930        elif isinstance(perm, dict):
     14931
     14932            # If all vertices do not have a new label, the code will touch the
     14933            # dictionary. Let us keep the one we received from the user clean !
     14934            from copy import copy
     14935            perm = copy(perm)
     14936
     14937        elif isinstance(perm, (list, tuple)):
    1491614938            perm = dict( [ [i,perm[i]] for i in xrange(len(perm)) ] )
    14917         from sage.groups.perm_gps.permgroup_element import PermutationGroupElement
    14918         if isinstance(perm, PermutationGroupElement):
     14939
     14940        elif isinstance(perm, PermutationGroupElement):
    1491914941            n = self.order()
    1492014942            ddict = {}
    1492114943            llist = perm.list()
     
    1492414946            if n > 0:
    1492514947                ddict[0] = llist[n-1]%n
    1492614948            perm = ddict
    14927         if callable(perm):
     14949
     14950        elif callable(perm):
    1492814951            perm = dict( [ i, perm(i) ] for i in self.vertices() )
    14929         if not isinstance(perm, dict):
     14952
     14953        else:
    1493014954            raise TypeError("Type of perm is not supported for relabeling.")
    1493114955
    1493214956        if not inplace: