Ticket #12872: trac_12872.patch

File trac_12872.patch, 5.8 KB (added by ncohen, 9 years ago)
  • sage/combinat/permutation.py

    # HG changeset patch
    # User Nathann Cohen <nathann.cohen@gmail.com>
    # Date 1335343538 -7200
    # Node ID 1719ee26d65b125a8f81b87e47985edf823bc7d6
    # Parent  38d39a4ac8f7e50cabac4c7c5e45d2ca3053ec90
    A show method for permutations
    
    diff --git a/sage/combinat/permutation.py b/sage/combinat/permutation.py
    a b  
    11341134    def inversions(self):
    11351135        r"""
    11361136        Returns a list of the inversions of permutation p.
    1137        
     1137
    11381138        EXAMPLES::
    1139        
     1139
    11401140            sage: Permutation([3,2,4,1,5]).inversions()
    11411141            [[0, 1], [0, 3], [1, 3], [2, 3]]
    11421142        """
     
    11511151
    11521152        return inversion_list
    11531153
     1154    def show(self, representation = "cycles", orientation = "landscape", **args):
     1155        r"""
     1156        Displays the permutation as a drawing.
     1157
     1158        INPUT:
     1159
     1160        - ``representation`` -- different kinds of drawings are available
     1161
     1162            - ``"cycles"`` (default) -- the permutation is displayed as a
     1163              collection of directed cycles
     1164
     1165            - ``"braid"`` -- the permutation is displayed as segments linking
     1166              each element `1, ..., n` to its image on a parallel line.
     1167
     1168              When using this drawing, it is also possible to display the
     1169              permutation horizontally (``orientation = "landscape"``, default
     1170              option) or vertically (``orientation = "portrait"``).
     1171
     1172            - ``"chord-diagram"`` -- the permutation is displayed as a directed
     1173              graph, all of its vertices being located on a circle.
     1174
     1175        All additional arguments are forwarded to the ``show`` subcalls.
     1176
     1177        EXAMPLES::
     1178
     1179            sage: Permutations(20).random_element().show(representation = "cycles")
     1180            sage: Permutations(20).random_element().show(representation = "chord-diagram")
     1181            sage: Permutations(20).random_element().show(representation = "braid")
     1182            sage: Permutations(20).random_element().show(representation = "braid", orientation='portrait')
     1183
     1184        TESTS::
     1185
     1186            sage: Permutations(20).random_element().show(representation = "modern_art")
     1187            Traceback (most recent call last):
     1188            ...
     1189            ValueError: The value of 'representation' must be equal to 'cycles', 'chord-digraph' or 'braid'
     1190        """
     1191
     1192        if representation == "cycles" or representation == "chord-diagram":
     1193            from sage.graphs.digraph import DiGraph
     1194            d = DiGraph(loops = True)
     1195            for i in range(len(self)):
     1196                d.add_edge(i+1, self[i])
     1197
     1198            if representation == "cycles":
     1199                d.show(**args)
     1200            else:
     1201                d.show(layout = "circular", **args)
     1202
     1203        elif representation == "braid":
     1204            from sage.plot.line import line
     1205            from sage.plot.text import text
     1206
     1207            if orientation == "landscape":
     1208                r = lambda x,y : (x,y)
     1209            elif orientation == "portrait":
     1210                r = lambda x,y : (-y,x)
     1211            else:
     1212                raise ValueError("The value of 'orientation' must be either "+
     1213                                 "'landscape' or 'portrait'.")
     1214
     1215            p = self[:]
     1216
     1217            L = line([r(1,1)])
     1218            for i in range(len(p)):
     1219                L += line([r(i,1.0), r(p[i]-1,0)])
     1220                L += text(str(i), r(i,1.05)) + text(str(i), r(p[i]-1,-.05))
     1221
     1222            return L.show(axes = False, **args)
     1223
     1224        else:
     1225            raise ValueError("The value of 'representation' must be equal to "+
     1226                             "'cycles', 'chord-digraph' or 'braid'")
    11541227
    11551228
    11561229    def number_of_inversions(self):
    11571230        r"""
    11581231        Returns the number of inversions in the permutation p.
    1159        
     1232
    11601233        An inversion of a permutation is a pair of elements (p[i],p[j])
    11611234        with i < j and p[i] > p[j].
    1162        
     1235
    11631236        REFERENCES:
    11641237
    11651238        - http://mathworld.wolfram.com/PermutationInversion.html
  • sage/graphs/graph_plot.py

    diff --git a/sage/graphs/graph_plot.py b/sage/graphs/graph_plot.py
    a b  
    108108    def _repr_(self):
    109109        """
    110110        Returns a string representation of a GraphPlot object.
    111        
     111
    112112        EXAMPLE:
    113        
     113
    114114        This function is called implicitly by the code below::
    115115
    116116            sage: g = Graph({0:[1,2], 2:[3], 4:[0,1]})
     
    118118            GraphPlot object for Graph on 5 vertices
    119119        """
    120120        return "GraphPlot object for %s"%self._graph
    121        
     121
    122122    def set_pos(self):
    123123        """
    124124        Sets the position plotting parameters for this GraphPlot.
    125        
     125
    126126        EXAMPLES:
    127        
     127
    128128        This function is called implicitly by the code below::
    129129
    130130            sage: g = Graph({0:[1,2], 2:[3], 4:[0,1]})
     
    142142             2: [-0.587..., -0.809...],
    143143             3: [0.587..., -0.809...],
    144144             4: [0.951..., 0.309...]}
    145              
     145
    146146            sage: T = list(graphs.trees(7))
    147147            sage: t = T[3]
    148148            sage: t.plot(heights={0:[0], 1:[4,5,1], 2:[2], 3:[3,6]})
     
    153153        isn't a bug in itself but makes it too easy to accidentally
    154154        introduce a bug elsewhere, such as in set_edges (trac #10124),
    155155        via silent truncating division of integers::
    156        
     156
    157157            sage: g = graphs.FruchtGraph()
    158158            sage: gp = g.graphplot()
    159159            sage: set(map(type, flatten(gp._pos.values())))
     
    165165
    166166        """
    167167        self._pos = self._graph.layout(**self._options)
    168         # make sure the positions are floats (trac #10124) 
     168        # make sure the positions are floats (trac #10124)
    169169        self._pos = dict((k,(float(v[0]), float(v[1]))) for k,v in self._pos.iteritems())
    170170
    171171    def set_vertices(self, **vertex_options):