-
-
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
ENH: implement the principle branch of the logarithm of Gamma. #5931
Conversation
Edit: forgot to rebuild before testing. |
The failures are real, and they are caused by this PR. The mechanism is a bit unclear, but I believe it's because you are including "complex.h" which introduces C99 complex numbers --- it should use numpy complex number functions instead, see _complexstuff.pxd. (Whether the failing spherical bessel functions are completely stably implemented if they fail with C99 complex is then a different question.) A different question is that |
Also cleaned up the code and improved the documentation.
Yes, including "complex.h" was the culprit; thanks for the hint. It's true that one could compute |
@@ master #5931 diff @@
======================================
Files 238 238
Stmts 43744 43805 +61
Branches 8213 8213
Methods 0 0
======================================
+ Hit 34168 34230 +62
- Partial 2602 2604 +2
+ Missed 6974 6971 -3
|
Ok good. Then, perhaps we can just replace the complex version of |
Right. Though maybe I'll make a proposal, and if you don't like it I'll merge things into Currently This behavior was only documented in 0.18 (before that the documentation said that |
As a data point, in a similar situation with the legendre P function |
Now gammaln always computes the log of the absolute value of gamma, so that one should use gammaln and gammasgn for working with real values an loggamma for working with complex values.
Updated in accordance with @pv's comment. I stripped out the complex part of |
Perhaps gammaln should raise a deprecationwarning for complex inputs, rather than scrapping it right away. iow, "gammaln -> _gammaln" and add a Python wrapper function |
Also changed several incorrect uses of "principle" to "principal".
Updated. |
Added deprecation warning to gammaln docstring and also fixed erronious claim that ``loggamma`` is defined by log(gamma(z)) for Re(z) > 0 and extended to the complex plain by continuation to the correct claim that it is defined by log(gamma(x)) for x > 0 and extended to the complex plain by continuation.
Updated again to explicitly mention the deprecation in the documentation of gammaln. Also fixed a bug I introduced in the Also, @pv is correct that the current behavior of A point worth noting is that for complex values |
loggamma has zeros at 1 and 2 which were causing a loss in accuracy, so used Taylor series about those points. Also increased the number of terms used in the Laurent series to improve accuracy near 0. Added tests to make sure there aren't jumps in accuracy when switching from the Taylor/Laurent series regime to the recurrence relation regime.
Updated yet again to improve accuracy around the pole at 0 and the zeros at 1 and 2. |
Also corrected some terminology in the comments for the series expansion for loggamma around 0.
Expanded the range of the Taylor series around 1 to a disk of radius 1/2 by using an explicit expression in terms of the zeta function. Started computing the function around 0 and 2 by using the recurrence relation to shift to 1 and then using the Taylor series. This increases accuracy and simplifies the code. There are now no known regions in the complex plane where the relative error is greater than 1e-13.
Added extra test coverage of the problem region and am updating to make Travis run again to figure out what's going wrong.
The added tests show that the accuracy around z = 1 is fine, which makes it likely that the Taylor series is fine. The accuracy has problems around z = 2. Results in that region are computed by the formula log(z - 1) + loggamma(z - 1). The loggamma term is computed by the Taylor series, so differences in accuracy for log around z = 1 on different systems are the prime suspects. Add an expansion of log around 1 to test this hypothesis.
Updated with more improvements to accuracy in the
I ran into a bug which (AFAICT) turned out to be caused by differences in accuracy of |
ENH: implement the principal branch of the logarithm of Gamma Add a new function `loggamma` for computing log Gamma on the complex plane using a consistent branch. Deprecate complex-valued inputs from `gammaln` --- the definition is inconsistent with the output from the real-valued function, and moreover the implementation of the complex-valued function has some bugs (accuracy issues, unnecessary branch cuts).
thanks, merged |
On the npy_clog issue --- may be a bug in the system libm, or in the numpy implementation of clog (depending of which one is used). So maybe report to numpy? |
Closes gh-5840.