Skip to content

bpo-35431: Add a function computing binomial numbers to the math module#11018

Closed
KellerFuchs wants to merge 20 commits intopython:masterfrom
KellerFuchs:math/binomial
Closed

bpo-35431: Add a function computing binomial numbers to the math module#11018
KellerFuchs wants to merge 20 commits intopython:masterfrom
KellerFuchs:math/binomial

Conversation

@KellerFuchs
Copy link
Copy Markdown

@KellerFuchs KellerFuchs commented Dec 7, 2018

  • Add tests specifying the behaviour of math.binomial(k, n):
    • when 0 <= k <= n;
    • when k > n;
    • when k or n are negative or non-integral;
    • when k or n are neither int nor float.
  • Implement math.binomial in C; done by @FR4NKESTI3N
  • Profile/benchmark and optimize if needed.
    @rhettinger suggested optimizing this, if needed, in a later PR.

https://bugs.python.org/issue35431

@the-knights-who-say-ni
Copy link
Copy Markdown

Hello, and thanks for your contribution!

I'm a bot set up to make sure that the project can legally accept your contribution by verifying you have signed the PSF contributor agreement (CLA).

Our records indicate we have not received your CLA. For legal reasons we need you to sign this before we can look at your contribution. Please follow the steps outlined in the CPython devguide to rectify this issue.

If you have recently signed the CLA, please wait at least one business day
before our records are updated.

You can check yourself to see if the CLA has been received.

Thanks again for your contribution, we look forward to reviewing it!

@KellerFuchs
Copy link
Copy Markdown
Author

Note that this is a work in progress; currently, it only contains tests specifying the behaviour of the function, so we can more easily discuss what's the desired behaviour.

Comment thread Lib/test/test_math.py Outdated
@rhettinger
Copy link
Copy Markdown
Contributor

rhettinger commented Dec 23, 2018

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

KellerFuchs added a commit to KellerFuchs/cpython that referenced this pull request Jan 28, 2019
@FR4NKESTI3N
Copy link
Copy Markdown
Contributor

Can we work on PR #11414 since it already passes all tests? Added you as a collaborator.

Comment thread Modules/mathmodule.c Outdated
}
else if (overflow == 1) {
PyErr_Format(PyExc_OverflowError,
"(n - k) and k must not exceed %lld",
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It has to be minimum of the two since the algorithm uses symmetry to reduce no of iterations

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, right.

@FR4NKESTI3N
Copy link
Copy Markdown
Contributor

Also, Is it okay to have multiple methods for tests of a single function? All other function in math have their tests encased in a single method which seems easier to maintain.

@KellerFuchs
Copy link
Copy Markdown
Author

Is it okay to have multiple methods for tests of a single function?

I would assume as much: I modelled those tests after the ones for factorial, which follow exactly that model (single function per property, with an informative name)

@KellerFuchs
Copy link
Copy Markdown
Author

KellerFuchs commented Feb 1, 2019

@FR4NKESTI3N The main reasons tests are failing is that the I had to reconcile the testsuite in one branch with the implemented behavior in the other, which would have to be done regardless of which branch we are pushing with.
It's now done (I had initially missed one test where k>n), and tests now pass.

I pushed to this PR rather than #11414 because this was the one I had write access to :)

@KellerFuchs
Copy link
Copy Markdown
Author

@csabella, @pablogsal, @scoder: Taking the liberty to ping you, since you commented on #11414

This is the PR and testsuite I started during the discussion on bugs.python.org, and I merged in @FR4NKESTI3N's C implementation that you already reviewed. The principal differences are:

  • minor improvements to the exception messages;
  • the testsuite for math.comb covers some extra edge cases, and is split in independent functions (as for math.factorial), which are documented by their docstring.

@FR4NKESTI3N
Copy link
Copy Markdown
Contributor

#11414 was already conflict free and had function name as comb, you needlessly worked by naming and renaming etc. The changes that you made in last 7-8 commits were already there. You were already invited as collaborator.
The test method names still need to be changed to testComb. The factorial test suite has 3 functions and is pretty old, and one of those methods needed a decorator. 6+ methods are bound to cause maintenance problems in the future.
Some exception tests are still missing and "combinations" isstill written everywhere except tests. Renaming and writing them all will be useless effort as all that is already done.
I would still insist you to work on that PR or merge that branch here again. Or maybe make the changes in that branch and then merge it with this. There were just some changes in 2 or 3 comments so it will be much more simpler to work on that branch.

@KellerFuchs
Copy link
Copy Markdown
Author

KellerFuchs commented Feb 1, 2019

@FR4NKESTI3N

#11414 was already conflict free and had function name as comb, you needlessly worked by naming and renaming etc. The changes that you made in last 7-8 commits were already there. You were already invited as collaborator.

Regarding “the last 7-8 commits” you talk of, I have no idea what you are going off, given that all I added since the initial merge 4 days ago (before you commented on this PR or added me as a collaborator) were commits afb3d36 (trivial changes, fixes the testsuite) and e4942bf (trivial rename).

Not sure either what you mean by “conflict-free” since there's no conflict between this and master (nor are there ever been).

On the other hand, recreating all that work on your PR would indeed be unnecessary work.

The test method names still need to be changed to testComb

Not sure that's entirely necessary, but I just did that to avoid having an argument, since it's a quick search-and-replace.

The factorial test suite has 3 functions and is pretty old, and one of those methods needed a decorator. 6+ methods are bound to cause maintenance problems in the future.

I am absolutely counfunded why you would expect a single function, testing a bunch of unrelated properties, to be more maintainable than separate tests.

I assure you, cramming everything into a single test does not make for maintainable testsuites.

Some exception tests are still missing

Which ones? I am pretty sure I kept them all when merging both testsuites.

"combinations" isstill written everywhere except tests. Renaming and writing them all will be useless effort as all that is already done.

Clearly not:

$ grep -i combinations Modules/mathmodule.c Lib/test/test_math.py
[Empty output, no match found]

I would still insist you to work on that PR or merge that branch here again. Or maybe make the changes in that branch and then merge it with this.

All the things you mentionned existed all along (with the exception of a single search-and-replace for test names), so I'm not sure why you want me to recreate all that on the other PR.

There were just some changes in 2 or 3 comments so it will be much more simpler to work on that branch.

There were changes to the testsuite, which was the whole point of that merge in the first place.

@FR4NKESTI3N
Copy link
Copy Markdown
Contributor

FR4NKESTI3N commented Feb 2, 2019

Hmm yeah, I just wanted to say said that it was wasteful. I think I was looking at an earlier commit, so I though that re-merging would be better.
Anyway fixed the name in other files.
Still not sure about unittests though but I am no expert.
Should we start on optimizations?
And what about math.perm()

@KellerFuchs
Copy link
Copy Markdown
Author

@FR4NKESTI3N I think math.perm ought to be a separate PR.
Regarding optimisations, like @rhettinger I'd rather get this reviewed/merged before we submit optimized code, but it doesn't mean we can't already do the legwork while this goes through the review.

Also thanks for noticing I had missed the documentation page :)

@KellerFuchs
Copy link
Copy Markdown
Author

PS: Just pushed a commit documenting under which circumstances math.comb may raise OverflowError.

@FR4NKESTI3N
Copy link
Copy Markdown
Contributor

FR4NKESTI3N commented Feb 4, 2019

Having LLONG_MAX doesn't seem right in documentation, maybe we can use something along the lines of "size of long long provided by compiler". This detail is purely implementation based anyway, so I don't know if it should even be there in documentation.

Edit: its minimum of the two btw

@KellerFuchs
Copy link
Copy Markdown
Author

@FR4NKESTI3N Good point; we might just mention there is a size beyond which math.comb is guaranteed to raise OverflowError.

The documentation for the math module mentions that things may raise OverflowError, but that's in the context of float overflow, which is a quite different beast altogether.

Comment thread Doc/library/math.rst Outdated
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
are too large (beyond `LLONG_MAX`).
are too large; the max. value is implementation- and platform-dependent.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@FR4NKESTI3N Does that seem like reasonable wording?

@FR4NKESTI3N
Copy link
Copy Markdown
Contributor

@KellerFuchs R.Hettinger reviewed the other PR so I have made the changes there. There was some change in implementation so I haven't written anything in docs about OverflowError for until Hettinger decides on implementation.

@brettcannon
Copy link
Copy Markdown
Member

Please note that the CLA has still not been signed. Without it we cannot accept this PR.

@KellerFuchs
Copy link
Copy Markdown
Author

@brettcannon Oh, thanks for the reminder, looks like the CLA signing thing didn't go through properly the first time around (I had some issues with DocuSign); I just redid the thing, so it should be good tomorrow.

@KellerFuchs
Copy link
Copy Markdown
Author

KellerFuchs commented Apr 7, 2019

PS: Also rebased the branch to deal with the merge conflict.

Comment thread Doc/library/math.rst
Return the binomial coefficient indexed by the pair of integers n >= k >= 0.

It is the coefficient of kth term in polynomial expansion of the expression
(1 + x)^n. It is also known as the number of ways to choose an unordered
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other parts of this file use double asterisks (**) for exponentiation, which is less likely to be confused with other meanings such as the XOR operator.

@rhettinger
Copy link
Copy Markdown
Contributor

Closing in favor of PR 11414. Any remaining ideas from this PR can be brought up in a separate PR.

Thanks for the parallel efforts.

@rhettinger rhettinger closed this Jun 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review tests Tests in the Lib/test dir

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants