-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Overhaul factorial{,2,k}
: API coherence, bug fixes & consistent array-support
#15841
Conversation
dfcc136
to
abfd462
Compare
4cea1e4
to
2c055e8
Compare
@tupui @ilayn @tirthasheshpatel @j-bowhay This should be ready for a first look. I realize it's pretty big, and I'll be happy to cut it up into more digestible pieces (though as the OP notes, I've tried to make the individual commits make sense on their own). I just wanted to ask for some confirmation about the direction (slight behaviour changes, array-extensions, testing approach) before doing that. I think cleaning up the current behavioural mess should be the first goal (TBD if we want to explicitly deprecate certain things, or if they're inconsequential enough to just change like 0-dim arrays to scalars), and further unifications/extensions should come on top of that. |
Regarding test coverage, this adds about 500 tests (though highly parametrized) - compared to the CI for 9ce6bb0:
we now have
They're all pretty-to-very fast though. 🙃 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Had a brief look to the parts that I could follow.
A friendly ping @h-vetinari |
2c055e8
to
51a62d2
Compare
Thanks. Just a rebase so far, still needs some work on some open review comments (I had still been hoping to get some feedback about the double factorial for even integers, but since |
@ilayn @j-bowhay The commit history is a complete mess (sorry), but before I spend a bunch of time cleaning it up, I wanted to ask:
I've rebased the PR (no conflicts), new commits since the last discussions start from 45a3177 (but I was thrashing around a fair bit, so not sure if single-commit reviewing is worth your time on this). |
I am fine with this, better to have a solid implementation first before worrying about extensions.
I'll try to have a look over this in the coming weeks
I'm happy with what you're proposing |
Can you post the benchmark results vs main? |
Unfortunately no. If you can check out this PR and run the benchmark, that would be nice though. |
I am getting quite a significant slow down in certain cases |
Thanks for testing. Even though we're testing very small arrays (where the checks' constant impact is mitigated the least by the size of the array), I'm surprised by the magnitude. Looking at the added checks, I don't see something that would explain this, but perhaps It's was also an API design decision to return all-nan arrays of any dtype unchanged, but that does add the overhead of having to check whether a given array is actually all-nan. Given the slow-downs, I think I'll remove this bit again and just throw if the dtype is not what it should be. I'll have a look at other places where we might be leaving some performance on the table. |
Could you do another run perhaps? For factorialk, I'm quite willing to eat the performance drop though - it currently has 0 checks and corner-case handling, and adding that is worth some overhead. Also, the |
That change looks like it improved performance quite a bit |
Also avoids math.factorial for these; which errors in 3.10+. Fixes scipyGH-13122
No functional changes, only added the docstring.
Also fix NaN-handling in factorial{2,k}.
For factorial2, the array-path for exact=False existed already.
No functional changes.
Remove benchmarks for floats with exact=True (not supported anymore) and extend the tested range with some larger inputs.
also improve errors for complex inputs in special.factorial
OK, it's time :) After chasing green CI for ~3 weeks, the only blemish is now #18407, but that's manageable. |
This is some of the work towards #15600. It's still missing several things (mainly
extend='complex'
), but it's very big as-is, so I wanted to post this as just a something to further the discussion.Currently it contains several small behaviour changes:
factorial2
/factorialk
return 0 for small negative integersfactorial
/factorial2
consistently return the same type as the input. This changesfactorial2
to not return 0-dim arrays for scalar inputs, and - inversely - changesfactorial
to not turn 0-dim arrays into scalars.factorial
return type for 0-d inputs #10954 was treated as a bugfix as well...).factorialk
toobject
fork >= 10
(I think this is a reasonable trade-off, for more details see 99d088a)Switch the float approximation fordeferred due to ambiguity of the extension.factorial2
to the one from ENH: extend factorial2 to complex domain #15349In addition, it adds array-support for factorialk, exact=True array-support for factorial2 (and many related fixes), and also fixes #13122; Finally, it cleans up and parametrizes a lot of tests, and adds a whole bunch of new ones.
Note: the individual commits should be much easier to review than the entire diff at once.