Skip to content

Commit

Permalink
Trac #18674: Remove deprecated classes ChooseNK and SplitNK
Browse files Browse the repository at this point in the history
Ticket #10534 deprecated classes ChooseNK and SplitNK and that ticket
was merged in Sage 6.3.  This ticket is to remove those functions.

'''Not to be merged before 10 August 2015'''

URL: http://trac.sagemath.org/18674
Reported by: zabrocki
Ticket author(s): Mike Zabrocki
Reviewer(s): Travis Scrimshaw
  • Loading branch information
Release Manager authored and vbraun committed Nov 5, 2015
2 parents 25e78b9 + fd5dbe2 commit 91451ea
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 195 deletions.
2 changes: 0 additions & 2 deletions src/doc/en/reference/combinat/module_list.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ Comprehensive Module list
sage/combinat/binary_tree
sage/combinat/cartesian_product
sage/combinat/catalog_partitions
sage/combinat/choose_nk
sage/combinat/cluster_algebra_quiver/__init__
sage/combinat/cluster_algebra_quiver/all
sage/combinat/cluster_algebra_quiver/cluster_seed
Expand Down Expand Up @@ -316,7 +315,6 @@ Comprehensive Module list
sage/combinat/species/structure
sage/combinat/species/subset_species
sage/combinat/species/sum_species
sage/combinat/split_nk
sage/combinat/subset
sage/combinat/subsets_hereditary
sage/combinat/subsets_pairwise
Expand Down
134 changes: 15 additions & 119 deletions src/sage/combinat/choose_nk.py
Original file line number Diff line number Diff line change
@@ -1,57 +1,6 @@
"""
Deprecated combinations
# the following functions have been moved to sage.combinat.combination
import sage.combinat.combination as combination

AUTHORS:
- Mike Hansen (2007): initial implementation
- Vincent Delecroix (2014): deprecation
"""
#*****************************************************************************
# Copyright (C) 2007 Mike Hansen <mhansen@gmail.com>,
#
# Distributed under the terms of the GNU General Public License (GPL)
#
# This code is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# The full text of the GPL is available at:
#
# http://www.gnu.org/licenses/
#*****************************************************************************
from sage.rings.arith import binomial

def ChooseNK(n, k):
"""
All possible choices of k elements out of range(n) without repetitions.
The elements of the output are tuples of Python int (and not Sage Integer).
This was deprecated in :trac:`10534` for :func:`Combinations`
(or ``itertools.combinations`` for doing iteration).
EXAMPLES::
sage: from sage.combinat.choose_nk import ChooseNK
sage: c = ChooseNK(4,2)
doctest:...: DeprecationWarning: ChooseNk is deprecated and will be
removed. Use Combinations instead (or combinations from the itertools
module for iteration)
See http://trac.sagemath.org/10534 for details.
sage: c.first()
[0, 1]
sage: c.list()
[[0, 1], [0, 2], [0, 3], [1, 2], [1, 3], [2, 3]]
"""
from sage.misc.superseded import deprecation
deprecation(10534, "ChooseNk is deprecated and will be removed. Use Combinations instead (or combinations from the itertools module for iteration)")
from sage.combinat.combination import Combinations
return Combinations(n,k)

#TODO: the following functions are used sage.combinat.combination and
# sage.combinat.subset. It might be good to move them somewhere else.
def rank(comb, n, check=True):
"""
Return the rank of ``comb`` in the subsets of ``range(n)`` of size ``k``
Expand All @@ -64,6 +13,8 @@ def rank(comb, n, check=True):
sage: import sage.combinat.choose_nk as choose_nk
sage: choose_nk.rank((), 3)
doctest:...: DeprecationWarning: choose_nk.rank is deprecated and will be removed. Use combination.rank instead
See http://trac.sagemath.org/18674 for details.
0
sage: choose_nk.rank((0,), 3)
0
Expand Down Expand Up @@ -94,55 +45,14 @@ def rank(comb, n, check=True):
sage: choose_nk.rank([0,1,2], 3)
0
"""
k = len(comb)
if check:
if k > n:
raise ValueError("len(comb) must be <= n")
comb = [int(_) for _ in comb]
for i in xrange(k - 1):
if comb[i + 1] <= comb[i]:
raise ValueError("comb must be a subword of (0,1,...,n)")

#Generate the combinadic from the
#combination

#w = [n-1-comb[i] for i in xrange(k)]

#Calculate the integer that is the dual of
#the lexicographic index of the combination
r = k
t = 0
for i in range(k):
t += binomial(n - 1 - comb[i], r)
r -= 1

return binomial(n,k)-t-1



def _comb_largest(a,b,x):
"""
Returns the largest w < a such that binomial(w,b) <= x.
EXAMPLES::
sage: from sage.combinat.choose_nk import _comb_largest
sage: _comb_largest(6,3,10)
5
sage: _comb_largest(6,3,5)
4
"""
w = a - 1

while binomial(w,b) > x:
w -= 1

return w
from sage.misc.superseded import deprecation
deprecation(18674, "choose_nk.rank is deprecated and will be removed. Use combination.rank instead")
return combination.rank(comb, n, check)

