Skip to content
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

Add workaround for scipy.special.factorial2 changes #301

Merged
merged 2 commits into from
Dec 11, 2023
Merged

Conversation

FarnazH
Copy link
Member

@FarnazH FarnazH commented Nov 18, 2023

This PR adds a factorial2 function that wraps scipy.special.factorial2 to return 1.0 when the input is not positive. This is a temporary addition, while we wait for Scipy's update. To learn more, see scipy/scipy#18409. This is a better solution than limiting Scipy version that works with IOData, as one might need to use scipy 1.11.x in their environment for other purposes. A similar change was made in other QC-Devs packages; see theochem/gbasis#138.

Copy link

codecov bot commented Nov 18, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (ca6113b) 95.00% compared to head (feecb0b) 94.98%.

❗ Current head feecb0b differs from pull request most recent head b3b7377. Consider uploading reports for the commit b3b7377 to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #301      +/-   ##
==========================================
- Coverage   95.00%   94.98%   -0.02%     
==========================================
  Files          74       74              
  Lines        8844     8819      -25     
  Branches     1216     1216              
==========================================
- Hits         8402     8377      -25     
  Misses        193      193              
  Partials      249      249              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@PaulWAyers PaulWAyers left a comment

Choose a reason for hiding this comment

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

Shouldn't we return 1 for factorial2(-1) and 0 for factorial2(x) where x = -2,-3,....? I think that is the "old" scipy behavior.

Should we check that n is an integer?

Addiiton: Looking at wikipedia and Wolfram Mathworld, double-factorials for negative numbers are a mess, though everyone agrees they are undefined for negative even integers. It would be good to return NotImplementedError or something like that except for negative odd integers, $n$, which can be evaluated as:

$$ n!! = \frac{(-1)^{\frac{|n|-1}{2}}|n|}{|n|!!} = \frac{(-1)^{\frac{|n|-1}{2}}}{|n-2|!!} $$

The advantage of the first formula is that it also applies for $n=-1$, while the second formula, although simpler, is only useful starting at $n=-3$. I don't know that $n=-3,-5,\ldots$ ever appears, but we should probably return NotImplementedError or otherwise a sensible value.

@FarnazH
Copy link
Member Author

FarnazH commented Dec 11, 2023

@PaulWAyers, thanks for your feedback.

  • We don't need to check that n is an integer, because this is just a wrapper function, and scipy.special.factorial2 does that check and prints a proper error message: ValueError: factorial2 does not support non-integral scalar arguments.
  • Following your suggestion, I updated the function to return 0.0 for n <= -2; see b3b7377. As scipy does not treat odd and even negative integers differently, I left it intact.

@FarnazH FarnazH merged commit 6b9c6e0 into master Dec 11, 2023
2 of 5 checks passed
@Ali-Tehrani Ali-Tehrani mentioned this pull request Feb 8, 2024
@tovrstra tovrstra deleted the fix_factorial2 branch June 5, 2024 09:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants