Skip to main content

No project description provided

Project description

combin

coverage_badge macos workflow status windows workflow status linux workflow status

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

## Fix k-combinations of an n-set
n = 10 # universe size 
k = 3  # size of each combination

## Ranks each 3-tuple into its index in the combinatorial number system  
C = combinations(range(n), k) # from itertools 
R = comb_to_rank(C, order='lex', n = n)
print(R)
# [0, 1, 2, ..., 119]

## If a generator is given, the default return type is a list
assert R == list(range(120))
# True

## Alternatively, combin has native support for NumPy arrays
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)))
# True

## An alternative bijection can also be chosen, such as the colexicographical order
## Note the colex order doesn't require the universe size (n)
print(comb_to_rank(C, order='colex'))
# [0, 1, 4, ..., 119]

## Unranking is just as easy: just supply n, k, and the order
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}")
# Equal? True, combs: [[0, 1, 2], [0, 1, 3], [0, 1, 4], ..., [6, 8, 9], [7, 8, 9]]

## Uniformly sampling from k-combinations becomes trivial
ind = np.random.choice(range(comb(n,k)), size=10, replace=False)
random_combs = rank_to_comb(ind, k=k, order='colex')
# [[2, 7, 8], [1, 4, 6], ...]

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

combin-0.1.7.tar.gz (63.4 kB view hashes)

Uploaded Source

Built Distributions

combin-0.1.7-cp312-cp312-win_amd64.whl (119.8 kB view hashes)

Uploaded CPython 3.12 Windows x86-64

combin-0.1.7-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (157.9 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

combin-0.1.7-cp312-cp312-macosx_11_0_arm64.whl (115.7 kB view hashes)

Uploaded CPython 3.12 macOS 11.0+ ARM64

combin-0.1.7-cp312-cp312-macosx_10_9_x86_64.whl (121.7 kB view hashes)

Uploaded CPython 3.12 macOS 10.9+ x86-64

combin-0.1.7-cp311-cp311-win_amd64.whl (120.7 kB view hashes)

Uploaded CPython 3.11 Windows x86-64

combin-0.1.7-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (159.2 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

combin-0.1.7-cp311-cp311-macosx_11_0_arm64.whl (116.3 kB view hashes)

Uploaded CPython 3.11 macOS 11.0+ ARM64

combin-0.1.7-cp311-cp311-macosx_10_9_x86_64.whl (122.3 kB view hashes)

Uploaded CPython 3.11 macOS 10.9+ x86-64

combin-0.1.7-cp310-cp310-win_amd64.whl (118.4 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

combin-0.1.7-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (157.9 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

combin-0.1.7-cp310-cp310-macosx_11_0_arm64.whl (115.2 kB view hashes)

Uploaded CPython 3.10 macOS 11.0+ ARM64

combin-0.1.7-cp310-cp310-macosx_10_9_x86_64.whl (120.8 kB view hashes)

Uploaded CPython 3.10 macOS 10.9+ x86-64

combin-0.1.7-cp39-cp39-win_amd64.whl (118.4 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

combin-0.1.7-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (159.7 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

combin-0.1.7-cp39-cp39-macosx_11_0_arm64.whl (115.3 kB view hashes)

Uploaded CPython 3.9 macOS 11.0+ ARM64

combin-0.1.7-cp39-cp39-macosx_10_9_x86_64.whl (120.8 kB view hashes)

Uploaded CPython 3.9 macOS 10.9+ x86-64

combin-0.1.7-cp38-cp38-win_amd64.whl (118.3 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

combin-0.1.7-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (158.9 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

combin-0.1.7-cp38-cp38-macosx_11_0_arm64.whl (115.2 kB view hashes)

Uploaded CPython 3.8 macOS 11.0+ ARM64

combin-0.1.7-cp38-cp38-macosx_10_9_x86_64.whl (120.8 kB view hashes)

Uploaded CPython 3.8 macOS 10.9+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page