Ticket #14138: trac_14138.patch
File trac_14138.patch, 18.7 KB (added by , 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 1 1 r""" 2 2 Combinatorial Functions 3 3 4 AUTHORS:5 6 - David Joyner (2006-07): initial implementation.7 8 - William Stein (2006-07): editing of docs and code; many9 optimizations, refinements, and bug fixes in corner cases10 11 - David Joyner (2006-09): bug fix for combinations, added12 permutations_iterator, combinations_iterator from Python Cookbook,13 edited docs.14 15 - David Joyner (2007-11): changed permutations, added hadamard_matrix16 17 - Florent Hivert (2009-02): combinatorial class cleanup18 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 24 4 This module implements some combinatorial functions, as listed 25 5 below. For a more detailed description, see the relevant 26 6 docstrings. 27 7 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 37 14 confused with the Catalan constant) 38 15 39 - Eulerian/Euler numbers, ``euler_number`` (Maxima)40 41 - Fibonacci numbers, ``fibonacci`` (PARI) and42 ``fibonacci_number`` (GAP) The PARI version is16 - Eulerian/Euler numbers, :func:`euler_number` (Maxima) 17 18 - Fibonacci numbers, :func:`fibonacci` (PARI) and 19 :func:`fibonacci_number` (GAP) The PARI version is 43 20 better. 44 21 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 69 35 set S is a ordered selection with repetitions of S and is 70 36 represented by a sorted list of length k containing elements from 71 37 S. 72 38 73 - Unordered tuples of a set, ``unordered_tuple`` and74 ``number_of_unordered_tuples``. An unordered tuple39 - Unordered tuples of a set, :func:`unordered_tuples` and 40 :func:`number_of_unordered_tuples`. An unordered tuple 75 41 of length k of set S is an unordered selection with repetitions of S 76 42 and is represented by a sorted list of length k containing elements 77 43 from S. 78 44 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 91 67 of set S is a set of pairwise disjoint nonempty sets with union S 92 68 and is represented by a sorted list of such sets. 93 69 94 - Partitions of an integer, ``Partitions``.70 - Partitions of an integer, :func:`Partitions`. 95 71 An unordered partition of n is an unordered sum 96 72 `n = p_1+p_2 +\ldots+ p_k` of positive integers and is 97 73 represented by the list `p = [p_1,p_2,\ldots,p_k]`, in 98 74 nonincreasing order, i.e., `p1\geq p_2 ...\geq p_k`. 99 75 100 76 - Ordered partitions of an integer, 101 ``ordered_partitions``,102 ``number_of_ordered_partitions``. An ordered77 :func:`ordered_partitions`, 78 :func:`number_of_ordered_partitions`. An ordered 103 79 partition of n is an ordered sum 104 80 `n = p_1+p_2 +\ldots+ p_k` of positive integers and is 105 81 represented by the list `p = [p_1,p_2,\ldots,p_k]`, in 106 82 nonincreasing order, i.e., `p1\geq p_2 ...\geq p_k`. 107 83 108 - ``partitions_greatest`` implements a special type84 - :func:`partitions_greatest` implements a special type 109 85 of restricted partition. 110 86 111 - ``partitions_greatest_eq`` is another type of87 - :func:`partitions_greatest_eq` is another type of 112 88 restricted partition. 113 89 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):** 130 99 131 100 The ``sage.rings.arith`` module contains the following 132 101 combinatorial functions: 133 102 134 135 103 - binomial the binomial coefficient (wrapped from PARI) 136 104 137 105 - factorial (wrapped from PARI) … … 139 107 - partition (from the Python Cookbook) Generator of the list of 140 108 all the partitions of the integer `n`. 141 109 142 - ``number_of_partitions`` (wrapped from PARI) the110 - :func:`number_of_partitions` (wrapped from PARI) the 143 111 *number* of partitions: 144 112 145 - ``falling_factorial`` Definition: for integer113 - :func:`falling_factorial` Definition: for integer 146 114 `a \ge 0` we have `x(x-1) \cdots (x-a+1)`. In all 147 115 other cases we use the GAMMA-function: 148 116 `\frac {\Gamma(x+1)} {\Gamma(x-a+1)}`. 149 117 150 - ``rising_factorial`` Definition: for integer118 - :func:`rising_factorial` Definition: for integer 151 119 `a \ge 0` we have `x(x+1) \cdots (x+a-1)`. In all 152 120 other cases we use the GAMMA-function: 153 121 `\frac {\Gamma(x+a)} {\Gamma(x)}`. … … 158 126 159 127 \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)}. 160 128 161 162 163 164 129 The ``sage.groups.perm_gps.permgroup_elements`` 165 130 contains the following combinatorial functions: 166 131 … … 182 147 REFERENCES: 183 148 184 149 - http://en.wikipedia.org/wiki/Twelvefold_way (general reference) 150 151 AUTHORS: 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 171 Functions and classes 172 --------------------- 185 173 """ 186 174 187 175 #***************************************************************************** … … 242 230 ans=gap.eval("Bell(%s)"%ZZ(n)) 243 231 return ZZ(ans) 244 232 245 ## def bernoulli_number(n):246 ## r"""247 ## Returns the n-th Bernoulli number $B_n$; $B_n/n!$ is the248 ## 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/66253 ## """254 ## ans=gap.eval("Bernoulli(%s)"%n)255 ## return QQ(ans) ## use QQ, not eval, here256 257 233 def catalan_number(n): 258 234 r""" 259 235 Returns the n-th Catalan number … … 2034 2010 Returns the size of combinations(mset,k). IMPLEMENTATION: Wraps 2035 2011 GAP's NrCombinations. 2036 2012 2037 .. note::2038 2039 ``mset`` must be a list of integers or strings (i.e., this is2040 very restricted).2041 2042 2013 EXAMPLES:: 2043 2014 2044 2015 sage: mset = [1,1,2,3,4,4,5] 2045 2016 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. 2046 2019 12 2047 2020 """ 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() 2049 2024 2050 2025 def arrangements(mset,k): 2051 2026 r""" … … 2112 2087 2113 2088 def number_of_arrangements(mset,k): 2114 2089 """ 2115 Returns the size of arrangements(mset,k). Wraps GAP's 2116 NrArrangements. 2090 Returns the size of arrangements(mset,k). 2117 2091 2118 2092 EXAMPLES:: 2119 2093 2120 2094 sage: mset = [1,1,2,3,4,4,5] 2121 2095 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. 2122 2098 22 2123 2099 """ 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() 2125 2103 2126 2104 def derangements(mset): 2127 2105 """ … … 2218 2196 y.append(s) 2219 2197 ans.append(y) 2220 2198 return ans 2221 ## code wrapping GAP's Tuples:2222 #ans=gap.eval("Tuples(%s,%s)"%(S,k))2223 #return eval(ans)2224 2225 2199 2226 2200 def number_of_tuples(S,k): 2227 2201 """ … … 2349 2323 sage: permutations(mset) 2350 2324 [[1, 2, 2], [2, 1, 2], [2, 2, 1]] 2351 2325 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. 2353 2328 [1, 2, 2] 2354 2329 [2, 1, 2] 2355 2330 [2, 2, 1] … … 2362 2337 sage: [x for x in X] 2363 2338 [[0, 1], [0, 2], [1, 0], [1, 2], [2, 0], [2, 1]] 2364 2339 """ 2340 deprecation(14138, 'Use the Permutations object instead.') 2365 2341 items = mset 2366 2342 if n is None: 2367 2343 n = len(items) 2344 from sage.combinat.permutation import Permutations 2368 2345 for i in range(len(items)): 2369 2346 v = items[i:i+1] 2370 2347 if n == 1: 2371 2348 yield v 2372 2349 else: 2373 2350 rest = items[:i] + items[i+1:] 2374 for p in permutations_iterator(rest, n-1):2351 for p in Permutations(rest, n-1): 2375 2352 yield v + p 2376 2353 2377 2354 def number_of_permutations(mset): … … 2398 2375 2399 2376 sage: mset = [1,1,2,2,2] 2400 2377 sage: number_of_permutations(mset) 2378 doctest:1: DeprecationWarning: Use the Permutations object instead. 2379 See http://trac.sagemath.org/14138 for details. 2401 2380 10 2402 2381 """ 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() 2414 2385 2415 2386 def cyclic_permutations(mset): 2416 2387 """ … … 2430 2401 ... print cycle 2431 2402 ['a', 'b', 'c'] 2432 2403 ['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]]2438 2404 """ 2439 2405 return list(cyclic_permutations_iterator(mset)) 2440 2406 … … 2457 2423 ... print cycle 2458 2424 ['a', 'b', 'c'] 2459 2425 ['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]]2465 2426 """ 2466 2427 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:]): 2468 2431 yield [mset[0]] + perm 2469 2432 else: 2470 2433 yield mset -
sage/combinat/multichoose_nk.py
diff --git a/sage/combinat/multichoose_nk.py b/sage/combinat/multichoose_nk.py
a b 2 2 Low-level multichoose 3 3 """ 4 4 #***************************************************************************** 5 # Copyright (C) 2007 Mike Hansen <mhansen@gmail.com>, 5 # Copyright (C) 2007 Mike Hansen <mhansen@gmail.com>, 6 6 # 7 7 # Distributed under the terms of the GNU General Public License (GPL) 8 8 # … … 23 23 def __init__(self, n, k): 24 24 """ 25 25 TESTS:: 26 26 27 27 sage: a = MultichooseNK(3,2) 28 28 sage: a == loads(dumps(a)) 29 29 True -
sage/combinat/partition.py
diff --git a/sage/combinat/partition.py b/sage/combinat/partition.py
a b 581 581 gens.append( range(1,self.size()+1) ) # to ensure we get a subgroup of Sym_n 582 582 return PermutationGroup( gens ) 583 583 584 585 584 def young_subgroup_generators(self): 586 585 """ 587 586 Return an indexing set for the generators of the corresponding Young … … 3679 3678 :: 3680 3679 3681 3680 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. 3683 3683 [[4, 0, 0], [3, 1, 0], [2, 2, 0], [2, 1, 1]] 3684 3684 sage: [x for x in Partitions(4, min_length=3, min_part=0)] 3685 3685 [[4, 0, 0], [3, 1, 0], [2, 2, 0], [2, 1, 1], [1, 1, 1, 1]] … … 3760 3760 if 'min_part' not in kwargs: 3761 3761 kwargs['min_part'] = 1 3762 3762 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.") 3765 3768 3766 3769 if 'max_slope' not in kwargs: 3767 3770 kwargs['max_slope'] = 0 … … 3821 3824 3822 3825 Element = Partition_class 3823 3826 3824 3825 3827 def cardinality(self): 3826 3828 """ 3827 3829 Returns the number of integer partitions. … … 5078 5080 [[1, 3, 4, 2], [5, 7, 6]], 5079 5081 [[1, 4, 2, 3], [5, 7, 6]], 5080 5082 [[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]]]5089 5083 """ 5090 5084 from sage.misc.superseded import deprecation 5091 5085 deprecation(13072,'cyclic_permutations_of_partition is being removed from the global namespace. Use sage.combinat.set_partition.cyclic_permutations_of_set_partition instead.') … … 5119 5113 [[1, 3, 4, 2], [5, 7, 6]], 5120 5114 [[1, 4, 2, 3], [5, 7, 6]], 5121 5115 [[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]]]5130 5116 """ 5131 5117 from sage.misc.superseded import deprecation 5132 5118 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 516 516 [[1, 3, 4, 2], [5, 7, 6]], 517 517 [[1, 4, 2, 3], [5, 7, 6]], 518 518 [[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]]]527 519 """ 528 520 return list(cyclic_permutations_of_set_partition_iterator(set_part)) 529 521 … … 553 545 [[1, 3, 4, 2], [5, 7, 6]], 554 546 [[1, 4, 2, 3], [5, 7, 6]], 555 547 [[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]]]564 548 """ 565 549 from combinat import cyclic_permutations_iterator 566 550 if len(set_part) == 1: