[issue35431] The math module should provide a function for computing binomial coefficients

2018-12-07 Thread kellerfuchs
kellerfuchs added the comment: @Steven D'Aprano: > all call this nCr, and nPr for the permutation version. This matches > the notation taught in secondary school maths classes in Australia. > That's common and familiar notation for secondary school students, but > personally I'm not

[issue35431] The math module should provide a function for computing binomial coefficients

2018-12-07 Thread kellerfuchs
kellerfuchs added the comment: > I'd personally prefer that floats not be accepted; I think this was a > misfeature of `factorial` that we shouldn't compound. OK; I only went with that because I assumed there were Good ReasonsĀ© that factorial did it, but if rejecting integral floats isn't

[issue35431] The math module should provide a function for computing binomial coefficients

2018-12-07 Thread kellerfuchs
kellerfuchs added the comment: @Mark Dickinson: > You don't mean the "k=0" part of that, right? Indeed not; the tests in the PR actually assert binomial(n, n) == binomial(n, 0) == 1. -- ___ Python tracker

[issue35431] The math module should provide a function for computing binomial coefficients

2018-12-07 Thread kellerfuchs
kellerfuchs added the comment: @Serhiy Storchaka: > I think that it is better to add this function in a new module imath, which > could contain other integer functions imath is a fine idea, and you already started a discussion in python-ideas@, but it's a much bigger undertaking than just

[issue35431] The math module should provide a function for computing binomial coefficients

2018-12-07 Thread Steven D'Aprano
Steven D'Aprano added the comment: On Fri, Dec 07, 2018 at 01:37:36PM +, Mark Dickinson wrote: > I'd personally prefer that floats not be accepted; Agreed. We can always add support for floats later, but its hard to remove it if it turns out to be problematic. We ought to require n, r

[issue35431] The math module should provide a function for computing binomial coefficients

2018-12-07 Thread Steven D'Aprano
Steven D'Aprano added the comment: > > Mathematically, `binomial(n, k)` for `k > n` is defined as 0. > > It's not so clear cut. You can find different definitions out there. > Knuth et. al., for example, in the book "Concrete Mathematics", extend > the definition not just to negative k, but

[issue35431] The math module should provide a function for computing binomial coefficients

2018-12-07 Thread Steven D'Aprano
Steven D'Aprano added the comment: On Fri, Dec 07, 2018 at 12:04:44AM +, Raymond Hettinger wrote: > Also, I'm not sure what the predominant choice for variable names > should be, "n things taken r at a time" or "n things taken k at time". > > Also, it's worth considering whether the

[issue35431] The math module should provide a function for computing binomial coefficients

2018-12-07 Thread Mark Dickinson
Mark Dickinson added the comment: One more decision that needs to be made: should the new function accept integer-valued floats? Or should any `float` input give a TypeError. I'd personally prefer that floats not be accepted; I think this was a misfeature of `factorial` that we shouldn't

[issue35431] The math module should provide a function for computing binomial coefficients

2018-12-07 Thread Mark Dickinson
Mark Dickinson added the comment: @kellerfuchs: > From a mathematical standpoint, (n choose k) is defined for all non-negative > k, n, with (n chooze k) = 0 when k>n or k=0. You don't mean the "k=0" part of that, right? -- ___ Python tracker

[issue35431] The math module should provide a function for computing binomial coefficients

2018-12-07 Thread Mark Dickinson
Mark Dickinson added the comment: > Mathematically, `binomial(n, k)` for `k > n` is defined as 0. It's not so clear cut. You can find different definitions out there. Knuth et. al., for example, in the book "Concrete Mathematics", extend the definition not just to negative k, but to

[issue35431] The math module should provide a function for computing binomial coefficients

2018-12-07 Thread Serhiy Storchaka
Serhiy Storchaka added the comment: Mathematically, `binomial(n, k)` for `k > n` is defined as 0. -- ___ Python tracker ___ ___

[issue35431] The math module should provide a function for computing binomial coefficients

2018-12-07 Thread Serhiy Storchaka
Serhiy Storchaka added the comment: I think that it is better to add this function in a new module imath, which could contain other integer functions: factorial, gcs, as_integer_ration, isqrt, isprime, primes. See https://mail.python.org/pipermail/python-ideas/2018-July/051917.html

[issue35431] The math module should provide a function for computing binomial coefficients

2018-12-07 Thread kellerfuchs
kellerfuchs added the comment: @Raymond Hettinger: > it's worth considering whether the function should be called "binomial", > "choose", "combinations", or "comb" Here goes the bike shed! :) Kidding, this is a pretty important ergonomics/discoverability concern, and I hadn't given it much

[issue35431] The math module should provide a function for computing binomial coefficients

2018-12-07 Thread kellerfuchs
Change by kellerfuchs : -- keywords: +patch pull_requests: +10253 stage: -> patch review ___ Python tracker ___ ___

[issue35431] The math module should provide a function for computing binomial coefficients

2018-12-07 Thread Mark Dickinson
Mark Dickinson added the comment: There's also the question of what inputs should be considered valid: `binomial(n, k)` for `k > n` should either return 0 or be a `ValueError`, but which? Same question for `k < 0`. There's a symmetry argument for allowing `k < 0` if you allow `k > n`, but I

[issue35431] The math module should provide a function for computing binomial coefficients

2018-12-07 Thread Mark Dickinson
Mark Dickinson added the comment: For some ranges of inputs, it may make sense to use the apparently naive implementation `factorial(n) // factorial(k) // factorial(n - k)`. The current factorial implementation is significantly optimised, and using it directly may be faster than using an

[issue35431] The math module should provide a function for computing binomial coefficients

2018-12-06 Thread Raymond Hettinger
Raymond Hettinger added the comment: +1 I have wanted this a number of times. FWIW, most recently I wrote it like this: def comb(n, r): 'Equivalent to factorial(n) // (factorial(r) * factorial(n-r))' c = 1 r = min(r, n-r) for i in range(1, r+1):

[issue35431] The math module should provide a function for computing binomial coefficients

2018-12-06 Thread kellerfuchs
kellerfuchs added the comment: Yes, that was a copypasta mistake (and I also import factorial needlessly) as the file I prototyped it in had some other code for testing my proposed implementation. :) -- ___ Python tracker

[issue35431] The math module should provide a function for computing binomial coefficients

2018-12-06 Thread Steven D'Aprano
Steven D'Aprano added the comment: You import reduce but never use it :-) +1 for this, I certainly miss it too. -- nosy: +steven.daprano ___ Python tracker ___

[issue35431] The math module should provide a function for computing binomial coefficients

2018-12-06 Thread kellerfuchs
New submission from kellerfuchs : 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