Skip to main content

A binomial function that extends the domain of combinations to include negative integers.

Project description

Negative Binom

A binomial function that extends the domain of combinations to include negative integers.

Installation

You can install the package using:

pip install negative-binom

Usage

Import the function and use it as follows:

from negative_binom import binom

# Standard case:
print(binom(5, 2)) # Output: 10

# Extended cases:
print(binom(-5, 2)) # Output: 15
print(binom(5, -2)) # Output: 0
print(binom(-5, -2)) # Output: 0

Mathematical Explanation

The binomial coefficient is traditionally defined as:

\binom{m}{n} = \frac{m!}{n!(m-n)!}, \quad \text{for }m \geq n \geq 0

The binom function extends the definition to handle negative values of m and n using "The Pascal Hexagon" provided by Hilton and Pedersen in Extending the Binomial Coefficients to Preserve Symmetry and Pattern:

Pascal's Hexagon

The hexagon is broken into different parts depending on if they contain zeros or non-zeros. Formulas below then can be used to determine the values of the binomial coefficients:

  1. Standard case $(m \geq 0)$
  • If $m \geq n \geq 0$:
\binom{m}{n}=\frac{m!}{n!(m-n)!}
  • Otherwise:
\binom{m}{n}=0
  1. Extended case $(0 > m)$
  • If $n \geq 0$:
\binom{m}{n}=(-1)^n \binom{-m+n-1}{n}
  • If $m \geq n$:
\binom{m}{n}=(-1)^{m+n} \binom{-n-1}{-m-1}
  • Otherwise:
\binom{m}{n}=0

This extended definition of binomial coefficients makes it possible to work with negative values of $m$ and $n$.

Personal Notes

I made this package because I needed a binomial function that included negative arguments as well. math.comb results in error and scipy.stats.binom sets the result to 0 for negative arguments, so they didn't work for me. Hope this helps anyone else.

License

MIT License

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

negative_binom-0.1.5.tar.gz (3.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

negative_binom-0.1.5-py3-none-any.whl (3.4 kB view details)

Uploaded Python 3

File details

Details for the file negative_binom-0.1.5.tar.gz.

File metadata

  • Download URL: negative_binom-0.1.5.tar.gz
  • Upload date:
  • Size: 3.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for negative_binom-0.1.5.tar.gz
Algorithm Hash digest
SHA256 bb5a0683a73eaa41fdbdd7a82637f854bb506f07cb1c8f6b439c6b563e8d8847
MD5 29a4164402c22c9e9ea262051e71b720
BLAKE2b-256 08d3e3c9116989cf057c893bcefa76f6ec6c3c94112e60d15901a93f434f2f00

See more details on using hashes here.

File details

Details for the file negative_binom-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: negative_binom-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 3.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for negative_binom-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 ffaa6d11cef62679f4948df9558cca3b10b2054dab2660aa62cc01fa4ea3a09c
MD5 f8cef303d393e5661e2668d49f2e9609
BLAKE2b-256 91365e019654dd1d66f55b0199ea14296629cf5ba0f64c095cf11fb8f8d43f75

See more details on using hashes here.

Supported by

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