Enforce int types in math.factorial#1550
Merged
quantshah merged 1 commit intoqutip:masterfrom May 11, 2021
Merged
Conversation
Using integer-like floats in math.factorial is deprecated as of Python 3.9. The arbitrary precision nature of Python ints is still desirable here.
quantshah
approved these changes
May 11, 2021
Member
|
I think we do not have very large numbers as output and the log degeneracy formula should work for us. Thanks for the neat suggestion and the benchmark. Let me see how much improvement we get in the construction with this faster degeneracy computation. I am going to merge this and discuss the change in a new PR. |
Member
Author
|
I think you won't get a huge speed up unless you're dealing with pretty large numbers - if I remember correctly from looking through the Cython backing as part of the |
quantshah
pushed a commit
to quantshah/qutip
that referenced
this pull request
Jun 2, 2021
Using integer-like floats in math.factorial is deprecated as of Python 3.9. The arbitrary precision nature of Python ints is still desirable here.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Using integer-like floats in
math.factorialis deprecated as of Python 3.9.Glancing over the rest of the code, I'm fairly sure
math.factorialis only called on floats formed by (e.g.)N / 2 + 0.5, which is guaranteed be an integer for all oddNintegers, but to be safe I inserted the same test thatmath.factorialwill do as well.By the way: depending on how accurate you actually want/need to be with your degeneracy calculations, a common way to deal with these binomial quantities is to work in the logarithmic space --
This is pretty much guaranteed to be faster, but a little less precise; double-precision floats have ~15 decimal digits of precision compared to the
Decimaldefault of 28. You have to be careful that thenp.expcall doesn't overflow (unless you can use the number in logarithmic space as well), but you can just use a singleDecimalinstance like I did if it really matters to you to have huuuuge numbers output. I suspect it doesn't, since you multiply it by a float right after, which will overflow toinf.Timings: