Ticket #9532: trac_9532-graphs-randstate.patch

File trac_9532-graphs-randstate.patch, 12.3 KB (added by cwitty, 9 years ago)
  • sage/graphs/base/dense_graph.pyx

    # HG changeset patch
    # User Carl Witty <cwitty@newtonlabs.com>
    # Date 1279406972 25200
    # Node ID 4b32bf1f9bc6a755c54203b065151256d338b285
    # Parent  cb1ad1ee5bd452850fde04d47f9618dec4c3e997
    Trac #9532: Fix several places in sage/graphs to use the randstate framework.
    Change doctests to have numbers instead of being totally disabled with "...".
    
    diff -r cb1ad1ee5bd4 -r 4b32bf1f9bc6 sage/graphs/base/dense_graph.pyx
    a b  
    602602    cdef int i, j, k, l, n
    603603    cdef DenseGraph Gnew
    604604    num_verts = 10
     605    # This code deliberately uses random instead of sage.misc.prandom,
     606    # so that every time it is run it does different tests, instead of
     607    # doing the same random stress test every time.  (Maybe it should
     608    # use sage.misc.random_testing?)
    605609    from random import randint
    606610    from sage.graphs.all import DiGraph
    607611    from sage.misc.misc import uniq
  • sage/graphs/base/sparse_graph.pyx

    diff -r cb1ad1ee5bd4 -r 4b32bf1f9bc6 sage/graphs/base/sparse_graph.pyx
    a b  
    12081208    cdef SparseGraph Gnew
    12091209    num_verts = 10
    12101210    Gnew = SparseGraph(num_verts)
     1211    # This code deliberately uses random instead of sage.misc.prandom,
     1212    # so that every time it is run it does different tests, instead of
     1213    # doing the same random stress test every time.  (Maybe it should
     1214    # use sage.misc.random_testing?)
    12111215    from random import randint
    12121216    from sage.graphs.all import DiGraph
    12131217    from sage.misc.misc import uniq
  • sage/graphs/digraph.py

    diff -r cb1ad1ee5bd4 -r 4b32bf1f9bc6 sage/graphs/digraph.py
    a b  
    20822082        EXAMPLES::
    20832083
    20842084            sage: H = DiGraph({0:[1,2],1:[3],2:[3],3:[],5:[1,6],6:[2,3]})
     2085
     2086        The actual layout computed will depend on whether dot2tex and
     2087        graphviz are installed, so we don't test it.
     2088           
    20852089            sage: H.layout_acyclic()
    20862090            {0: [..., ...], 1: [..., ...], 2: [..., ...], 3: [..., ...], 5: [..., ...], 6: [..., ...]}
    20872091
     
    21022106
    21032107            sage: H = DiGraph({0:[1,2],1:[3],2:[3],3:[],5:[1,6],6:[2,3]})
    21042108            sage: H.layout_acyclic_dummy()
    2105             {0: [..., 0], 1: [..., 1], 2: [..., 2], 3: [..., 3], 5: [..., 0], 6: [..., 1]}
     2109            {0: [1.00..., 0], 1: [1.00..., 1], 2: [1.51..., 2], 3: [1.50..., 3], 5: [2.01..., 0], 6: [2.00..., 1]}
    21062110
    21072111            sage: H = DiGraph({0:[1,2],1:[3],2:[3],3:[1],5:[1,6],6:[2,3]})
    21082112            sage: H.layout_acyclic_dummy()
  • sage/graphs/digraph_generators.py

    diff -r cb1ad1ee5bd4 -r 4b32bf1f9bc6 sage/graphs/digraph_generators.py
    a b  
    432432       
    433433        EXAMPLE::
    434434       
    435             sage: digraphs.RandomDirectedGNP(10, .2).num_verts()
     435            sage: D = digraphs.RandomDirectedGNP(10, .2)
     436            sage: D.num_verts()
    436437            10
     438            sage: D.edges(labels=False)
     439            [(0, 1), (0, 3), (0, 6), (0, 8), (1, 4), (3, 7), (4, 1), (4, 8), (5, 2), (5, 6), (5, 8), (6, 4), (7, 6), (8, 4), (8, 5), (8, 7), (8, 9), (9, 3), (9, 4), (9, 6)]
    437440        """
    438         from random import random
     441        from sage.misc.prandom import random
    439442        D = DiGraph(n)
    440443        for i in xrange(n):
    441444            for j in xrange(i):
  • sage/graphs/generic_graph.py

    diff -r cb1ad1ee5bd4 -r 4b32bf1f9bc6 sage/graphs/generic_graph.py
    a b  
    15641564       
    15651565            sage: G = graphs.PetersenGraph()
    15661566            sage: G.get_pos()       
    1567             {0: (..., ...),
     1567            {0: (...e-17, 1.0),
    15681568             ...
    1569              9: (..., ...)}
     1569             9: (0.475..., 0.154...)}
    15701570        """
    15711571        if dim == 2:
    15721572            return self._pos
     
    49344934        paths, the first one from the top-left corner
    49354935        to the bottom-left corner, and the second from
    49364936        the top-right corner to the bottom-right corner
    4937         is ... easy ::
     4937        is easy ::
    49384938
    49394939            sage: g = graphs.GridGraph([5,5])
    49404940            sage: p1,p2 = g.disjoint_routed_paths( [((0,0), (0,4)), ((4,4), (4,0))])
     
    58645864        ``l`` is the label of edge `(u,v)`::
    58655865
    58665866            sage: g.random_edge()
    5867             (...,...,...)
     5867            (3, 4, None)
    58685868        """
    58695869        from sage.misc.prandom import randint
    58705870        it = self.edge_iterator(**kwds)
     
    1093510935
    1093610936            sage: g = digraphs.ButterflyGraph(1)
    1093710937            sage: g.layout()
    10938             {('1', 1): [..., ...],
    10939              ('0', 0): [..., ...],
    10940              ('1', 0): [..., ...],
    10941              ('0', 1): [..., ...]}
     10938            {('1', 1): [2.50..., -0.545...],
     10939             ('0', 0): [2.22..., 0.832...],
     10940             ('1', 0): [1.12..., -0.830...],
     10941             ('0', 1): [0.833..., 0.543...]}
    1094210942
    1094310943            sage: 1+1
    1094410944            2
     
    1094910949
    1095010950
    1095110951            sage: g.layout(dim = 3)
    10952             {('1', 1): [..., ..., ...],
    10953              ('0', 0): [..., ..., ...],
    10954              ('1', 0): [..., ..., ...],
    10955              ('0', 1): [..., ..., ...]}
     10952            {('1', 1): [1.07..., -0.260..., 0.927...],
     10953             ('0', 0): [2.02..., 0.528..., 0.343...],
     10954             ('1', 0): [0.674..., -0.528..., -0.343...],
     10955             ('0', 1): [1.61..., 0.260..., -0.927...]}
    1095610956
    1095710957        Here is the list of all the available layout options::
    1095810958
     
    1100711007        return pos
    1100811008
    1100911009
    11010     def layout_spring(self, heights = None, by_component = True, **options):
     11010    def layout_spring(self, by_component = True, **options):
    1101111011        """
    1101211012        Computes a spring layout for this graph
    1101311013
     
    1102411024        EXAMPLES::
    1102511025
    1102611026            sage: g = graphs.LadderGraph(3) #TODO!!!!
    11027             sage: g.layout_ranked(heights = dict( (i,[i, i+3]) for i in range(3) ))
    11028             {0: [..., 0],
    11029              1: [..., 1],
    11030              2: [..., 2],
    11031              3: [..., 0],
    11032              4: [..., 1],
    11033              5: [..., 2]}
     11027            sage: g.layout_spring()
     11028            {0: [1.28..., -0.943...],
     11029             1: [1.57..., -0.101...],
     11030             2: [1.83..., 0.747...],
     11031             3: [0.531..., -0.757...],
     11032             4: [0.795..., 0.108...],
     11033             5: [1.08..., 0.946...]}
    1103411034            sage: g = graphs.LadderGraph(7)
    11035             sage: g.plot(layout = "ranked", heights = dict( (i,[i, i+7]) for i in range(7) ))
     11035            sage: g.plot(layout = "spring")
    1103611036        """
    1103711037        return spring_layout_fast(self, by_component = by_component, **options)
    1103811038
     
    1106211062
    1106311063            sage: g = graphs.LadderGraph(3)
    1106411064            sage: g.layout_ranked(heights = dict( (i,[i, i+3]) for i in range(3) ))
    11065             {0: [..., 0],
    11066              1: [..., 1],
    11067              2: [..., 2],
    11068              3: [..., 0],
    11069              4: [..., 1],
    11070              5: [..., 2]}
     11065            {0: [0.668..., 0],
     11066             1: [0.667..., 1],
     11067             2: [0.677..., 2],
     11068             3: [1.34..., 0],
     11069             4: [1.33..., 1],
     11070             5: [1.33..., 2]}
    1107111071            sage: g = graphs.LadderGraph(7)
    1107211072            sage: g.plot(layout = "ranked", heights = dict( (i,[i, i+7]) for i in range(7) ))
    1107311073        """
     
    1113011130            sage: H.layout_extend_randomly({('0',0): (0,0), ('1',1): (1,1)})
    1113111131            {('1', 1): (1, 1),
    1113211132             ('0', 0): (0, 0),
    11133              ('1', 0): [0..., 0...],
    11134              ('0', 1): [0..., 0...]}
     11133             ('1', 0): [0.111..., 0.514...],
     11134             ('0', 1): [0.0446..., 0.332...]}
    1113511135        """
    1113611136        assert dim == 2 # 3d not yet implemented
    1113711137        from sage.misc.randstate import current_randstate
     
    1157611576             9: (0.47..., 0.15...)}
    1157711577            sage: P = G.plot(save_pos=True, layout='spring')
    1157811578           
    11579             The following illustrates the format of a position dictionary,
    11580             but due to numerical noise we do not check the values themselves.
     11579            The following illustrates the format of a position dictionary.
    1158111580           
    1158211581            sage: G.get_pos()
    11583             {0: [..., ...],
    11584              1: [..., ...],
    11585              2: [..., ...],
    11586              3: [..., ...],
    11587              4: [..., ...],
    11588              5: [..., ...],
    11589              6: [..., ...],
    11590              7: [..., ...],
    11591              8: [..., ...],
    11592              9: [..., ...]}
     11582            {0: [1.17..., -0.855...],
     11583             1: [1.81..., -0.0990...],
     11584             2: [1.35..., 0.184...],
     11585             3: [1.51..., 0.644...],
     11586             4: [2.00..., -0.507...],
     11587             5: [0.597..., -0.236...],
     11588             6: [2.04..., 0.687...],
     11589             7: [1.46..., -0.473...],
     11590             8: [0.902..., 0.773...],
     11591             9: [2.48..., -0.119...]}
    1159311592       
    1159411593        ::
    1159511594       
  • sage/graphs/generic_graph_pyx.pyx

    diff -r cb1ad1ee5bd4 -r 4b32bf1f9bc6 sage/graphs/generic_graph_pyx.pyx
    a b  
    2020include '../ext/stdsage.pxi'
    2121
    2222# import from Python standard library
    23 from random import random
     23from sage.misc.prandom import random
    2424
    2525# import from third-party library
    2626from sage.graphs.base.dense_graph cimport DenseGraph
     
    5050        sage: for i in range(10): G.add_cycle(range(100*i, 100*i+3))
    5151        sage: from sage.graphs.generic_graph_pyx import spring_layout_fast_split
    5252        sage: spring_layout_fast_split(G)
    53         {0: [..., ...], ..., 502: [..., ...]}
     53        {0: [0.452..., 0.247...], ..., 502: [25.7..., 0.505...]}
    5454       
    5555    AUTHOR:
    5656        Robert Bradshaw
     
    9292        sage: for i in range(10): G.add_cycle(range(100*i, 100*i+3))
    9393        sage: from sage.graphs.generic_graph_pyx import spring_layout_fast
    9494        sage: spring_layout_fast(G)
    95         {0: [..., ...], ..., 502: [..., ...]}
     95        {0: [-0.0733..., 0.157...], ..., 502: [-0.551..., 0.682...]}
    9696
    9797    With ``split=True``, each component of G is layed out separately,
    9898    placing them adjacent to each other. This is done because on a
     
    110110        sage: for i in range(10): G.add_cycle(range(100*i, 100*i+3))
    111111        sage: from sage.graphs.generic_graph_pyx import spring_layout_fast
    112112        sage: spring_layout_fast(G, by_component = True)
    113         {0: [..., ...], ..., 502: [..., ...]}
     113        {0: [2.12..., -0.321...], ..., 502: [26.0..., -0.812...]}
    114114    """
    115115
    116116    if by_component:
  • sage/graphs/graph_generators.py

    diff -r cb1ad1ee5bd4 -r 4b32bf1f9bc6 sage/graphs/graph_generators.py
    a b  
    22342234            2
    22352235            sage: HS.num_verts()
    22362236            50
     2237
     2238        Note that you get a different layout each time you create the graph.
     2239
     2240            sage: HS.layout()[1]
     2241            (-0.844..., 0.535...)
     2242            sage: graphs.HoffmanSingletonGraph().layout()[1]
     2243            (-0.904..., 0.425...)
    22372244        """
    22382245        H = graph.Graph({ \
    22392246        'q00':['q01'], 'q01':['q02'], 'q02':['q03'], 'q03':['q04'], 'q04':['q00'], \
     
    22532260                    H.add_edge(('q%d%d'%(k,con),'p%d%d'%(j,i)))
    22542261        H.name('Hoffman-Singleton graph')
    22552262        from sage.combinat.combinat import permutations
    2256         from random import randint
     2263        from sage.misc.prandom import randint
    22572264        P = permutations([1,2,3,4])
    22582265        qpp = [0]+P[randint(0,23)]
    22592266        ppp = [0]+P[randint(0,23)]
  • sage/graphs/graph_plot.py

    diff -r cb1ad1ee5bd4 -r 4b32bf1f9bc6 sage/graphs/graph_plot.py
    a b  
    126126            but due to numerical noise we do not check the values themselves.
    127127
    128128            sage: g.get_pos()
    129             {0: [..., ...],
    130              1: [..., ...],
    131              2: [..., ...],
    132              3: [..., ...],
    133              4: [..., ...]}
     129            {0: [...e-17, 1.0],
     130             1: [-0.951..., 0.309...],
     131             2: [-0.587..., -0.809...],
     132             3: [0.587..., -0.809...],
     133             4: [0.951..., 0.309...]}
    134134             
    135135            sage: T = list(graphs.trees(7))
    136136            sage: t = T[3]