combin

Lightweight implementation of various combinatorics routines (Under construction!).
Current routines offered:
- Combinatorial number system ranking/unranking (
rank_to_comb and comb_to_rank)
- Binomial coefficient inversion (
inverse_choose)
Installation
combin is on PyPI and can be installed with the usual command:
python -m pip install combin
If this fails for your system, please file an issue.
Usage
combin supports fast bijections to the combinatorial number system.
from combin import comb_to_rank, rank_to_comb
n = 10
k = 3
C = combinations(range(n), k)
R = comb_to_rank(C, order='lex', n = n)
print(R)
assert R == list(range(120))
C = np.fromiter(combinations(range(n), k), dtype=(np.int16, k))
assert np.all(comb_to_rank(C, order='lex', n=n) == np.arange(comb(n,k)))
print(comb_to_rank(C, order='colex'))
R = np.arange(comb(n,k))
C_lex = rank_to_comb(R, k=k, n=n, order='lex')
print(f"Equal? {np.all(C_lex == C)}, combs: {C_lex}")
ind = np.random.choice(range(comb(n,k)), size=10, replace=False)
random_combs = rank_to_comb(ind, k=k, order='colex')