def from_rank(r, n, k):
"""
Returns the combination of rank r in the subsets of range(n) of
size k when listed in lexicographic order.
r"""
Returns the combination of rank ``r`` in the subsets of ``range(n)`` of
size ``k`` when listed in lexicographic order.
The algorithm used is based on combinadics and James McCaffrey's
MSDN article. See: http://en.wikipedia.org/wiki/Combinadic
Expand All @@ -151,6 +61,8 @@ def from_rank(r, n, k):
sage: import sage.combinat.choose_nk as choose_nk
sage: choose_nk.from_rank(0,3,0)
doctest:...: DeprecationWarning: choose_nk.from_rank is deprecated and will be removed. Use combination.from_rank instead
See http://trac.sagemath.org/18674 for details.
()
sage: choose_nk.from_rank(0,3,1)
(0,)
Expand All @@ -167,23 +79,7 @@ def from_rank(r, n, k):
sage: choose_nk.from_rank(0,3,3)
(0, 1, 2)
"""
if k < 0:
raise ValueError("k must be > 0")
if k > n:
raise ValueError("k must be <= n")

a = n
b = k
x = binomial(n, k) - 1 - r # x is the 'dual' of m
comb = [None] * k

for i in xrange(k):
comb[i] = _comb_largest(a, b, x)
x = x - binomial(comb[i], b)
a = comb[i]
b = b - 1

for i in xrange(k):
comb[i] = (n - 1) - comb[i]
from sage.misc.superseded import deprecation
deprecation(18674, "choose_nk.from_rank is deprecated and will be removed. Use combination.from_rank instead")
return combination.from_rank(r, n, k)

return tuple(comb)
135 changes: 134 additions & 1 deletion src/sage/combinat/combination.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
from sage.rings.all import ZZ, Integer
from sage.rings.arith import binomial
from combinat import CombinatorialClass
from choose_nk import rank, from_rank
from integer_vector import IntegerVectors
from sage.misc.misc import uniq

Expand Down Expand Up @@ -466,6 +465,140 @@ def rank(self, x):
return rank(x, len(self.mset))


def rank(comb, n, check=True):
"""
Return the rank of ``comb`` in the subsets of ``range(n)`` of size ``k``
where ``k`` is the length of ``comb``.
The algorithm used is based on combinadics and James McCaffrey's
MSDN article. See: :wikipedia:`Combinadic`.
EXAMPLES::
sage: import sage.combinat.combination as combination
sage: combination.rank((), 3)
0
sage: combination.rank((0,), 3)
0
sage: combination.rank((1,), 3)
1
sage: combination.rank((2,), 3)
2
sage: combination.rank((0,1), 3)
0
sage: combination.rank((0,2), 3)
1
sage: combination.rank((1,2), 3)
2
sage: combination.rank((0,1,2), 3)
0
sage: combination.rank((0,1,2,3), 3)
Traceback (most recent call last):
...
ValueError: len(comb) must be <= n
sage: combination.rank((0,0), 2)
Traceback (most recent call last):
...
ValueError: comb must be a subword of (0,1,...,n)
sage: combination.rank([1,2], 3)
2
sage: combination.rank([0,1,2], 3)
0
"""
k = len(comb)
if check:
if k > n:
raise ValueError("len(comb) must be <= n")
comb = [int(_) for _ in comb]
for i in xrange(k - 1):
if comb[i + 1] <= comb[i]:
raise ValueError("comb must be a subword of (0,1,...,n)")

#Generate the combinadic from the
#combination

#w = [n-1-comb[i] for i in xrange(k)]

#Calculate the integer that is the dual of
#the lexicographic index of the combination
r = k
t = 0
for i in range(k):
t += binomial(n - 1 - comb[i], r)
r -= 1

return binomial(n,k)-t-1

def _comb_largest(a,b,x):
r"""
Returns the largest `w < a` such that `binomial(w,b) <= x`.
EXAMPLES::
sage: from sage.combinat.combination import _comb_largest
sage: _comb_largest(6,3,10)
5
sage: _comb_largest(6,3,5)
4
"""
w = a - 1

while binomial(w,b) > x:
w -= 1

return w

def from_rank(r, n, k):
r"""
Returns the combination of rank ``r`` in the subsets of
``range(n)`` of size ``k`` when listed in lexicographic order.
The algorithm used is based on combinadics and James McCaffrey's
MSDN article. See: :wikipedia:`Combinadic`
EXAMPLES::
sage: import sage.combinat.combination as combination
sage: combination.from_rank(0,3,0)
()
sage: combination.from_rank(0,3,1)
(0,)
sage: combination.from_rank(1,3,1)
(1,)
sage: combination.from_rank(2,3,1)
(2,)
sage: combination.from_rank(0,3,2)
(0, 1)
sage: combination.from_rank(1,3,2)
(0, 2)
sage: combination.from_rank(2,3,2)
(1, 2)
sage: combination.from_rank(0,3,3)
(0, 1, 2)
"""
if k < 0:
raise ValueError("k must be > 0")
if k > n:
raise ValueError("k must be <= n")

a = n
b = k
x = binomial(n, k) - 1 - r # x is the 'dual' of m
comb = [None] * k

for i in xrange(k):
comb[i] = _comb_largest(a, b, x)
x = x - binomial(comb[i], b)
a = comb[i]
b = b - 1

for i in xrange(k):
comb[i] = (n - 1) - comb[i]

return tuple(comb)

##########################################################
# Deprecations

Expand Down
2 changes: 0 additions & 2 deletions src/sage/combinat/enumerated_sets.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,6 @@
-------------------------
- :ref:`sage.combinat.permutation_nk`
- :ref:`sage.combinat.split_nk`
- :ref:`sage.combinat.choose_nk`
- :ref:`sage.combinat.multichoose_nk`
- :ref:`sage.combinat.gray_codes`
Expand Down
Loading

0 comments on commit 91451ea

Please sign in to comment.