Skip to content

Commit e2d3217

Browse files
committed
Avoid negative initial x0 in gamma_inc_inv_qsmall. When the initial
x0 value is less than one, the updates in gamma_inc_inv_qsmall could make the initial guess negative whith could make the Newton iteration fail. Also, expand the tests of gamma_inc_inv.
1 parent 9968537 commit e2d3217

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/gamma_inc.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ end
179179
"""
180180
lambdaeta(eta)
181181
182-
Compute the value of eta satisfying ``eta^{2}/2 = \\lambda-1-\\ln{\\lambda}``.
182+
Compute the value of ``\\lambda`` satisfying ``\\eta^{2}/2 = \\lambda-1-\\log{\\lambda}``.
183183
"""
184184
function lambdaeta(eta::Float64)
185185
s = eta*eta*0.5
@@ -715,7 +715,9 @@ function gamma_inc_inv_qsmall(a::Float64, q::Float64, qgammaxa::Float64)
715715
ck3 = (@horner(l, @horner(b, -12, -24, -11), @horner(b, 12, 24, 6), @horner(b, -6, -9), 2))/6.0
716716
ck4 = (@horner(l, @horner(b, 72, 162, 120, 25), @horner(b, -72, -168, -114, -12), @horner(b, 36, 84, 36), @horner(b, -12, -22), 3))/12.0
717717
x0 = x0 - l + b*r*@horner(r, ck1, ck2, ck3, ck4)
718-
else
718+
elseif x0 > 1
719+
# The x0 > 1 condition isn't in the original version but without it
720+
# the update in the branch can cause negative initial x0
719721
r = 1.0/x0
720722
= l*l
721723
ck1 = l - 1.0
@@ -942,7 +944,7 @@ function __gamma_inc_inv(a::Float64, minpq::Float64, pcase::Bool)
942944
if logr < log(0.2*(1 + a)) #small value of p
943945
x0 = gamma_inc_inv_psmall(a, logr)
944946
elseif !pcase && a < 10 && minpq < 0.02 && (qgammaxa = minpq*gammax(a)*sqrt(twoπ/a)) < 1 #small q
945-
# This deviates from the original version. The tmp variable
947+
# This deviates from the original version. The qgammaxa variable
946948
# here ensures that the argument of sqrt in gamma_inc_inv_qsmall
947949
# is positive
948950
x0 = gamma_inc_inv_qsmall(a, minpq, qgammaxa)

test/gamma_inc.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,16 @@ end
191191
q = 0.010101010101010102
192192
@test last(gamma_inc(a, gamma_inc_inv(a, 1 - q, q))) q
193193
end
194+
195+
@testset "Issue 387" begin
196+
n = 1000
197+
@testset "a=$a" for a in exp10.(range(-20, stop=20, length=n))
198+
@testset "x=$x" for x = exp10.(range(-20, stop=2, length=n))
199+
p, q = gamma_inc(a, x)
200+
@test p < floatmin() || q < floatmin() || gamma_inc_inv(a, p, q) x
201+
end
202+
end
203+
end
194204
end
195205

196206
double(x::Real) = Float64(x)

0 commit comments

Comments
 (0)