# HG changeset patch
# User Lukas Lansky <lansky@kam.mff.cuni.cz>
# Date 1327348262 0
# Node ID 7d0f7e4d1219f7c9c95c7858cbd601cd99643376
# Parent 7e8784ab604a363654f47cfeaf0c3f5a1725d2c2
Trac 9714: better incidence matrix checking
diff --git a/sage/graphs/graph.py b/sage/graphs/graph.py
a
|
b
|
|
698 | 698 | sage: Graph(M,sparse=True) |
699 | 699 | Graph on 3 vertices |
700 | 700 | |
701 | | sage: M = Matrix([[0,1,1],[1,0,0],[0,0,0]]); M |
702 | | [0 1 1] |
703 | | [1 0 0] |
704 | | [0 0 0] |
705 | | sage: Graph(M) |
706 | | Traceback (most recent call last): |
707 | | ... |
708 | | ValueError: Non-symmetric or non-square matrix assumed to be an incidence matrix: There must be two nonzero entries (-1 & 1) per column. |
709 | | |
710 | 701 | sage: M = Matrix([[0,1,1],[1,0,1],[-1,-1,0]]); M |
711 | 702 | [ 0 1 1] |
712 | 703 | [ 1 0 1] |
… |
… |
|
716 | 707 | ... |
717 | 708 | ValueError: Non-symmetric or non-square matrix assumed to be an incidence matrix: Each column represents an edge: -1 goes to 1. |
718 | 709 | |
| 710 | :: |
| 711 | |
| 712 | sage: MA = Matrix([[1,2,0], [0,2,0], [0,0,1]]) # trac 9714 |
| 713 | sage: MI = Graph(MA, format='adjacency_matrix').incidence_matrix(); MI |
| 714 | [-1 -1 0 0 0 1] |
| 715 | [ 1 1 0 1 1 0] |
| 716 | [ 0 0 1 0 0 0] |
| 717 | sage: Graph(MI).edges(labels=None) |
| 718 | [(0, 0), (0, 1), (0, 1), (1, 1), (1, 1), (2, 2)] |
| 719 | |
| 720 | sage: M = Matrix([[1], [-1]]); M |
| 721 | [ 1] |
| 722 | [-1] |
| 723 | sage: Graph(M).edges() |
| 724 | [(0, 1, None)] |
| 725 | |
719 | 726 | #. a list of edges, or labelled edges:: |
720 | 727 | |
721 | 728 | sage: g = Graph([(1,3),(3,8),(5,2)]) |
… |
… |
|
1050 | 1057 | positions = [] |
1051 | 1058 | for c in data.columns(): |
1052 | 1059 | NZ = c.nonzero_positions() |
1053 | | positions.append(tuple(NZ)) |
1054 | | if len(NZ) != 2: |
| 1060 | if len(NZ) == 1: |
| 1061 | if loops is None: |
| 1062 | loops = True |
| 1063 | elif not loops: |
| 1064 | msg += "There must be two nonzero entries (-1 & 1) per column." |
| 1065 | assert False |
| 1066 | positions.append((NZ[0], NZ[0])) |
| 1067 | elif len(NZ) != 2: |
1055 | 1068 | msg += "There must be two nonzero entries (-1 & 1) per column." |
1056 | 1069 | assert False |
| 1070 | else: |
| 1071 | positions.append(tuple(NZ)) |
1057 | 1072 | L = uniq(c.list()) |
1058 | 1073 | L.sort() |
1059 | | if L != [-1,0,1]: |
| 1074 | desirable = [-1, 0, 1] if len(NZ) == 2 else [0, 1] |
| 1075 | if data.nrows() == len(desirable) - 1: |
| 1076 | desirable = filter(lambda i: i != 0, desirable) |
| 1077 | if L != desirable: |
1060 | 1078 | msg += "Each column represents an edge: -1 goes to 1." |
1061 | 1079 | assert False |
1062 | 1080 | if loops is None: loops = False |