A recuring pain point, for me and for many others who use Python for mathematical computations, is that the standard library does not provide a function for computing binomial coefficients.
I would like to suggest adding a function, in the math module, with the following signature:
binomial(n: Integral, k: Integral) -> Integral
A simple native Python implementation would be:
from functools import reduce
from math import factorial
from numbers import Integral
def binomial(n: Integral, k: Integral) -> Integral:
if k < 0 or n < 0:
raise ValueError("math.binomial takes non-negative parameters.")
k = min(k, n-k)
num, den = 1, 1
for i in range(k):
num = num * (n - i)
den = den * (i + 1)
return num//den
As far as I can tell, all of the math module is implemented in C, so this should be done in C too, but the implemented behaviour should be equivalent.
I will submit a Github pull request once I have a ready-to-review patch.
Not starting a PEP, per [PEP 1]:
> Small enhancements or patches often don't need a PEP and can be injected into the Python development workflow with a patch submission to the Python issue tracker.
[PEP 1]: https://www.python.org/dev/peps/pep-0001/#id36 |