-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
ENH:Rewrite specfun F77 code in C #19824
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
Conversation
If I am not mistaken, which is extremely easy with this file, these are all the public functions we use. I tested half of them by Wolfram Alpha by reading what it does from the docstring, but some I don't know much about the subject and probably we'll rely on the existing tests. |
6b68631
to
2b178be
Compare
All tests are now passing on my local machine. However, of course because C, occasionally I'm getting a windows access violation crash. I don't know which one is the offending one but I'll keep digging. If anybody knows how to pinpoint the actual test that originates the violation that would be awesome (maybe a tool I don't know yet exists in Linux or Mac?). Other than that, I tested most of the if branches I see in the functions and when succeeds it passes all so let's see what CI would say. Every commit is one specific file hence we can keep the commit structure just in case. |
Nevermind CI actually caught them correctly. I'll fix those. |
426b230
to
705672d
Compare
I don't understand why |
Is it possible the CI for #8404 last ran without properly merging main, and thus didn’t have those tests? |
I've confirmed locally that that the test fails with I think I understand what's going on, and why the tolerance was chosen at the level it was. In the following, I will refer to the current estimate in a root finding iterative process as For root finding algorithms with better than linear convergence, This means that the value being compared against tol at each iteration is not an estimate of the current error, it is the sum of the current error and the previous error, which for fast enough convergence will be an estimate of the previous error. The rate of convergence of algorithm R used in cdflib is bounded below by 1.6202 (but could be faster depending on properties of the function). This made me think that 10**(np.log10(np.finfo(float).eps)/1.6202) = 2.1802262222980188e-10 might be a better tolerance. This takes into account the rate of convergence to estimate what I've found the test still fails in that case. I think because 1.6202 is only a lower bound on the rate of convergence, thus it's still possible to make additional steps after optimal convergence has been reached, causing accumulation of rounding errors to swamp the benefit of additional iterations. |
I checked and the only wrapper missing in |
FWIW, I just did a coverage run on the main branch via
|
If you have known values for the test, I can add it right away. |
Interesting, scipy/scipy/special/tests/test_mpmath.py Lines 1845 to 1855 in 4edfcaa
|
Ok, if I haven't made any mistakes, (
np.array([2.976319645712036, 1.358840996329579, 0.5501016716383508]),
np.array([3.105638472238475, 0.9380581512176672, 0.533688488872053]),
) we should also fix |
Thanks @dschmitz89, I'll come up with test cases for those others too. |
I've just seen that scipy/scipy/special/tests/test_mpmath.py Lines 1765 to 1768 in 6103deb
|
For |
[skip cirrus]
@steppi I first repeated all your comments but the Thank you very much for your thorough review. |
What input is causing the segfault? And happy to review. It's awesome to see this getting done. |
The two tests |
Thanks. Also this is really confusing: scipy/scipy/special/tests/test_basic.py Line 39 in 6103deb
scipy/scipy/special/tests/test_basic.py Lines 780 to 781 in 6103deb
A holdover from when scipy.special was just cephes in the very earliest days. |
scipy/special/specfun.c
Outdated
for (k = n2 - 1; k >= 0; k--) { | ||
bk[k - 1] -= w[k - 1] * bk[k]; | ||
} |
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.
Here's what's causing the segfault. Writing to bk[-1]
, and then you see the segfault when trying to free bk
because the metadata about the allocated block got corrupted.
scipy/scipy/special/specfun/specfun.f
Lines 576 to 577 in 6103deb
DO 70 K=N2-1,1,-1 | |
70 BK(K)=BK(K)-W(K)*BK(K+1) |
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.
Another facepalm moment.
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.
I missed it too and I spent tens of hours pouring over this code. On the whole, I think you’ve done a great job.
[skip cirrus] [skip circle]
@steppi if we are happy with its state should we merge and do a complete sweep on testing including cdflib ones too on a separate PR? I'm a bit worried that there is too much happening in this one and we probably need a bit more focus on tests alone as @dschmitz89 and @inkydragon noticed already. |
Yeah, I think this is in good shape. I'm going to go everything quickly one more time and plan to merge today if I find no further issues. |
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.
I forgot to officially approve before merging, but needless to say I approve. This is great work, and I think we've ironed out the issues which didn't already exist in the original code.
Thanks all for the input above and beyond! Very happy that this is finally happening. Let's finish it off by completing the testing suite. |
Reference issue
Part of #18566 . As mentioned already by @steppi, this was indeed not very hard as I predicted earlier. Functions are standalone and much easier to test.
Closes #17681
Closes #18423
Closes #19813
What does this implement/fix?
This is the third and final part of rewriting Fortran files in
scipy.special
in C followingcdflib
andamos
libs.Additional information
gamma
,loggamma
,gamln
,complexgamma
implementations in everyspecial
vendored library. We should do something about these functions; merging their precision domains might be a powerful and conclusive functionality.y1p_zeros
doc example to have only the relevant data because the signs of the zeros were flipping randomly.