[issue35431] Add a function for computing binomial coefficients to the math module

2019-06-23 Thread Serhiy Storchaka


Serhiy Storchaka  added the comment:


New changeset 914d6b79735e5eabaf4e4d77e3f2ad4eae0beb9a by Serhiy Storchaka in 
branch '3.8':
[3.8] bpo-35431: Test math.comb() and math.perm() for OverflowError only on 
CPython. (GH-14146) (#14226)
https://github.com/python/cpython/commit/914d6b79735e5eabaf4e4d77e3f2ad4eae0beb9a


--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-06-19 Thread Serhiy Storchaka


Change by Serhiy Storchaka :


--
pull_requests: +14060
pull_request: https://github.com/python/cpython/pull/14226

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-06-17 Thread Serhiy Storchaka


Serhiy Storchaka  added the comment:


New changeset 1b8a46d59734a77cd1f5ffcf3bdfcaafd58a87e7 by Serhiy Storchaka in 
branch 'master':
bpo-35431: Test math.comb() and math.perm() for OverflowError only on CPython. 
(GH-14146)
https://github.com/python/cpython/commit/1b8a46d59734a77cd1f5ffcf3bdfcaafd58a87e7


--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-06-17 Thread Serhiy Storchaka


Change by Serhiy Storchaka :


--
pull_requests: +13987
pull_request: https://github.com/python/cpython/pull/14146

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-06-16 Thread miss-islington


Change by miss-islington :


--
pull_requests: +13972
pull_request: https://github.com/python/cpython/pull/14125

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-06-06 Thread Mark Dickinson


Change by Mark Dickinson :


--
pull_requests: +13746
pull_request: https://github.com/python/cpython/pull/13870

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-06-05 Thread Raymond Hettinger


Change by Raymond Hettinger :


--
resolution:  -> fixed
stage: patch review -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-06-04 Thread Raymond Hettinger


Change by Raymond Hettinger :


--
pull_requests: +13687
pull_request: https://github.com/python/cpython/pull/13801

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-06-04 Thread Raymond Hettinger


Raymond Hettinger  added the comment:


New changeset 963eb0f4738456455b9bef7eb531b46805415208 by Raymond Hettinger in 
branch 'master':
bpo-35431: Drop the k <= n requirement (GH-13798)
https://github.com/python/cpython/commit/963eb0f4738456455b9bef7eb531b46805415208


--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-06-04 Thread Raymond Hettinger


Change by Raymond Hettinger :


--
pull_requests: +13683
pull_request: https://github.com/python/cpython/pull/13798

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-06-03 Thread Mark Dickinson


Mark Dickinson  added the comment:

Fine by me. The same change should be applied to `math.perm`.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-06-03 Thread Raymond Hettinger


Raymond Hettinger  added the comment:

> If it were me, I'd drop the k <= n requirement

+1 from me.

--
versions: +Python 3.8 -Python 3.6

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-06-03 Thread Tim Peters


Tim Peters  added the comment:

Python needs a benevolent dictator ;-)

I'd be happy for Mark, Raymond, or me to play that role here.  If it were me, 
I'd drop the k <= n requirement:  both arguments must be non-negative integers. 
 Because a combinatorial (not factorial-, and certainly not gamma-, based) 
definition of `comb()` is best suited for Python's general audience, and 
"number of ways to pick k things out of n without regard to order" makes clear 
sense for any pair of non-negative k and n.  But anything beyond that does not.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-06-03 Thread Raymond Hettinger


Raymond Hettinger  added the comment:

I don't have a preference one way or the other (both ways have plausible 
arguments, error checking vs utility beyond simple combinatorics, and both ways 
have ample precedents).

That said, if we're going to ultimately relax the constraints, it would be 
better to do it now before the API is released.  

Even if we were already in feature freeze, it would be okay to change it (the 
purpose of a beta is to elicit end-user feedback which is what David has just 
given).

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-06-03 Thread Mark Dickinson


Mark Dickinson  added the comment:

Given that (a) we're right up against feature freeze, and (b) the discussions 
about API already happened in plenty of good time some months ago, leading to 
agreement on the initial API, I think we should avoid any last-minute changes 
and stick with what we have for 3.8. Then we can have a more relaxed discussion 
about what, if anything, should be changed for 3.9.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-06-03 Thread Steven D'Aprano


