Ticket #7709: trac_7709.patch

File trac_7709.patch, 9.9 KB (added by ncohen, 10 years ago)
  • sage/graphs/digraph.py

    # HG changeset patch
    # User Nathann Cohen <nathann.cohen@gmail.com>
    # Date 1280892586 -28800
    # Node ID fd33d03764111b0caa73d743511e78fd76a5d236
    # Parent  426be7b253ad9c3137e733aba26165a3de5b832f
    trac 7709 - Graph/DiGraph constructor with a list of edges as input
    
    diff -r 426be7b253ad -r fd33d0376411 sage/graphs/digraph.py
    a b  
    314314            True
    315315            sage: g.get_pos() == graphs.PetersenGraph().get_pos()
    316316            True
     317
     318        Invalid sequence of edges given as an input (they do not all
     319        have the same length)::
     320
     321            sage: g = DiGraph([(1,2),(2,3),(2,3,4)])
     322            Traceback (most recent call last):
     323            ...
     324            ValueError: Edges input must all follow the same format.
     325
     326
     327        Two different labels given for the same edge in a graph
     328        without multiple edges::
     329
     330            sage: g = Graph([(1,2,3),(1,2,4)], multiedges = False)
     331            Traceback (most recent call last):
     332            ...
     333            ValueError: Two different labels given for the same edge in a graph without multiple edges.
     334
    317335        """
    318336        msg = ''
    319337        GenericGraph.__init__(self)
     
    359377        if format is None and data is None:
    360378            format = 'int'
    361379            data = 0
     380
     381        # Input is a list of edges
     382        if format is None and isinstance(data, list):
     383
     384            # If we are given a list (we assume it is a list of
     385            # edges), we convert the problem to a dict_of_dicts or a
     386            # dict_of_lists
     387
     388            edges = data
     389
     390            # Along the process, we are assuming all edges have the
     391            # same length. If it is not true, a ValueError will occur
     392            try:
     393
     394                # The edges are not labelled
     395                if len(data[0]) == 2:
     396                    data = {}
     397                    for u,v in edges:
     398                        if not u in data:
     399                            data[u] = []
     400
     401                        data[u].append(v)
     402
     403                    format = 'dict_of_lists'
     404
     405                # The edges are labelled
     406                elif len(data[0]) == 3:
     407                    data = {}
     408                    for u,v,l in edges:
     409
     410                        if not u in data:
     411                            data[u] = {}
     412
     413                        # Now the key exists, and is a dictionary ...
     414
     415                        # If we notice for the first time that there
     416                        # are multiple edges, we update the whole
     417                        # dictionary so that data[u][v] is a list
     418
     419                        if (multiedges is None and
     420                            (u in data[v])):
     421                            multiedges = True
     422                            for uu, dd in data.iteritems():
     423                                for vv, ddd in dd.iteritems():
     424                                    dd[vv] = [ddd]
     425
     426                        # If multiedges is set to False while the same
     427                        # edge is present in the list with different
     428                        # values of its label
     429                        elif (multiedges is False and
     430                            (v in data[u] and data[u][v] != l)):
     431                                raise ValueError("MULTIEDGE")
     432
     433                        # Now we are behaving as if multiedges == None
     434                        # means multiedges = False. If something bad
     435                        # happens later, the whole dictionary will be
     436                        # updated anyway
     437
     438                        if multiedges is True:
     439                            if v not in data[u]:
     440                                data[u][v] = []
     441
     442                            data[u][v].append(l)
     443
     444                        else:
     445                            data[u][v] = l
     446
     447                    format = 'dict_of_dicts'
     448
     449            except ValueError as ve:
     450                if str(ve) == "MULTIEDGE":
     451                    raise ValueError("Two different labels given for the same edge in a graph without multiple edges.")
     452                else:
     453                    raise ValueError("Edges input must all follow the same format.")
     454
    362455        if format is None:
    363456            import networkx
    364457            data = networkx.MultiDiGraph(data)
  • sage/graphs/graph.py

    diff -r 426be7b253ad -r fd33d0376411 sage/graphs/graph.py
    a b  
    209209       ...
    210210       ValueError: There must be two nonzero entries (-1 & 1) per column.
    211211
     212-  a list of edges::
     213
     214       sage: g = Graph([(1,3),(3,8),(5,2)])
     215       sage: g
     216       Graph on 5 vertices
    212217
    213218Generators
    214219----------
     
    729734            ...
    730735            ValueError: Non-symmetric or non-square matrix assumed to be an incidence matrix: Each column represents an edge: -1 goes to 1.
    731736
     737    #. a list of edges, or labelled edges::
     738
     739          sage: g = Graph([(1,3),(3,8),(5,2)])
     740          sage: g
     741          Graph on 5 vertices
     742
     743          ::
     744
     745          sage: g = Graph([(1,2,"Peace"),(7,-9,"and"),(77,2, "Love")])
     746          sage: g
     747          Graph on 5 vertices
    732748
    733749    #. A NetworkX MultiGraph::
    734750
     
    788804            sage: h = Graph(d)
    789805            sage: g.get_pos() == h.get_pos()
    790806            True
    791            
     807
     808        Invalid sequence of edges given as an input (they do not all
     809        have the same length)::
     810
     811            sage: g = Graph([(1,2),(2,3),(2,3,4)])
     812            Traceback (most recent call last):
     813            ...
     814            ValueError: Edges input must all follow the same format.
     815
     816
     817        Two different labels given for the same edge in a graph
     818        without multiple edges::
     819
     820            sage: g = Graph([(1,2,3),(1,2,4)], multiedges = False)
     821            Traceback (most recent call last):
     822            ...
     823            ValueError: Two different labels given for the same edge in a graph without multiple edges.
    792824        """
    793825        GenericGraph.__init__(self)
    794826        msg = ''
     
    841873        if format is None and data is None:
    842874            format = 'int'
    843875            data = 0
     876
     877        # Input is a list of edges
     878        if format is None and isinstance(data, list):
     879
     880            # If we are given a list (we assume it is a list of
     881            # edges), we convert the problem to a dict_of_dicts or a
     882            # dict_of_lists
     883
     884            edges = data
     885
     886            # Along the process, we are assuming all edges have the
     887            # same length. If it is not true, a ValueError will occur
     888            try:
     889
     890                # The edges are not labelled
     891                if len(data[0]) == 2:
     892                    data = {}
     893                    for u,v in edges:
     894                        if not u in data:
     895                            data[u] = []
     896                        if not v in data:
     897                            data[v] = []
     898                        data[u].append(v)
     899                        data[v].append(u)
     900
     901                    format = 'dict_of_lists'
     902
     903                # The edges are labelled
     904                elif len(data[0]) == 3:
     905                    data = {}
     906                    for u,v,l in edges:
     907
     908                        if not u in data:
     909                            data[u] = {}
     910                        if not v in data:
     911                            data[v] = {}
     912
     913                        # Now the keys exists, and are dictionaries ...
     914
     915
     916                        # If we notice for the first time that there
     917                        # are multiple edges, we update the whole
     918                        # dictionary so that data[u][v] is a list
     919
     920                        if (multiedges is None and
     921                            (u in data[v])):
     922                            multiedges = True
     923                            for uu, dd in data.iteritems():
     924                                for vv, ddd in dd.iteritems():
     925                                    dd[vv] = [ddd]
     926
     927                        # If multiedges is set to False while the same
     928                        # edge is present in the list with different
     929                        # values of its label
     930                        elif (multiedges is False and
     931                            (u in data[v] and data[v][u] != l)):
     932                                raise ValueError("MULTIEDGE")
     933
     934                        # Now we are behaving as if multiedges == None
     935                        # means multiedges = False. If something bad
     936                        # happens later, the whole dictionary will be
     937                        # updated anyway
     938
     939                        if multiedges is True:
     940                            if v not in data[u]:
     941                                data[u][v] = []
     942                                data[v][u] = []
     943
     944                            data[u][v].append(l)
     945                            data[v][u].append(l)
     946
     947                        else:
     948                            data[u][v] = l
     949                            data[v][u] = l
     950
     951                    format = 'dict_of_dicts'
     952
     953            except ValueError as ve:
     954                if str(ve) == "MULTIEDGE":
     955                    raise ValueError("Two different labels given for the same edge in a graph without multiple edges.")
     956                else:
     957                    raise ValueError("Edges input must all follow the same format.")
     958
     959               
    844960        if format is None:
    845961            import networkx
    846962            data = networkx.MultiGraph(data)
  • sage/matrix/matrix0.pyx

    diff -r 426be7b253ad -r fd33d0376411 sage/matrix/matrix0.pyx
    a b  
    464464
    465465    def set_immutable(self):
    466466        r"""
    467         Call this function to matrix a matrix immutable.
     467        Call this function to set the matrix as immutable.
    468468       
    469469        Matrices are always mutable by default, i.e., you can change their
    470470        entries using ``A[i,j] = x``. However, mutable matrices