Skip to content

Commit ade38cb

Browse files
committed
Incorporate further improvement from @helto
1 parent 0b0a61a commit ade38cb

File tree

1 file changed

+16
-54
lines changed

1 file changed

+16
-54
lines changed

src/gamma_inc.jl

Lines changed: 16 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -421,35 +421,16 @@ See also: [`gamma_inc(a,x,ind)`](@ref SpecialFunctions.gamma_inc)
421421
"""
422422
function gamma_inc_taylor(a::Float64, x::Float64, ind::Integer)
423423
acc = acc0[ind + 1]
424-
wk = zeros(20)
425-
apn = a
426-
427-
# compute and store larger terms in wk, to add from small to large
428-
t = 1
429-
i = 0
430-
while i < 20
431-
i += 1
432-
apn += 1.0
433-
t *= x / apn
434-
t > 1.0e-3 || break
435-
wk[i] = t
436-
end
437-
438-
# sum the smaller terms directly
439-
sm = t
424+
MaxIter = 5000
425+
t = 1.0
426+
s = 0.0
440427
tolerance = 0.5acc
441-
while t > tolerance
442-
apn += 1.0
443-
t *= x / apn
444-
sm += t
445-
end
446-
447-
# sum terms from small to large
448-
for v @view wk[i:-1:1]
449-
sm += v
428+
for i in 1:MaxIter
429+
s += t
430+
t < tolerance && break
431+
t *= x / (a + i)
450432
end
451-
452-
p = (rgammax(a, x) / a) * (1.0 + sm)
433+
p = (rgammax(a, x) / a) * s
453434
return (p, 1.0 - p)
454435
end
455436
"""
@@ -467,34 +448,15 @@ See also: [`gamma_inc(a,x,ind)`](@ref SpecialFunctions.gamma_inc)
467448
"""
468449
function gamma_inc_asym(a::Float64, x::Float64, ind::Integer)
469450
acc = acc0[ind + 1]
470-
wk = zeros(20)
471-
amn = a
472-
473-
# compute and store larger terms in wk, to add from small to large
474-
t = 1
475-
i = 0
476-
while i < 20
477-
i += 1
478-
amn -= 1.0
479-
t *= amn / x
480-
abs(t) > 1.0e-3 || break
481-
wk[i] = t
482-
end
483-
484-
# sum the smaller terms directly
485-
sm = t
486-
while abs(t) > acc
487-
amn -= 1.0
488-
t *= amn / x
489-
sm += t
451+
MaxIter = 5000
452+
t = 1.0
453+
s = 0.0
454+
for i in 1:MaxIter
455+
s += t
456+
abs(t) < acc && break
457+
t *= (a - i) / x
490458
end
491-
492-
# sum terms from small to large
493-
for v @view wk[i:-1:1]
494-
sm += v
495-
end
496-
497-
q = (rgammax(a, x) / x) * (1.0 + sm)
459+
q = (rgammax(a, x) / x) * s
498460
return (1.0 - q, q)
499461
end
500462
"""

0 commit comments

Comments
 (0)