Steven D'Aprano  added the comment:

For what its worth, there are concrete, practical applications for 
binomial coefficients with negative arguments. They are used in 
fractional calculus

https://nrich.maths.org/1365

which in turn has applications in physics, chemistry and other sciences:

https://en.wikipedia.org/wiki/Fractional_calculus#Applications

Take that however you see fit :-)

My own preference is for a comb() function that returns 0 for out of 
bounds values (e.g. "choose 5 items from 4"), and a seperate binomial() 
function that accepts any positive or negative integer values. Even I 
think that fractional and complex arguments are probably a bit too 
exotic for the std lib -- that's Mathematica territory.

And yes, Mathematica does accept fractional and complex arguments to 
the choose() function.

https://www.wolframalpha.com/input/?i=choose%28sqr%28-3.5%29,+sqrt%28-4%2Bi%29%29

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-06-02 Thread Tim Peters


Tim Peters  added the comment:

I'm not fatally opposed to relaxing k <= n.  David makes some good points about 
it, and - as Raymond already noted - "0" is consistent with the behavior of 
itertools.combinations().

The docs would need to change, though, because the factorial "definition" would 
no longer make sense.  Defining it in terms of the falling factorial would, but 
that's too obscure for Python's audience.  Probably a combinatorial definition 
would be best:  `comb(n, k) is the number of k-element subsets of an n-element 
set".  Then it's clear that n and k are cardinals (integers >= 0), and that the 
result is 0 when k > n.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-06-02 Thread David Radcliffe


David Radcliffe  added the comment:

I understand that pure mathematics is not the primary use case. But I would
expect that math.comb() would be used to implement mathematical formulas.
As a simple example, the number of edges in a complete graph on n vertices
is binomial(n, 2), and this should be valid if n < 2. Polynomial
interpolation is another problem where this can occur. However, I do agree
that binomial coefficients with negative arguments are relatively
unimportant.

On Sun, Jun 2, 2019 at 8:59 PM Raymond Hettinger 
wrote:

>
> Raymond Hettinger  added the comment:
>
> Here are a few other data points.  There is no consensus but either
> returning 0 or erroring out seem reasonable.
>
> MS Excel's COMBIN: If number < 0, number_chosen < 0, or number <
> number_chosen, COMBIN returns the #NUM! error value.
>
> Scipy.misc.comb: If k > N, N < 0, or k < 0, then a 0 is returned.
>
> Matlib.nchoose: Error: K must an integer between 0 and N.
>
> Maple.numbcomb: Unclear from the docs what this does
>
> Wolfram alpha:  Returns 0
> https://www.wolframalpha.com/input/?i=10+choose+12
>
> --
>
> ___
> Python tracker 
> 
> ___
>

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-06-02 Thread Tim Peters


Tim Peters  added the comment:

I'm going to repeat part of an earlier comment :-)

"""
Please resist pointless feature creep.  The original report was about comb(n, 
k) for integer n and k with 0 <= k <= n and that's all.  Everyone who commented 
appeared to agree they'd find that useful.
"""

Why can't we take universal agreement for an initial answer? ;-)

Looking at what other packages do doesn't really interest me, unless they 
differ on the cases everyone agreed would be useful - they have different 
audiences than Python has.  Guess what Wolfram returns for

(-4) choose (-5)

Go ahead.  That's right:  -4.  Sheesh - I don't care who their audience is, 
_any_ code calling `choose` with those arguments would be far better served if 
the function complained.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-06-02 Thread Raymond Hettinger


Raymond Hettinger  added the comment:

Here are a few other data points.  There is no consensus but either returning 0 
or erroring out seem reasonable.

MS Excel's COMBIN: If number < 0, number_chosen < 0, or number < number_chosen, 
COMBIN returns the #NUM! error value.

Scipy.misc.comb: If k > N, N < 0, or k < 0, then a 0 is returned.

Matlib.nchoose: Error: K must an integer between 0 and N.

Maple.numbcomb: Unclear from the docs what this does

Wolfram alpha:  Returns 0
https://www.wolframalpha.com/input/?i=10+choose+12

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-06-02 Thread Tim Peters


Tim Peters  added the comment:

I'm not convinced, although I agree relaxing k <= n is less damaging than 
relaxing k >= 0.

Python isn't aimed at mathematicians (although some 3rd-party packages 
certainly are, and they're free to define things however they like).  We have 
to trade off convenience for experts in edge cases against the likelihood that 
an ordinary user is making a mistake.

For example, that's why, despite that Python supports complex numbers, 
math.sqrt(-1) raises ValueError.  For _most_ users, trying that was probably an 
error in their logic that led to the argument being negative.  Experts can use 
cmath.sqrt() instead.

Ordinary users think comb(n, k) is the value of n!//(k!*(n-k)!), and as far as 
they're concerned factorial is only defined for non-negative integers.  0 <= k 
<= n follows from that.  (Indeed, the current docs for `comb()` _define_ the 
result via the factorial expression.)

And ordinary users think of the sum of the first n integers as "number of terms 
times the average", or memorize directly that the answer is n*(n+1)/2.  That 
works fine for n=0.  Only an expert thinks of it as `comb(n+1, 2)`.

So I would still rather enforce 0 <= k <= n at the start, and relax it later 
only if there's significant demand.  In going on three decades of using Python 
and having written my own `comb()` at least a dozen times, I've never found 
that constraint limiting, and enforcing it _has_ caught errors in my code.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-06-02 Thread Mark Dickinson


Mark Dickinson  added the comment:

> @David Ratcliffe

Radcliffe! Apologies for the misspelling. It's still early in this timezone and 
I haven't had any coffee yet.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-06-02 Thread Mark Dickinson


Mark Dickinson  added the comment:

@David Ratcliffe

> the sum of the first n positive integers is binomial(n+1, 2), and the formula 
> should still work if n is zero.

I agree that's a convincing use-case.

Can you think of any practical uses for allowing negative `k`? I can't.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-06-01 Thread David Radcliffe


David Radcliffe  added the comment:

I think that binomial(n, k) should return 0 when k > n or k < 0. This is a 
practical consideration. I'm concerned about evaluating sums involving binomial 
coefficients. Mathematicians are often rather loose about specifying the upper 
and lower bounds of summation, because the unwanted terms are zero anyway. 
These formulas should not result in value errors when they are implemented 
directly.

To give a simplistic example, the sum of the first n positive integers is 
binomial(n+1, 2), and the formula should still work if n is zero.

--
nosy: +David Radcliffe

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-06-01 Thread Raymond Hettinger


Change by Raymond Hettinger :


--
pull_requests: +13617
pull_request: https://github.com/python/cpython/pull/13734

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-06-01 Thread Tim Peters


Tim Peters  added the comment:

Strongly prefer requiring 0 <= k <= n at first.  This is a programming 
language:  it will be applied to real problems, not to abstract proofs where 
some slop can be helpful in reducing the number of cases that need to be 
considered.

The Twitter fellow is certainly right that "0" is the clear answer to "how many 
5-element subsets does have a 4-element set have?", but in the context of real 
problems it's more likely (to my eyes) that a programmer asking that question 
of `comb()` has gotten confused.  It certainly would have been "an error" in 
any programming use for `comb()` I've ever had.

Raymond, I'm not clear on what you mean by "alias".  You mean supplying the 
same function under more than one name?  I'd be -1 on that (where would it end? 
 the answer to every future name bikeshedding issue then would be "all of the 
above").  But +1 on, e.g., adding a doc index entry for "binomial" pointing to 
"comb".

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-06-01 Thread Mark Dickinson


Mark Dickinson  added the comment:

I'm particularly sceptical about real-world use-cases for k < 0. Maybe we 
should consider removing just the k > n requirement.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-06-01 Thread Raymond Hettinger


Raymond Hettinger  added the comment:

> I'd say leave it as-is for 3.8, see what the reaction is, 
> and maybe relax constraints in 3.9 if that seems appropriate.

I concur.  That said, the referenced tweet was a reaction :-)

FWIW, itertools.combinations(seq, r) returns 0 values when r > len(seq).

Tim, what do you think?

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-06-01 Thread Mark Dickinson


Mark Dickinson  added the comment:

> What are your thoughts?

Sigh. I don't object to extending to `k < 0` and `k > n`, but once we've made 
that extension it's impossible to undo if we decide we'd rather have had the 
error checking. I'd really like to see some convincing use-cases. Quotes from 
Concrete Mathematics (fine book though it is) don't amount to use-cases.

I'd say leave it as-is for 3.8, see what the reaction is, and maybe relax 
constraints in 3.9 if that seems appropriate. But if a majority of others 
really want to make the change now, that's okay with me.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-06-01 Thread Pablo Galindo Salgado


Pablo Galindo Salgado  added the comment:

Just a heads up from issue37125: The leak is was fixed by PR13725.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-06-01 Thread Raymond Hettinger


Raymond Hettinger  added the comment:

One other idea: it may be worth putting in an alias for "binomial" to improve 
findability and readability in contexts where a person really does what 
binomial coefficients and is not otherwise thinking about counting contexts.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-06-01 Thread Raymond Hettinger


Raymond Hettinger  added the comment:

Mark, please see:  https://twitter.com/daveinstpaul/status/1134919179361034240

What are your thoughts?

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-06-01 Thread Mark Dickinson


Mark Dickinson  added the comment:

I'd expect any math module function accepting integers to be sufficiently 
duck-typed to accept integer-like things (i.e., objects that implement 
`__index__`), in just the same way that the regular math module functions (sin, 
log, atan, sqrt, ...) accept arbitrary objects defining `__float__`. We already 
do this for gcd, factorial.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-06-01 Thread Serhiy Storchaka


Serhiy Storchaka  added the comment:

NumPy integer types are not subclasses of int.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-06-01 Thread Raymond Hettinger


Raymond Hettinger  added the comment:

Why do we want __index__ support?  This seems like an unnecessary extension 
without relevant use cases.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-06-01 Thread Serhiy Storchaka


Change by Serhiy Storchaka :


--
pull_requests: +13614
pull_request: https://github.com/python/cpython/pull/13731

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-06-01 Thread Serhiy Storchaka


Serhiy Storchaka  added the comment:


New changeset 2b843ac0ae745026ce39514573c5d075137bef65 by Serhiy Storchaka in 
branch 'master':
bpo-35431: Refactor math.comb() implementation. (GH-13725)
https://github.com/python/cpython/commit/2b843ac0ae745026ce39514573c5d075137bef65


--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-06-01 Thread Pablo Galindo Salgado


Pablo Galindo Salgado  added the comment:

math.comb is leaking, I opened https://bugs.python.org/issue37125 to track it.

--
nosy: +pablogsal

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-06-01 Thread Serhiy Storchaka


Serhiy Storchaka  added the comment:

PR 13725 is a technical fix/optimization/cleanup. Later we can apply 
algorithmic optimization.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-06-01 Thread Serhiy Storchaka


Change by Serhiy Storchaka :


--
pull_requests: +13609
pull_request: https://github.com/python/cpython/pull/13725

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-06-01 Thread Yash Aggarwal


Yash Aggarwal  added the comment:

Thanks @rhettinger for cleaning up the code and closing the pr. I didn't get 
what you meant by long, and sorry for not being much active as well. I am stuck 
with a pretty time consuming internship.

--
versions: +Python 3.6 -Python 3.8

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-06-01 Thread Mark Dickinson


Mark Dickinson  added the comment:

Thanks, Raymond. I'm planning to do a post-merge review and test this weekend.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-06-01 Thread Raymond Hettinger


Raymond Hettinger  added the comment:

Leaving this open for a while in case there is more work that needs to be done 
or any further comments to be resolved.

--
assignee:  -> rhettinger

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-06-01 Thread Raymond Hettinger


Raymond Hettinger  added the comment:


New changeset 4a686504eb2bbf69adf78077458508a7ba131667 by Raymond Hettinger 
(Yash Aggarwal) in branch 'master':
bpo-35431: Implemented math.comb (GH-11414)
https://github.com/python/cpython/commit/4a686504eb2bbf69adf78077458508a7ba131667


--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-05-12 Thread Yash Aggarwal


Yash Aggarwal  added the comment:

@mark.dickinson both pr's are more or less same. Keller was offline for some 
time so I made the new issue. They were merged later. Only difference is in 
unittests. I'd say it's up to you to decide which one to keep.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-05-12 Thread Mark Dickinson


Mark Dickinson  added the comment:

@kellerfuchs @FR4NKESTI3N

We seem to have two open PRs for this feature, both with some review comments. 
Which is the one true PR? Can we close the other one?

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-02-27 Thread Yash Aggarwal


Yash Aggarwal  added the comment:

@mark.dickinson
Ok, then I will work on comb for now then.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-02-24 Thread Mark Dickinson


Mark Dickinson  added the comment:

> Can I get a consensus on weather math.perm() is needed?

It's not, for the purposes of this issue. I think `math.perm` should be the 
subject of a separate issue and discussion, and a separate PR. That way it 
doesn't block completion of this issue.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-02-23 Thread Yash Aggarwal


Yash Aggarwal  added the comment:

Can I get a consensus on weather math.perm() is needed?

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-02-01 Thread kellerfuchs


kellerfuchs  added the comment:

> Given that people clarified they prefer comb(), and that people conspicuously 
> didn't comment on it being entirely-opaque to people who do not elready know 
> what it is, I guess there is indeed consensus.

commit afb3d36e82b8d690a410fa9dca8029a8fad42984
Author: The Fox in the Shell 
Date:   Fri Feb 1 15:42:11 2019 +0100

Rename math.combinations to math.comb

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-02-01 Thread kellerfuchs


kellerfuchs  added the comment:

@Steven
> > This involved a few changes, which seem to reflect the consensus here:
> > - raise ValueError if k>n ;
> > - rename the function to math.combinations.
> [...]
> > As far as I can tell from the discussions here, Steven and you stated a 
> > preference for the shortened names, and that's it.
> > There was also no reply to my comment about `comb` being confusing (due to 
> > the collision with an English word).
> >
> > Since there was, however, pretty clear agreement on calling it after 
> > combinations (shortened or not) rather than binomial(), I went with this.
>
> I see at least four people (myself, Raymond, Mark and Tim) giving comb 
as first choice, and I didn't see anyone give combinations as their 
first choice.
>
> I don't object to you taking it upon yourself to go with the longer name 
> (which is my second choice), but I do object to you claiming concensus 

I wasn't claiming consensus on the short-vs.-long name issue, but on the 
binomial-vs-combinations one.
I thought that would have been clear considering the context quoted above 
(which was missing from your reply)

Given that people clarified they prefer comb(), and that people conspicuously 
didn't comment on it being entirely-opaque to people who do not elready know 
what it is, I guess there is indeed consensus.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-01-31 Thread Jakub Wilk


Change by Jakub Wilk :


--
nosy: +jwilk

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-01-28 Thread Tim Peters


Tim Peters  added the comment:

> As far as I can tell from the discussions here, Steven
> and you stated a preference for the shortened names, and
> that's it.

Plus Mark, plus me - all backed "comb()" specifically.

> I find it hard to imagine anyone needing combinations without
> also needing permutations, and I didn't think it was necessary
< to explicitly say so.

Of course it is.  Merely saying something is possible is no reason at all to do 
it.  The original report didn't say anything about counting partial 
permutations, and so it's "feature creep" on the face of it to tack that on.

I personally have scant use for `perm()`, but have written my own `comb()` many 
times.  Far more often than I've written a `factorial()` and a `product()` 
combined, but I've written each of the latter more than twice, and a `perm()` 
not even once.  Especially if `prod()` (the topic of a different report) is 
added, the case for adding a `perm()` gets weaker (rising and falling 
factorials are special cases of what `prod()` does, and `perm()` is just an 
instance of falling factorial).

Which doesn't mean `perm()` must not be added ;-)  You're now the first to say 
it _would_ be useful, which is a start.  Can we get a second?

In any case, I don't want to _see_ this report get bogged down by feature 
creep:  `comb()` is what it was about from the start, and everyone so far has 
agreed `comb()` would be useful.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-01-28 Thread Yash Aggarwal


Yash Aggarwal  added the comment:

Agreed, comb sounds much better than combination. And using the name binomial 
would make it sound like something that would puke out whole binomial series 
rather than a single coefficient(maybe leave it for that in case is it decided 
to be useful in the future).

PR 11414 implements simple algorithm that performs slower than using a 
factorial definition for k>n/3. 
@kellerfuchs I'd prefer if we could work on this since it's conflict free and 
already reflects the behavior everyone agreed upon. 

Would it be better to create a separate issue for math.perm to discuss its 
behavior?


If the behavior of comb is satisfactory, can we start with optimizations?

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-01-28 Thread Raymond Hettinger


Raymond Hettinger  added the comment:

> But given the precedent set by itertools and math.factorial,
> perhaps you are right and we ought to stick to the longer name

I disagree.  Let's stick with comb() which is what SciPy uses.  It is tedious 
to write out combinations in a formula what includes other steps. Also, I 
really want to differentiate it from the existing combinations() in the 
itertools module (it is reasonably forseeable that the two will used together.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-01-28 Thread Steven D'Aprano


Steven D'Aprano  added the comment:

> This involved a few changes, which seem to reflect the consensus here:
> - raise ValueError if k>n ;
> - rename the function to math.combinations.

I see at least four people (myself, Raymond, Mark and Tim) giving comb 
as first choice, and I didn't see anyone give combinations as their 
first choice.

I don't object to you taking it upon yourself to go with the longer name 
(which is my second choice), but I do object to you claiming concensus 
for the change without evidence of such.

> There was also no reply to my comment about `comb` being confusing 
> (due to the collision with an English word).

To be honest, I didn't think that comment needed a reply.

Collisions between words in different knowledge domains are not unusual. 
I don't think people think that math.tan has anything to do with 
changing the colour of their skin, or math.sin is about being wicked. 
Due to their length, permutation, combination and factorial are 
frequently abbreviated to perm, comb, fact and anyone looking for those 
functions should recognise the abbreviations.

But given the precedent set by itertools and math.factorial, perhaps you 
are right and we ought to stick to the longer name.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-01-28 Thread Steven D'Aprano


Steven D'Aprano  added the comment:

Sorry for the late reply, I missed Tim's comment when it first came 
through.

> Please resist pointless feature creep. The original report was about 
> comb(n, k) for integer n and k with 0 <= k <= n and that's all.  
> Everyone who commented appeared to agree they'd find that useful.
> 
> But nobody has said [...] that they'd find perm(n, k) USEFUL.

I'm not going to argue for binomial coefficients with negative n, but I 
find it hard to imagine anyone needing combinations without also needing 
permutations, and I didn't think it was necessary to explicitly say so.

But since you insist, I'll say so: I would find it useful to have a 
function to compute the number of permutations of n taking k at a time.

My perspective may be biased from my experience with secondary school 
maths, where they are taught together, but providing one without the 
other strikes me as weird as providing tan without sin and cos.

There are other functions from combinatorics which I personally use, 
like derangements, but I know when I'm pushing my luck :-)

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-01-28 Thread Josh Rosenberg


Josh Rosenberg  added the comment:

Steven: I'm assuming Brett rearranged the title to put emphasis on the new 
function and to place it earlier in the title. Especially important if you're 
reading e-mails with the old subject on an e-mail client with limited subject 
preview lengths, you end up seeing something like:

"The math module should provide a function for computing..."

rather than the more useful:

"Add a function for computing binomial coefficients to t..."

--
nosy: +josh.r

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-01-28 Thread kellerfuchs


kellerfuchs  added the comment:

@Raymond Hettinger
> Let's name this comb() instead of binomial() please (as requested by me, 
> Mark, and Tim).

(Replying here to keep the discussion in a single place.)

As far as I can tell from the discussions here, Steven and you stated a 
preference for the shortened names, and that's it.
There was also no reply to my comment about `comb` being confusing (due to the 
collision with an English word).

Since there was, however, pretty clear agreement on calling it after 
combinations (shortened or not) rather than binomial(), I went with this.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-01-28 Thread kellerfuchs


kellerfuchs  added the comment:

So, I rebased Yash's and my branches, and merged them together.  The result is 
still in PR#11018.

This involved a few changes, which seem to reflect the consensus here:
- raise ValueError if k>n ;
- rename the function to math.combinations.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-01-28 Thread kellerfuchs


kellerfuchs  added the comment:

> Start with an initial patch that implements this simplest possible 
> implementation, accompanied by clean documentation and thorough testing.
>
> Once everyone has agreed on the API (i.e. calling it "comb()", how to handle 
> various input datatypes, and handling of corner cases), and the patch is 
> applied, only then turn to a second pass for optimizations

+1 from me on that.

@Yash: Thanks a bunch for starting on the implementation.  I will have a look 
shortly  :)

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-01-28 Thread kellerfuchs


kellerfuchs  added the comment:

Hi everyone,

Sorry for the lack of reply, I severely underestimated how exhausting the 
holiday season would be.
Catching up with the comments right now.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-01-04 Thread Yash Aggarwal


Yash Aggarwal  added the comment:

@tim.peters
Got it.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-01-04 Thread Tim Peters


Tim Peters  added the comment:

Please resist pointless feature creep.  The original report was about comb(n, 
k) for integer n and k with 0 <= k <= n and that's all.  Everyone who commented 
appeared to agree they'd find that useful.

But nobody has said they'd find generalizing beyond those constraints USEFUL, 
or that they'd find perm(n, k) USEFUL.  They just pointed out that such things 
are possible.

Every bit of new API implies eternal maintenance, porting, testing, doc, and 
conceptual costs.  So please stick to what's (at least nearly) universally 
agreed to be useful.  Complications can be added later if there turns out to be 
real demand for them.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-01-04 Thread Yash Aggarwal


Yash Aggarwal  added the comment:

@steven.daprano 
> Are you also providing a perm(n, k) function?
I didn't know it is also being implemented. Should I start on that too?

My implementation is based on these requirements:

> - Spell it comb(n, k).
> - TypeError if args aren't ints.
> - ValueError if not 0 <= k <= n.

Should the bincoeff function be same with exception of allowing negative n?

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-01-03 Thread Steven D'Aprano


Steven D'Aprano  added the comment:

> return (-1)**k * bincoeff(n+k+1, k)

Oops, that's meant to be n+k-1.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-01-03 Thread Steven D'Aprano


Steven D'Aprano  added the comment:

> should the function be expanded to calculate for negative 
> n or is the function expected to work only in combination sense?

If this were my design, I would offer both but in separate functions:

def comb(n, k):
if n < 0: raise ValueError
return bincoeff(n, k)

def bincoeff(n, k):
if n < 0:
return (-1)**k * bincoeff(n+k+1, k)
else:
# implementation here...

I believe we agreed earlier that supporting non-integers was not 
necessary.

Are you also providing a perm(n, k) function?

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-01-03 Thread Yash Aggarwal


Yash Aggarwal  added the comment:

I have written the function in the latest patch to work only for positive n. 
Although the definition of combination or nChoosek makes no sense for negative 
n, negative binomial distribution exists and so binomial coefficient is defined 
for negative value of n. So my question is should the function be expanded to 
calculate for negative n or is the function expected to work only in 
combination sense?

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-01-02 Thread Yash Aggarwal


Change by Yash Aggarwal :


--
pull_requests: +10812

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2019-01-01 Thread Yash Aggarwal


Yash Aggarwal  added the comment:

Thanks @mark.dickinson. As @rhettinger suggested, I'll write a basic function 
that uses division and works in O(k) for now. It's holiday season but hopefully 
@kellerfuchs will respond by then, and in the meantime I'll write more tests 
other than pascal's identity and corner cases.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2018-12-31 Thread Raymond Hettinger


Raymond Hettinger  added the comment:

Kellar and Yash, my suggestion is to separate the work into two phases.

Start with an initial patch that implements this simplest possible 
implementation, accompanied by clean documentation and thorough testing.

Once everyone has agreed on the API (i.e. calling it "comb()", how to handle 
various input datatypes, and handling of corner cases), and the patch is 
applied, only then turn to a second pass for optimizations (special casing 
various types, minimizing how big intermediate values can get by doing early 
cancellation, exploiting even/odd patterns etc).

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2018-12-31 Thread Mark Dickinson


Mark Dickinson  added the comment:

> Can I work on C implementation if no-one else is doing it right now?

Sounds fine to me. You might want to coordinate with @kellerfuchs to see what 
the status of their PR is; maybe the two of you can collaborate?

@kellerfuchs: are you still planning to work on this?

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2018-12-31 Thread Yash Aggarwal


Yash Aggarwal  added the comment:

Can I work on C implementation if no-one else is doing it right now?

--
nosy: +FR4NKESTI3N

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2018-12-14 Thread Tim Peters


Tim Peters  added the comment:

Just for fun, here's a gonzo implementation (without arg-checking) using ideas 
from the sketch.  All factors of 2 are shifted out first, and all divisions are 
done before any multiplies.

For large arguments, this can run much faster than a dumb loop.  For example, 
combp(10**100, 400) takes about a quarter the time of a dumb-loop 
divide-each-time-thru implementation.

# Return number of trailing zeroes in `n`.
def tzc(n):
result = 0
if n:
mask = 1
while n & mask == 0:
result += 1
mask <<= 1
return result

# Return exponent of prime `p` in prime factorization of
# factorial(k).
def facexp(k, p):
result = 0
k //= p
while k:
result += k
k //= p
return result

def combp(n, k):
from heapq import heappop, heapify, heapreplace

if n-k < k:
k = n-k
if k == 0:
return 1
if k == 1:
return n
firstnum = n - k + 1
nums = list(range(firstnum, n+1))
assert len(nums) == k

# Shift all factors of 2 out of numerators.
shift2 = 0
for i in range(firstnum & 1, k, 2):
val = nums[i]
c = tzc(val)
assert c
nums[i] = val >> c
shift2 += c
shift2 -= facexp(k, 2) # cancel all 2's in factorial(k)
assert shift2 >= 0

# Any prime generator is fine here.  `k` can't be
# huge, and we only want the primes through `k`.
pgen = psieve()
p = next(pgen)
assert p == 2

for p in pgen:
if p > k:
break
pcount = facexp(k, p)
assert pcount
# Divide that many p's out of numerators.
i = firstnum % p
if i:
i = p - i
for i in range(i, k, p):
val, r = divmod(nums[i], p)
assert r == 0
pcount -= 1
while pcount:
val2, r = divmod(val, p)
if r:
break
else:
val = val2
pcount -= 1
nums[i] = val
if pcount == 0:
break
assert pcount == 0

heapify(nums)
while len(nums) > 1:
a = heappop(nums)
heapreplace(nums, a * nums[0])
return nums[0] << shift2

I'm NOT suggesting to adopt this.  Just for history in the unlikely case 
there's worldwide demand for faster `comb` of silly arguments ;-)

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2018-12-13 Thread Mark Dickinson


Mark Dickinson  added the comment:

[Tim]

> My two cents:
> 
> - Spell it comb(n, k).
> - TypeError if args aren't ints.
> - ValueError if not 0 <= k <= n.

+1 to all of this.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2018-12-12 Thread Tim Peters


Tim Peters  added the comment:

My two cents:

- Spell it comb(n, k).
- TypeError if args aren't ints.
- ValueError if not 0 <= k <= n.

Not concerned about speed.  It's possible to do this with no divisions 
involving integers bigger than `n` and `k`(*), using O(k) space, but for 
"practical" arguments I bet that's slower than the dumbest possible loop.

(*) Sketch:  make lists of the k numerators and k-1 denominators (skip 1).  For 
each prime P <= k, a modulus operation can determine the index of the first 
multiple of P in each list.  For that, and for each P'th later list element, 
divide out the multiples of P, adding 1 to a running count for numerators, 
subtracting 1 for denominators, and reducing each list element by the Ps 
divided out.  Then if the final P count isn't 0 (it will always be >= 0), 
append pow(P, count) to a list of factors.  pow() is efficient.

After that, all the denominators will be reduced to 1, so can be ignored 
thereafter.  It just remains to multiply all the reduced numerators and 
prime-power factors.

Catenate them all in a single list, heapify it (min heap), and so long as at 
least 2 factors remain pop the two smallest and push their product.  This 
attempts to balance bit lengths of incoming factors, allowing 
close-to-best-case-for-it Karatsuba multiplication to kick in.

But that's nuts ;-)  To get even nutsier, special-case P=2 to use shifts 
instead, skip adding pow(2, count) to the list of factors, and just shift left 
by the count at the end.

That said, even the "dumbest possible loop" may benefit in C by shifting out 
all trailing zeroes, multiplying/dividing only odd integers, and shifting left 
at the end.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2018-12-08 Thread Raymond Hettinger


Raymond Hettinger  added the comment:

> For what its worth, the colour I prefer for this bikeshed are 
> "comb" and "perm", which are the names used by the 
> HP 48GX calculator. Second choice would be to spell the names 
> out in full, "combinations" and "permutations".

+1 These would be my preferences as well :-)

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2018-12-07 Thread Steven D'Aprano


Steven D'Aprano  added the comment:

Brett, what was the purpose of the title change?

> title: The math module should provide a function for computing 
> binomial coefficients -> Add a function for computing binomial 
> coefficients to the math module

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35431] Add a function for computing binomial coefficients to the math module

2018-12-07 Thread Brett Cannon


Change by Brett Cannon :


--
title: The math module should provide a function for computing binomial 
coefficients -> Add a function for computing binomial coefficients to the math 
module

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com