Ticket #15269: trac_15269-tableau-speedup-dg.patch

File trac_15269-tableau-speedup-dg.patch, 3.6 KB (added by darij, 8 years ago)
  • sage/combinat/tableau.py

    # HG changeset patch
    # User darij grinberg <darijgrinberg@gmail.com>
    # Date 1381435876 25200
    # Node ID 139c1dd4f919699bfe42ad9f0144952623889eff
    # Parent  3b0ecd26b41b8797422fd340f8df6350f305d7ea
    trac #15269: speeding up two tableau methods
    
    diff --git a/sage/combinat/tableau.py b/sage/combinat/tableau.py
    a b class Tableau(CombinatorialObject, Eleme 
    11961196        return self[i][j]
    11971197
    11981198    def weight(self):
    1199         """
    1200         Returns the weight of the word corresponding to the tableau ``self``.
     1199        r"""
     1200        Return the weight of the tableau ``self``. Trailing zeroes are
     1201        omitted when returning the weight.
     1202
     1203        The weight of a tableau `T` is the sequence `(a_1, a_2, a_3, \ldots )`,
     1204        where `a_k` is the number of entries of `T` equal to `k`. This
     1205        sequence contains only finitely many nonzero entries.
     1206
     1207        The weight of a tableau `T` is the same as the weight of the
     1208        reading word of `T`, for any reading order.
    12011209
    12021210        EXAMPLES::
    12031211
    12041212            sage: Tableau([[1,2],[3,4]]).weight()
    12051213            [1, 1, 1, 1]
    1206         """
    1207         ed = self.to_word().evaluation_dict()
    1208         entries = ed.keys()
    1209         m = max(entries) + 1 if entries else -1
    1210         return [ed.get(k,0) for k in range(1,m)]
     1214
     1215            sage: Tableau([]).weight()
     1216            []
     1217
     1218            sage: Tableau([[1,3,3,7],[4,2],[2,3]]).weight()
     1219            [1, 2, 3, 1, 0, 0, 1]
     1220        """
     1221        if len(self) == 0:
     1222            return []
     1223        m = max([max(row) for row in self])
     1224        res = [0] * m
     1225        for row in self:
     1226            for i in row:
     1227                res[i - 1] += 1
     1228        return res
     1229        # Old alternative implementation:
     1230        # ed = self.to_word().evaluation_dict()
     1231        # entries = ed.keys()
     1232        # m = max(entries) + 1 if entries else -1
     1233        # return [ed.get(k,0) for k in range(1,m)]
    12111234
    12121235    evaluation = weight
    12131236
    class Tableau(CombinatorialObject, Eleme 
    13621385
    13631386    def cells_containing(self, i):
    13641387        r"""
    1365         Return the list of cells in which the letter `i` appears in the tableau
    1366         ``self``. The list is ordered with cells appearing from left to right.
     1388        Return the list of cells in which the letter `i` appears in the
     1389        tableau ``self``. The list is ordered with cells appearing from
     1390        left to right.
     1391
     1392        Cells are given as pairs of coordinates `(a, b)`, where both
     1393        rows and columns are counted from `0` (so `a = 0` means the cell
     1394        lies in the leftmost column of the tableau, etc.).
    13671395
    13681396        EXAMPLES::
    13691397
    class Tableau(CombinatorialObject, Eleme 
    13781406            sage: t = Tableau([[1,1,2,4],[2,4,4],[4]])
    13791407            sage: t.cells_containing(4)
    13801408            [(2, 0), (1, 1), (1, 2), (0, 3)]
    1381         """
    1382         list = []
    1383         for r in range(len(self)):
    1384             for c in range(self.shape()[r]-1,-1,-1):
    1385                 if self[r][c] == i:
    1386                     list += [(r,c)]
    1387         list.reverse()
    1388         return list
     1409
     1410            sage: t = Tableau([[1,1,2,8,9],[2,5,6,11],[3,7,7,13],[4,8,9],[5],[13],[14]])
     1411            sage: t.cells_containing(8)
     1412            [(3, 1), (0, 3)]
     1413
     1414            sage: Tableau([]).cells_containing(3)
     1415            []
     1416        """
     1417        cell_list = []
     1418        for r in range(len(self)-1, -1, -1):
     1419            rth_row = self[r]
     1420            for c in range(len(rth_row)):
     1421                if rth_row[c] == i:
     1422                    cell_list.append((r,c))
     1423        return cell_list
    13891424
    13901425    def k_weight(self, k):
    13911426        """