Ticket #12476: trac_12476-lattice_join_matrix_speedup-fh.2.patch

File trac_12476-lattice_join_matrix_speedup-fh.2.patch, 5.0 KB (added by hivert, 9 years ago)
  • sage/combinat/posets/hasse_diagram.py

    # HG changeset patch
    # User Florent Hivert <Florent.Hivert@univ-rouen.fr>
    # Date 1328835481 -3600
    # Node ID a91335f562c8ccae64564452a3d572bc978a1a5f
    # Parent  793d8edfc2536ca874db8fee549f4850e3daf82b
    #12476: Little fixes for a major speedup of join/meet matrices for FiniteLatticePoset
    
    diff --git a/sage/combinat/posets/hasse_diagram.py b/sage/combinat/posets/hasse_diagram.py
    a b class HasseDiagram(DiGraph): 
    975975        Returns the matrix of meets of self. The ``(x,y)``-entry of
    976976        this matrix is the meet of ``x`` and ``y`` in ``self``.
    977977
    978         This algorithm is modelled after the algorithm of
    979         Freese-Jezek-Nation (p217).
     978        This algorithm is modelled after the algorithm of Freese-Jezek-Nation
     979        (p217). It can also be found on page 140 of [Gec81m]_.
    980980
    981981        .. note::
     982
    982983            Once the matrix has been computed, it is stored in
    983984            self._meet_matrix. Delete this attribute if you want to
    984985            recompute the matrix.
    985            
     986
    986987        EXAMPLES::
    987988
    988989            sage: from sage.combinat.posets.hasse_diagram import HasseDiagram
    class HasseDiagram(DiGraph): 
    997998            [0 0 2 3 2 2 6 6]
    998999            [0 1 2 3 4 5 6 7]
    9991000
     1001        REFERENCE:
     1002
     1003        .. [Gec81m] Fundamentals of Computation Theory
     1004          Gecseg, F.
     1005          Proceedings of the 1981 International Fct-Conference
     1006          Szeged, Hungaria, August 24-28, vol 117
     1007          Springer-Verlag, 1981
     1008
    10001009        TESTS::
    10011010
    10021011            sage: from sage.combinat.posets.hasse_diagram import HasseDiagram
    class HasseDiagram(DiGraph): 
    10211030        if not all([le[0,x]==1 for x in range(n)]):
    10221031            raise ValueError, "Not a meet-semilattice: no bottom element."
    10231032        lc = [[y[0] for y in self.incoming_edges([x])] for x in range(n)]
    1024         S = []
     1033
    10251034        for x in range(n): # x=x_k
    10261035            meet[x][x] = x
    1027             for y in S:
     1036            for y in range(x):
    10281037                T = []
    10291038                for z in lc[x]:
    10301039                    T.append(meet[y][z]) # T = {x_i \wedge z : z>-x_k}
     1040
    10311041                q = T[0]
    1032                 for z in T[1:]:
     1042                for z in T:
    10331043                    if z>q: q = z
    10341044                for z in T:
    10351045                    if not le[z,q]:
    10361046                        raise ValueError, "No meet for x=%s y=%s"%(x,y)
    10371047                meet[x][y] = q
    10381048                meet[y][x] = q
    1039             S.append(x)
     1049
    10401050        self._meet = matrix(ZZ,meet)
    10411051        return self._meet
    10421052
    class HasseDiagram(DiGraph): 
    10721082        Returns the matrix of joins of ``self``. The ``(x,y)``-entry
    10731083        of this matrix is the join of ``x`` and ``y`` in ``self``.
    10741084
    1075         This algorithm is modelled after the algorithm of
    1076         Freese-Jezek-Nation (p217).
     1085        This algorithm is modelled after the algorithm of Freese-Jezek-Nation
     1086        (p217). It can also be found on page 140 of [Gec81j]_.
    10771087
    10781088        .. note::
    10791089            Once the matrix has been computed, it is stored in
    10801090            ``self._join_matrix``. Delete this attribute if you want
    10811091            to recompute the matrix.
    1082            
     1092
    10831093        EXAMPLES::
    10841094
    10851095            sage: from sage.combinat.posets.hasse_diagram import HasseDiagram
    class HasseDiagram(DiGraph): 
    10941104            [6 7 6 6 7 7 6 7]
    10951105            [7 7 7 7 7 7 7 7]
    10961106
     1107        REFERENCE:
     1108
     1109        .. [Gec81j] Fundamentals of Computation Theory
     1110          Gecseg, F.
     1111          Proceedings of the 1981 International Fct-Conference
     1112          Szeged, Hungaria, August 24-28, vol 117
     1113          Springer-Verlag, 1981
     1114
    10971115        TESTS::
    10981116
    10991117            sage: from sage.combinat.posets.hasse_diagram import HasseDiagram
    class HasseDiagram(DiGraph): 
    11141132        join = [[0 for x in range(n)] for x in range(n)]
    11151133        le = copy.copy(self.lequal_matrix())
    11161134        for i in range(n): le[i,i] = 1
    1117         if not all([le[x][n-1]==1 for x in range(n)]):
     1135        if not all([le[x,n-1]==1 for x in range(n)]):
    11181136            raise ValueError, "Not a join-semilattice: no top element."
    11191137        uc = [sorted([n-1-y[1] for y in self.outgoing_edges([x])]) for
    11201138                x in reversed(range(n))]
    1121         S = []
     1139
    11221140        for x in range(n): # x=x_k
    11231141            join[x][x] = x
    1124             for y in S:
     1142
     1143            for y in range(x):
    11251144                T = []
    11261145                for z in uc[x]:
    11271146                    T.append(join[y][z]) # T = {x_i \vee z : z>-x_k}
    11281147                q = T[0]
    1129                 for z in T[1:]:
     1148                for z in T:
    11301149                    if z>q: q = z
    11311150                for z in T:
    1132                     if not le[n-1-q][n-1-z]:
     1151                    if not le[n-1-q,n-1-z]:
    11331152                        raise ValueError, "No join for x=%s y=%s"%(x,y)
    11341153                join[x][y] = q
    11351154                join[y][x] = q
    1136             S.append(x)
     1155
    11371156        self._join = matrix(ZZ,[[n-1-join[n-1-x][n-1-y] for y in range(n)] for x in range(n)])
    11381157        return self._join
    11391158