Ticket #14138: trac_14138.patch

File trac_14138.patch, 18.7 KB (added by ncohen, 8 years ago)
  • sage/combinat/combinat.py

    # HG changeset patch
    # User Nathann Cohen <nathann.cohen@gmail.com>
    # Date 1360967257 -3600
    # Node ID ec73e10e904c942c9231520a23445cf9dc3b4f33
    # Parent  efe3c1085e05c5ae339d6e1fb9dcf58a4b52aea3
    some cleanup in sage.combinat
    
    diff --git a/sage/combinat/combinat.py b/sage/combinat/combinat.py
    a b  
    11r"""
    22Combinatorial Functions
    33
    4 AUTHORS:
    5 
    6 - David Joyner (2006-07): initial implementation.
    7 
    8 - William Stein (2006-07): editing of docs and code; many
    9   optimizations, refinements, and bug fixes in corner cases
    10 
    11 - David Joyner (2006-09): bug fix for combinations, added
    12   permutations_iterator, combinations_iterator from Python Cookbook,
    13   edited docs.
    14 
    15 - David Joyner (2007-11): changed permutations, added hadamard_matrix
    16 
    17 - Florent Hivert (2009-02): combinatorial class cleanup
    18 
    19 - Fredrik Johansson (2010-07): fast implementation of ``stirling_number2``
    20 
    21 - Punarbasu Purkayastha (2012-12): deprecate arrangements, combinations,
    22   combinations_iterator, and clean up very old deprecated methods.
    23 
    244This module implements some combinatorial functions, as listed
    255below. For a more detailed description, see the relevant
    266docstrings.
    277
    28 Sequences:
    29 
    30 
    31 -  Bell numbers, ``bell_number``
    32 
    33 -  Bernoulli numbers, ``bernoulli_number`` (though
    34    PARI's bernoulli is better)
    35 
    36 -  Catalan numbers, ``catalan_number`` (not to be
     8**Sequences:**
     9
     10
     11-  Bell numbers, :func:`bell_number`
     12
     13-  Catalan numbers, :func:`catalan_number` (not to be
    3714   confused with the Catalan constant)
    3815
    39 -  Eulerian/Euler numbers, ``euler_number`` (Maxima)
    40 
    41 -  Fibonacci numbers, ``fibonacci`` (PARI) and
    42    ``fibonacci_number`` (GAP) The PARI version is
     16-  Eulerian/Euler numbers, :func:`euler_number` (Maxima)
     17
     18-  Fibonacci numbers, :func:`fibonacci` (PARI) and
     19   :func:`fibonacci_number` (GAP) The PARI version is
    4320   better.
    4421
    45 -  Lucas numbers, ``lucas_number1``,
    46    ``lucas_number2``.
    47 
    48 -  Stirling numbers, ``stirling_number1``,
    49    ``stirling_number2``.
    50 
    51 
    52 Set-theoretic constructions:
    53 
    54 
    55 -  Combinations of a multiset, ``combinations``,
    56    ``combinations_iterator``, and
    57    ``number_of_combinations``. These are unordered
    58    selections without repetition of k objects from a multiset S.
    59 
    60 -  Arrangements of a multiset, ``arrangements`` and
    61    ``number_of_arrangements`` These are ordered
    62    selections without repetition of k objects from a multiset S.
    63 
    64 -  Derangements of a multiset, ``derangements`` and
    65    ``number_of_derangements``.
    66 
    67 -  Tuples of a multiset, ``tuples`` and
    68    ``number_of_tuples``. An ordered tuple of length k of
     22-  Lucas numbers, :func:`lucas_number1`,
     23   :func:`lucas_number2`.
     24
     25-  Stirling numbers, :func:`stirling_number1`,
     26   :func:`stirling_number2`.
     27
     28**Set-theoretic constructions:**
     29
     30-  Derangements of a multiset, :func:`derangements` and
     31   :func:`number_of_derangements`.
     32
     33-  Tuples of a multiset, :func:`tuples` and
     34   :func:`number_of_tuples`. An ordered tuple of length k of
    6935   set S is a ordered selection with repetitions of S and is
    7036   represented by a sorted list of length k containing elements from
    7137   S.
    7238
    73 -  Unordered tuples of a set, ``unordered_tuple`` and
    74    ``number_of_unordered_tuples``. An unordered tuple
     39-  Unordered tuples of a set, :func:`unordered_tuples` and
     40   :func:`number_of_unordered_tuples`. An unordered tuple
    7541   of length k of set S is an unordered selection with repetitions of S
    7642   and is represented by a sorted list of length k containing elements
    7743   from S.
    7844
    79 -  Permutations of a multiset, ``permutations``,
    80    ``permutations_iterator``,
    81    ``number_of_permutations``. A permutation is a list
    82    that contains exactly the same elements but possibly in different
    83    order.
    84 
    85 
    86 Partitions:
    87 
    88 
    89 -  Partitions of a set, ``partitions_set``,
    90    ``number_of_partitions_set``. An unordered partition
     45.. WARNING::
     46
     47   The following functions are deprecated and will soon be removed.
     48
     49    - Combinations of a multiset, :func:`combinations`,
     50      :func:`combinations_iterator`, and :func:`number_of_combinations`. These
     51      are unordered selections without repetition of k objects from a multiset
     52      S.
     53
     54    - Arrangements of a multiset, :func:`arrangements` and
     55      :func:`number_of_arrangements` These are ordered selections without
     56      repetition of k objects from a multiset S.
     57
     58    - Permutations of a multiset, :func:`permutations`,
     59      :func:`permutations_iterator`, :func:`number_of_permutations`. A
     60      permutation is a list that contains exactly the same elements but possibly
     61      in different order.
     62
     63**Partitions:**
     64
     65-  Partitions of a set, :func:`partitions_set`,
     66   :func:`number_of_partitions_set`. An unordered partition
    9167   of set S is a set of pairwise disjoint nonempty sets with union S
    9268   and is represented by a sorted list of such sets.
    9369
    94 -  Partitions of an integer, ``Partitions``.
     70-  Partitions of an integer, :func:`Partitions`.
    9571   An unordered partition of n is an unordered sum
    9672   `n = p_1+p_2 +\ldots+ p_k` of positive integers and is
    9773   represented by the list `p = [p_1,p_2,\ldots,p_k]`, in
    9874   nonincreasing order, i.e., `p1\geq p_2 ...\geq p_k`.
    9975
    10076-  Ordered partitions of an integer,
    101    ``ordered_partitions``,
    102    ``number_of_ordered_partitions``. An ordered
     77   :func:`ordered_partitions`,
     78   :func:`number_of_ordered_partitions`. An ordered
    10379   partition of n is an ordered sum
    10480   `n = p_1+p_2 +\ldots+ p_k` of positive integers and is
    10581   represented by the list `p = [p_1,p_2,\ldots,p_k]`, in
    10682   nonincreasing order, i.e., `p1\geq p_2 ...\geq p_k`.
    10783
    108 ``partitions_greatest`` implements a special type
     84:func:`partitions_greatest` implements a special type
    10985   of restricted partition.
    11086
    111 ``partitions_greatest_eq`` is another type of
     87:func:`partitions_greatest_eq` is another type of
    11288   restricted partition.
    11389
    114 -  Tuples of partitions, ``PartitionTuples``. A `k`-tuple
    115    of partitions is represented by a list of all `k`-tuples of
    116    partitions which together form a partition of `n`.
    117 
    118 -  Powers of a partition, ``partition_power(pi, k)``.
    119    The power of a partition corresponds to the `k`-th power of
    120    a permutation with cycle structure `\pi`.
    121 
    122 
    123 Related functions:
    124 
    125 
    126 -  Bernoulli polynomials, ``bernoulli_polynomial``
    127 
    128 
    129 Implemented in other modules (listed for completeness):
     90-  Tuples of partitions, :class:`PartitionTuples`. A `k`-tuple of partitions is
     91   represented by a list of all `k`-tuples of partitions which together form a
     92   partition of `n`.
     93
     94**Related functions:**
     95
     96-  Bernoulli polynomials, :func:`bernoulli_polynomial`
     97
     98**Implemented in other modules (listed for completeness):**
    13099
    131100The ``sage.rings.arith`` module contains the following
    132101combinatorial functions:
    133102
    134 
    135103-  binomial the binomial coefficient (wrapped from PARI)
    136104
    137105-  factorial (wrapped from PARI)
     
    139107-  partition (from the Python Cookbook) Generator of the list of
    140108   all the partitions of the integer `n`.
    141109
    142 ``number_of_partitions`` (wrapped from PARI) the
     110:func:`number_of_partitions` (wrapped from PARI) the
    143111   *number* of partitions:
    144112
    145 ``falling_factorial`` Definition: for integer
     113:func:`falling_factorial` Definition: for integer
    146114   `a \ge 0` we have `x(x-1) \cdots (x-a+1)`. In all
    147115   other cases we use the GAMMA-function:
    148116   `\frac {\Gamma(x+1)} {\Gamma(x-a+1)}`.
    149117
    150 ``rising_factorial`` Definition: for integer
     118:func:`rising_factorial` Definition: for integer
    151119   `a \ge 0` we have `x(x+1) \cdots (x+a-1)`. In all
    152120   other cases we use the GAMMA-function:
    153121   `\frac {\Gamma(x+a)} {\Gamma(x)}`.
     
    158126
    159127             \binom{n}{k}_q = \frac{(1-q^m)(1-q^{m-1})\cdots (1-q^{m-r+1})}                              {(1-q)(1-q^2)\cdots (1-q^r)}.
    160128
    161 
    162 
    163 
    164129The ``sage.groups.perm_gps.permgroup_elements``
    165130contains the following combinatorial functions:
    166131
     
    182147REFERENCES:
    183148
    184149- http://en.wikipedia.org/wiki/Twelvefold_way (general reference)
     150
     151AUTHORS:
     152
     153- David Joyner (2006-07): initial implementation.
     154
     155- William Stein (2006-07): editing of docs and code; many
     156  optimizations, refinements, and bug fixes in corner cases
     157
     158- David Joyner (2006-09): bug fix for combinations, added
     159  permutations_iterator, combinations_iterator from Python Cookbook,
     160  edited docs.
     161
     162- David Joyner (2007-11): changed permutations, added hadamard_matrix
     163
     164- Florent Hivert (2009-02): combinatorial class cleanup
     165
     166- Fredrik Johansson (2010-07): fast implementation of ``stirling_number2``
     167
     168- Punarbasu Purkayastha (2012-12): deprecate arrangements, combinations,
     169  combinations_iterator, and clean up very old deprecated methods.
     170
     171Functions and classes
     172---------------------
    185173"""
    186174
    187175#*****************************************************************************
     
    242230    ans=gap.eval("Bell(%s)"%ZZ(n))
    243231    return ZZ(ans)
    244232
    245 ## def bernoulli_number(n):
    246 ##     r"""
    247 ##     Returns the n-th Bernoulli number $B_n$; $B_n/n!$ is the
    248 ##     coefficient of $x^n$ in the power series of $x/(e^x-1)$.
    249 ##     Wraps GAP's Bernoulli.
    250 ##     EXAMPLES:
    251 ##         sage: bernoulli_number(50)
    252 ##         495057205241079648212477525/66
    253 ##     """
    254 ##     ans=gap.eval("Bernoulli(%s)"%n)
    255 ##     return QQ(ans)   ## use QQ, not eval, here
    256 
    257233def catalan_number(n):
    258234    r"""
    259235    Returns the n-th Catalan number
     
    20342010    Returns the size of combinations(mset,k). IMPLEMENTATION: Wraps
    20352011    GAP's NrCombinations.
    20362012
    2037     .. note::
    2038 
    2039        ``mset`` must be a list of integers or strings (i.e., this is
    2040        very restricted).
    2041 
    20422013    EXAMPLES::
    20432014
    20442015        sage: mset = [1,1,2,3,4,4,5]
    20452016        sage: number_of_combinations(mset,2)
     2017        doctest:1: DeprecationWarning: Use Combinations(mset,k).cardinality() instead.
     2018        See http://trac.sagemath.org/14138 for details.
    20462019        12
    20472020    """
    2048     return ZZ(gap.eval("NrCombinations(%s,%s)"%(mset,ZZ(k))))
     2021    from sage.combinat.combination import Combinations
     2022    deprecation(14138, 'Use Combinations(mset,k).cardinality() instead.')
     2023    return Combinations(mset,k).cardinality()
    20492024
    20502025def arrangements(mset,k):
    20512026    r"""
     
    21122087
    21132088def number_of_arrangements(mset,k):
    21142089    """
    2115     Returns the size of arrangements(mset,k). Wraps GAP's
    2116     NrArrangements.
     2090    Returns the size of arrangements(mset,k).
    21172091
    21182092    EXAMPLES::
    21192093
    21202094        sage: mset = [1,1,2,3,4,4,5]
    21212095        sage: number_of_arrangements(mset,2)
     2096        doctest:1: DeprecationWarning: Use Arrangements(mset,k).cardinality() instead.
     2097        See http://trac.sagemath.org/14138 for details.
    21222098        22
    21232099    """
    2124     return ZZ(gap.eval("NrArrangements(%s,%s)"%(mset,ZZ(k))))
     2100    from sage.combinat.permutation import Arrangements
     2101    deprecation(14138, 'Use Arrangements(mset,k).cardinality() instead.')
     2102    return Arrangements(mset, k).cardinality()
    21252103
    21262104def derangements(mset):
    21272105    """
     
    22182196            y.append(s)
    22192197            ans.append(y)
    22202198    return ans
    2221     ## code wrapping GAP's Tuples:
    2222     #ans=gap.eval("Tuples(%s,%s)"%(S,k))
    2223     #return eval(ans)
    2224 
    22252199
    22262200def number_of_tuples(S,k):
    22272201    """
     
    23492323        sage: permutations(mset)
    23502324        [[1, 2, 2], [2, 1, 2], [2, 2, 1]]
    23512325        sage: for p in permutations_iterator(mset): print p
    2352         [1, 2, 2]
     2326        doctest:1: DeprecationWarning: Use the Permutations object instead.
     2327        See http://trac.sagemath.org/14138 for details.
    23532328        [1, 2, 2]
    23542329        [2, 1, 2]
    23552330        [2, 2, 1]
     
    23622337        sage: [x for x in X]
    23632338        [[0, 1], [0, 2], [1, 0], [1, 2], [2, 0], [2, 1]]
    23642339    """
     2340    deprecation(14138, 'Use the Permutations object instead.')
    23652341    items = mset
    23662342    if n is None:
    23672343        n = len(items)
     2344    from sage.combinat.permutation import Permutations
    23682345    for i in range(len(items)):
    23692346        v = items[i:i+1]
    23702347        if n == 1:
    23712348            yield v
    23722349        else:
    23732350            rest = items[:i] + items[i+1:]
    2374             for p in permutations_iterator(rest, n-1):
     2351            for p in Permutations(rest, n-1):
    23752352                yield v + p
    23762353
    23772354def number_of_permutations(mset):
     
    23982375
    23992376        sage: mset = [1,1,2,2,2]
    24002377        sage: number_of_permutations(mset)
     2378        doctest:1: DeprecationWarning: Use the Permutations object instead.
     2379        See http://trac.sagemath.org/14138 for details.
    24012380        10
    24022381    """
    2403     from sage.rings.arith import factorial
    2404     m = len(mset)
    2405     n = []
    2406     seen = []
    2407     for element in mset:
    2408         try:
    2409             n[seen.index(element)] += 1
    2410         except (IndexError, ValueError):
    2411             n.append(1)
    2412             seen.append(element)
    2413     return factorial(m)/prod([factorial(k) for k in n])
     2382    deprecation(14138, 'Use the Permutations object instead.')
     2383    from sage.combinat.permutation import Permutations
     2384    return Permutations(mset).cardinality()
    24142385
    24152386def cyclic_permutations(mset):
    24162387    """
     
    24302401        ...       print cycle
    24312402        ['a', 'b', 'c']
    24322403        ['a', 'c', 'b']
    2433 
    2434     Note that lists with repeats are not handled intuitively::
    2435 
    2436         sage: cyclic_permutations([1,1,1])
    2437         [[1, 1, 1], [1, 1, 1]]
    24382404    """
    24392405    return list(cyclic_permutations_iterator(mset))
    24402406
     
    24572423        ...       print cycle
    24582424        ['a', 'b', 'c']
    24592425        ['a', 'c', 'b']
    2460 
    2461     Note that lists with repeats are not handled intuitively::
    2462 
    2463         sage: cyclic_permutations([1,1,1])
    2464         [[1, 1, 1], [1, 1, 1]]
    24652426    """
    24662427    if len(mset) > 2:
    2467         for perm in permutations_iterator(mset[1:]):
     2428        from sage.combinat.permutation import Permutations
     2429
     2430        for perm in Permutations(mset[1:]):
    24682431            yield [mset[0]] + perm
    24692432    else:
    24702433        yield mset
  • sage/combinat/multichoose_nk.py

    diff --git a/sage/combinat/multichoose_nk.py b/sage/combinat/multichoose_nk.py
    a b  
    22Low-level multichoose
    33"""
    44#*****************************************************************************
    5 #       Copyright (C) 2007 Mike Hansen <mhansen@gmail.com>, 
     5#       Copyright (C) 2007 Mike Hansen <mhansen@gmail.com>,
    66#
    77#  Distributed under the terms of the GNU General Public License (GPL)
    88#
     
    2323    def __init__(self, n, k):
    2424        """
    2525        TESTS::
    26        
     26
    2727            sage: a = MultichooseNK(3,2)
    2828            sage: a == loads(dumps(a))
    2929            True
  • sage/combinat/partition.py

    diff --git a/sage/combinat/partition.py b/sage/combinat/partition.py
    a b  
    581581        gens.append( range(1,self.size()+1) )  # to ensure we get a subgroup of Sym_n
    582582        return PermutationGroup( gens )
    583583
    584 
    585584    def young_subgroup_generators(self):
    586585        """
    587586        Return an indexing set for the generators of the corresponding Young
     
    36793678    ::
    36803679
    36813680        sage: [x for x in Partitions(4, length=3, min_part=0)]
    3682         doctest:... RuntimeWarning: Currently, setting min_part=0 produces Partition objects which violate internal assumptions.  Calling methods on these objects may produce errors or WRONG results!
     3681        doctest:1: DeprecationWarning: Setting min_part=0 violates internal assumptions of Partition objects. Some methods may produce errors or WRONG results ! This will soon be disabled.
     3682        See http://trac.sagemath.org/14138 for details.
    36833683        [[4, 0, 0], [3, 1, 0], [2, 2, 0], [2, 1, 1]]
    36843684        sage: [x for x in Partitions(4, min_length=3, min_part=0)]
    36853685        [[4, 0, 0], [3, 1, 0], [2, 2, 0], [2, 1, 1], [1, 1, 1, 1]]
     
    37603760                if 'min_part' not in kwargs:
    37613761                    kwargs['min_part'] = 1
    37623762                elif kwargs['min_part'] == 0:
    3763                     from warnings import warn
    3764                     warn("Currently, setting min_part=0 produces Partition objects which violate internal assumptions.  Calling methods on these objects may produce errors or WRONG results!", RuntimeWarning)
     3763                    from sage.misc.superseded import deprecation
     3764                    deprecation(14138,"Setting min_part=0 violates internal "+
     3765                                "assumptions of Partition objects. Some "+
     3766                                "methods may produce errors or WRONG results "+
     3767                                "! This will soon be disabled.")
    37653768
    37663769                if 'max_slope' not in kwargs:
    37673770                    kwargs['max_slope'] = 0
     
    38213824   
    38223825    Element = Partition_class
    38233826
    3824 
    38253827    def cardinality(self):
    38263828        """
    38273829        Returns the number of integer partitions.
     
    50785080         [[1, 3, 4, 2], [5, 7, 6]],
    50795081         [[1, 4, 2, 3], [5, 7, 6]],
    50805082         [[1, 4, 3, 2], [5, 7, 6]]]
    5081 
    5082     Note that repeated elements are not considered equal::
    5083 
    5084         sage: cyclic_permutations_of_partition([[1,2,3],[4,4,4]])
    5085         [[[1, 2, 3], [4, 4, 4]],
    5086          [[1, 3, 2], [4, 4, 4]],
    5087          [[1, 2, 3], [4, 4, 4]],
    5088          [[1, 3, 2], [4, 4, 4]]]
    50895083    """
    50905084    from sage.misc.superseded import deprecation
    50915085    deprecation(13072,'cyclic_permutations_of_partition is being removed from the global namespace. Use sage.combinat.set_partition.cyclic_permutations_of_set_partition instead.')
     
    51195113         [[1, 3, 4, 2], [5, 7, 6]],
    51205114         [[1, 4, 2, 3], [5, 7, 6]],
    51215115         [[1, 4, 3, 2], [5, 7, 6]]]
    5122 
    5123     Note that repeated elements are not considered equal::
    5124 
    5125         sage: list(cyclic_permutations_of_partition_iterator([[1,2,3],[4,4,4]]))
    5126         [[[1, 2, 3], [4, 4, 4]],
    5127          [[1, 3, 2], [4, 4, 4]],
    5128          [[1, 2, 3], [4, 4, 4]],
    5129          [[1, 3, 2], [4, 4, 4]]]
    51305116    """
    51315117    from sage.misc.superseded import deprecation
    51325118    deprecation(13072,'cyclic_permutations_of_partition_iterator is being removed from the global namespace. Please use sage.combinat.set_partition.cyclic_permutations_of_set_partition_iterator instead.')
  • sage/combinat/set_partition.py

    diff --git a/sage/combinat/set_partition.py b/sage/combinat/set_partition.py
    a b  
    516516         [[1, 3, 4, 2], [5, 7, 6]],
    517517         [[1, 4, 2, 3], [5, 7, 6]],
    518518         [[1, 4, 3, 2], [5, 7, 6]]]
    519 
    520     Note that repeated elements are not considered equal::
    521 
    522         sage: cyclic_permutations_of_set_partition([[1,2,3],[4,4,4]])
    523         [[[1, 2, 3], [4, 4, 4]],
    524          [[1, 3, 2], [4, 4, 4]],
    525          [[1, 2, 3], [4, 4, 4]],
    526          [[1, 3, 2], [4, 4, 4]]]
    527519    """
    528520    return list(cyclic_permutations_of_set_partition_iterator(set_part))
    529521
     
    553545         [[1, 3, 4, 2], [5, 7, 6]],
    554546         [[1, 4, 2, 3], [5, 7, 6]],
    555547         [[1, 4, 3, 2], [5, 7, 6]]]
    556 
    557     Note that repeated elements are not considered equal::
    558 
    559         sage: list(cyclic_permutations_of_set_partition_iterator([[1,2,3],[4,4,4]]))
    560         [[[1, 2, 3], [4, 4, 4]],
    561          [[1, 3, 2], [4, 4, 4]],
    562          [[1, 2, 3], [4, 4, 4]],
    563          [[1, 3, 2], [4, 4, 4]]]
    564548    """
    565549    from combinat import cyclic_permutations_iterator
    566550    if len(set_part) == 1: