Ticket #14772: trac_14772-review-dg.patch

File trac_14772-review-dg.patch, 13.5 KB (added by darij, 7 years ago)

review patch. finished!

  • sage/combinat/permutation.py

    # HG changeset patch
    # User darij grinberg <darijgrinberg@gmail.com>
    # Date 1374276088 25200
    # Node ID c5d98d7e87201304898f22a0dd1f21f93bd42ae0
    # Parent  e6a0c6c4e3f21f38156166aab745b881267249bb
    trac #14772: review patch (dg)
    
    diff --git a/sage/combinat/permutation.py b/sage/combinat/permutation.py
    a b class Permutations(Parent, UniqueReprese 
    34743474    specify ``n`` or ``k`` along with a keyword.
    34753475   
    34763476    ``Permutations(descents=(list,n))`` returns the class of permutations of
    3477     `n` with descents in the positions specified by ``list``. If `n` is not
    3478     specified (i. e., with syntax ``Permutations(descents=list)``), it
    3479     defaults to the (arguably not very useful) value ``max(list) + 1``.
     3477    `n` with descents in the positions specified by ``list``. This uses the
     3478    slightly nonstandard convention that the images of `1,2,...,n` under the
     3479    permutation are regarded as positions `0,1,...,n-1`, so for example the
     3480    presence of `1` in ``list`` signifies that the permutations `\pi` should
     3481    satisfy `\pi(2) > \pi(3)`.
    34803482    Note that ``list`` is supposed to be a list of positions of the descents,
    34813483    not the descents composition.
     3484    The alternative syntax ``Permutations(descents=list)`` is deprecated. It
     3485    used to boil down ``Permutations(descents=(list, max(list) + 2)``)
     3486    (unless the list ``list`` is empty). It does *not* return the class of
     3487    permutations with descents composition ``list``.
    34823488   
    34833489    ``Permutations(bruhat_smaller=p)`` and ``Permutations(bruhat_greater=p)``
    34843490    return the class of permutations smaller or greater, respectively, than
    class Permutations(Parent, UniqueReprese 
    37023708                return StandardPermutations_descents(tuple(args[0]), args[1])
    37033709            else:
    37043710                #Size not specified
    3705                 return StandardPermutations_descents(tuple(kwargs['descents']), max(kwargs['descents'])+1)
     3711                return StandardPermutations_descents(kwargs['descents'])
    37063712        elif 'bruhat_smaller' in kwargs:
    37073713            return StandardPermutations_bruhat_smaller(Permutation(kwargs['bruhat_smaller']))
    37083714        elif 'bruhat_greater' in kwargs:
    class Permutations_mset(Permutations): 
    38613867        self.mset = mset
    38623868        Permutations.__init__(self, category=FiniteEnumeratedSets())
    38633869
     3870    def __contains__(self, x):
     3871        """
     3872        EXAMPLES::
     3873       
     3874            sage: p = Permutations([1,2,2])
     3875            sage: [1,2,2] in p
     3876            True
     3877            sage: [] in p
     3878            False
     3879            sage: [2,2] in p
     3880            False
     3881            sage: [1,1] in p
     3882            False
     3883            sage: [2,1] in p
     3884            False
     3885            sage: [2,1,2] in p
     3886            True
     3887        """
     3888        s = list(self.mset)
     3889        if len(x) != len(s):
     3890            return False
     3891        for i in x:
     3892            if i in s:
     3893                s.remove(i)
     3894            else:
     3895                return False
     3896        return True
     3897
    38643898    class Element(ClonableArray):
    38653899        """
    38663900        A permutation of an arbitrary multiset.
    class Permutations_mset(Permutations): 
    38763910                sage: elt = S(['c','c','a'])
    38773911                sage: elt.check()
    38783912            """
    3879             mset = self.parent().mset
    3880             for val in self:
    3881                 if val not in mset:
    3882                     raise ValueError("Invalid permutation")
     3913            if self not in self.parent():
     3914                raise ValueError("Invalid permutation")
    38833915
    38843916    def _repr_(self):
    38853917        """
    class Permutations_mset(Permutations): 
    39633995        c = factorial(len(lmset))
    39643996        for i in d:
    39653997            if d[i] != 1:
    3966                 c /= factorial(d[i])
     3998                c //= factorial(d[i])
    39673999        return ZZ(c)
    39684000
    39694001class Permutations_set(Permutations):
    39704002    """
    3971     Permutations of an arbitrary given set.
     4003    Permutations of an arbitrary given finite set.
     4004   
     4005    Here, a "permutation of a finite set `S`" means a list of the
     4006    elements of `S` in which every element of `S` occurs exactly
     4007    once. This is not to be confused with bijections from `S` to
     4008    `S`, which are also often called permutations in literature.
    39724009    """
    39734010    @staticmethod
    39744011    def __classcall_private__(cls, s):
    class Permutations_set(Permutations): 
    39944031        Permutations.__init__(self, category=FiniteEnumeratedSets())
    39954032        self._set = s
    39964033
     4034    def __contains__(self, x):
     4035        """
     4036        EXAMPLES::
     4037       
     4038            sage: p = Permutations([4,-1,'cthulhu'])
     4039            sage: [4,-1,'cthulhu'] in p
     4040            True
     4041            sage: [] in p
     4042            False
     4043            sage: [4,'cthulhu','fhtagn'] in p
     4044            False
     4045            sage: [4,'cthulhu',4,-1] in p
     4046            False
     4047            sage: [-1,'cthulhu',4] in p
     4048            True
     4049        """
     4050        s = list(self._set)
     4051        if len(x) != len(s):
     4052            return False
     4053        for i in x:
     4054            if i in s:
     4055                s.remove(i)   # not strictly necessary since no element appears in s twice
     4056            else:
     4057                return False
     4058        return True
     4059
    39974060    def _repr_(self):
    39984061        """
    39994062        TESTS::
    class Permutations_set(Permutations): 
    40184081                sage: elt = S(['t','c','a'])
    40194082                sage: elt.check()
    40204083            """
    4021             set = self.parent()._set
    4022             for val in self:
    4023                 if val not in set:
    4024                     raise ValueError("Invalid permutation")
     4084            if self not in self.parent():
     4085                raise ValueError("Invalid permutation")
    40254086   
    40264087    def __iter__(self):
    40274088        r"""
    class Permutations_msetk(Permutations_ms 
    41834244
    41844245class Permutations_setk(Permutations_set):
    41854246    """
    4186     Permutations of length `k` of an arbitrary set.
     4247    Permutations of length `k` of an arbitrary given finite set.
     4248
     4249    Here, a "permutation of length `k` of a finite set `S`" means
     4250    a list of length `k` whose entries are pairwise distinct and
     4251    all belong to `S`.
    41874252    """
    41884253    @staticmethod
    41894254    def __classcall_private__(cls, s, k):
    class StandardPermutations_n(Permutation 
    45134578        from sage.combinat.partition import Partition
    45144579        nu = Partition(nu)
    45154580        if nu.size() > self.n:
    4516             raise ValueError("The size of the partition (=%s) should be lower"
    4517                              " than the size of the permutations(=%s)"%(nu.size,self.n))
    4518         l=[]
    4519         i=0
     4581            raise ValueError("The size of the partition (=%s) should be lower or equal"
     4582                             " to the size of the permutations (=%s)"%(nu.size,self.n))
     4583        l = []
     4584        i = 0
    45204585        for nui in nu:
    45214586            for j in range(nui-1):
    45224587                l.append(i+j+2)
    45234588            l.append(i+1)
    4524             i+=nui
    4525         for i in range(nu.size(),self.n):
     4589            i += nui
     4590        for i in range(nu.size(), self.n):
    45264591            l.append(i+1)
    45274592        return self.element_class(self, l)
    45284593 
    def from_inversion_vector(iv): 
    46744739
    46754740def from_cycles(n, cycles):
    46764741    r"""
    4677     Return the permutation corresponding to cycles.
     4742    Return the permutation in the `n`-th symmetric group whose decomposition
     4743    into disjoint cycles is ``cycles``.
    46784744
    46794745    This function checks that its input is correct (i.e. that the cycles are
    46804746    disjoint and their elements integers among `1...n`). It raises an exception
    def from_cycles(n, cycles): 
    46894755        sage: import sage.combinat.permutation as permutation
    46904756        sage: permutation.from_cycles(4, [[1,2]])
    46914757        [2, 1, 3, 4]
     4758        sage: permutation.from_cycles(4, [[1,2,4]])
     4759        [2, 4, 3, 1]
     4760        sage: permutation.from_cycles(10, [[3,1],[4,5],[6,8,9]])
     4761        [3, 2, 1, 5, 4, 8, 7, 9, 6, 10]
     4762        sage: permutation.from_cycles(10, ((2, 5), (6, 1, 3)))
     4763        [3, 5, 6, 4, 2, 1, 7, 8, 9, 10]
    46924764        sage: permutation.from_cycles(4, [])
    46934765        [1, 2, 3, 4]
    46944766        sage: permutation.from_cycles(4, [[]])
    robinson_schensted_inverse = deprecated_ 
    47984870def bistochastic_as_sum_of_permutations(M, check = True):
    47994871    r"""
    48004872    Return the positive sum of permutations corresponding to
    4801     the bistochastic matrix.
     4873    the bistochastic matrix ``M``.
    48024874
    48034875    A stochastic matrix is a matrix with nonnegative real entries such that the
    48044876    sum of the elements of any row is equal to 1. A bistochastic matrix is a
    def bistochastic_as_sum_of_permutations( 
    49244996       
    49254997class StandardPermutations_descents(StandardPermutations_n):
    49264998    """
    4927     Permutations of `\{1, \ldots, n\}` with a fixed set of decents.
     4999    Permutations of `\{1, \ldots, n\}` with a fixed set of descents.
    49285000    """
    49295001    @staticmethod
    49305002    def __classcall_private__(cls, d, n=None):
    class StandardPermutations_descents(Stan 
    49375009            sage: P2 = Permutations(descents=((1,0,4,8),12))
    49385010            sage: P1 is P2
    49395011            True
     5012            sage: from sage.combinat.permutation import *
     5013            sage: Permutations(descents=[1,0,4,8])
     5014            doctest:...: DeprecationWarning: Permutations(descents=list) has been deprecated in favor of Permutations(descents=(list, n)) for a suitably chosen n (this function sets n = max(list) + 2, but this might not be what you want
     5015            See http://trac.sagemath.org/14772 for details.
     5016            Standard permutations of 10 with descents [1, 0, 4, 8]
    49405017        """
    49415018        if n is None:
    4942             n = max(d) + 1
     5019            # This if-loop allows calling Permutations(descents=list)
     5020            # rather than Permutations(descents=(list, n)). In this
     5021            # case, the n is set to the first integer for which
     5022            # Permutations(descents=(list, n)) would be
     5023            # well-defined. (Note that this only allows constructing
     5024            # classes of permutations where the last possible
     5025            # position for a descent is a descent!)
     5026            # The syntax is deprecated since (and was broken before)
     5027            # trac #14772.
     5028            from sage.misc.superseded import deprecation
     5029            deprecation(14772, 'Permutations(descents=list) has been '
     5030                               + 'deprecated in favor of '
     5031                               + 'Permutations(descents=(list, n)) for '
     5032                               + 'a suitably chosen n (this function '
     5033                               + 'sets n = max(list) + 2, but this might '
     5034                               + 'not be what you want')
     5035            if len(d) == 0:
     5036                n = 0
     5037            n = max(d) + 2
    49435038        return super(StandardPermutations_descents, cls).__classcall__(cls, tuple(d), n)
    49445039
    49455040    def __init__(self, d, n):
    49465041        """
    49475042        The class of all permutations of `\{1, 2, ..., n\}`
    4948         with set of descents `d`.
     5043        with set of descent positions `d` (where the descent positions
     5044        are being counted from `0`, so that `i` lies in this set if
     5045        and only if the permutation takes a larger value at `i + 1` than
     5046        at `i + 2`).
    49495047
    49505048        TESTS::
    49515049       
    def descents_composition_list(dc): 
    50565154def descents_composition_first(dc):
    50575155    r"""
    50585156    Compute the smallest element of a descent class having a descent
    5059     decomposition ``dc``.
     5157    composition ``dc``.
    50605158   
    50615159    EXAMPLES::
    50625160   
    def descents_composition_first(dc): 
    50825180def descents_composition_last(dc):
    50835181    r"""
    50845182    Return the largest element of a descent class having a descent
    5085     decomposition ``dc``.
     5183    composition ``dc``.
    50865184   
    50875185    EXAMPLES::
    50885186   
    def from_major_code(mc, final_descent=Fa 
    53745472class StandardPermutations_bruhat_smaller(Permutations):
    53755473    r"""
    53765474    Permutations of `\{1, \ldots, n\}` that are less than or equal to a
    5377     permutation `p` in Bruhat order.
     5475    permutation `p` in the Bruhat order.
    53785476    """
    53795477    @staticmethod
    53805478    def __classcall_private__(cls, p):
    class StandardPermutations_bruhat_smalle 
    54325530class StandardPermutations_bruhat_greater(Permutations):
    54335531    r"""
    54345532    Permutations of `\{1, \ldots, n\}` that are greater than or equal to a
    5435     permutation `p`.
     5533    permutation `p` in the Bruhat order.
    54365534    """
    54375535    @staticmethod
    54385536    def __classcall_private__(cls, p):
    def bruhat_lequal(p1, p2): 
    55235621
    55245622def permutohedron_lequal(p1, p2, side="right"):
    55255623    r"""
    5526     Return ``True`` if ``p1`` is less than ``p2`` in the permutohedron order.
     5624    Return ``True`` if ``p1`` is less than or equal to ``p2`` in the
     5625    permutohedron order.
    55275626   
    55285627    By default, the computations are done in the right permutohedron.
    55295628    If you pass the option ``side='left'``, then they will be done in the
    class StandardPermutations_avoiding_321( 
    61326231        """
    61336232        TESTS::
    61346233
    6135             sage: P = Permutations(3, avoiding=[2, 1, 3])
     6234            sage: P = Permutations(3, avoiding=[3, 2, 1])
    61366235            sage: TestSuite(P).run()
    61376236        """
    6138         StandardPermutations_avoiding_generic.__init__(self, n, Permutation([2, 1, 3]))
     6237        StandardPermutations_avoiding_generic.__init__(self, n, Permutation([3, 2, 1]))
    61396238
    61406239    def cardinality(self):
    61416240        """
    class StandardPermutations_avoiding_231( 
    61636262        """
    61646263        TESTS::
    61656264
    6166             sage: P = Permutations(3, avoiding=[2, 1, 3])
     6265            sage: P = Permutations(3, avoiding=[2, 3, 1])
    61676266            sage: TestSuite(P).run()
    61686267        """
    6169         StandardPermutations_avoiding_generic.__init__(self, n, Permutation([2, 1, 3]))
     6268        StandardPermutations_avoiding_generic.__init__(self, n, Permutation([2, 3, 1]))
    61706269
    61716270    def cardinality(self):
    61726271        """