Closed banana-bred closed 1 year ago
@banana-bred Thank you for reporting this bug. Could it be a special case when z%re
is equal to 0 (I never use complex values)?
Because gamma(x)
and exp(log_gamma( x))
report (almost) equal values for cmplx(0.25, 0.25)
, cmplx(-0.25, 0.25)
, cmplx(0.25, -0.25)
.
I think so. The cases you mentioned seem to be handled appropriately, but z%re = 0 currently gets treated the same as z%re < 0, which is probably (?) not intended and is what seems to cause this issue.
Ok. Thank you for the clarification. Would you like to submit a PR with the appropriate fix and test, please?
Le lun. 14 août 2023 à 22:16, banana-bred @.***> a écrit :
I think so. The cases you mentioned seem to be handled appropriately, but z%re = 0 currently gets treated the same as z%re < 0, which is probably (?) not intended and is what seems to cause this issue.
— Reply to this email directly, view it on GitHub https://github.com/fortran-lang/stdlib/issues/729#issuecomment-1677997997, or unsubscribe https://github.com/notifications/unsubscribe-auth/AD5RO7EA7ZPICKQX62JDWJTXVKBTHANCNFSM6AAAAAA3QCLKCU . You are receiving this because you commented.Message ID: @.***>
sure
Fixed by #730
Description
The gamma function implemented in
cgamma_csp
andcgamma_cdp
in modulestdlib_specialfunctions_gamma
produce the wrong output when the argument is a pure imaginary. They return the complex conjugate of the output instead.Expected Behaviour
An example where the stdlib implementation of
gamma
returns the complex conjugate of the expected result. The expected result here is calculated (with worse accuracy) by the user-defined functiongammaWeierstrass
, which implementshttps://en.wikipedia.org/wiki/Gamma_function#19th_century:_Gauss,_Weierstrass_and_Legendre ,
and by
exp(log_gamma(z))
. The latter two agree up to some low accuracy, but are both different thangamma(z)
. This is not expected, asexp(log_gamma(z))
should be the same asgamma(z)
.test.f90:
output:
Compiler: gfortran 13.2.1
Version of stdlib
a4ff2f054b640857c76dfb0d625d038dd94c9baa
Platform and Architecture
Artix Linux x86_64
Additional Information
It seems like this comes from the imaginary axis not being included in the right complex half-plane ( z % re > 0 ). This causes the argument to be complex conjugated, which then conjugates (*) the output because Γ(z) = Γ(z).
Suggested fix: include the imaginary axis in the right complex half-plane in gamma_cdp and gamma_csp.
This format of defining the left half-plane with a strict inequality, i.e.
if(z % re < zero_k1)
, mirrors the behavior inl_gamma_cdp
andl_gamma_csp
, which is why log_gamma seems to not have this issue.