Skip to content

Commit eb71c65

Browse files
committed
Rewrite more concisely
1 parent ae35d10 commit eb71c65

File tree

1 file changed

+83
-59
lines changed

1 file changed

+83
-59
lines changed

src/gamma_inc.jl

Lines changed: 83 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -421,39 +421,32 @@ 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(30)
425-
flag = false
426-
apn = a + 1.0
427-
t = x/apn
428-
wk[1] = t
429-
loop = 2
430-
for indx = 2:20
431-
apn += 1.0
432-
t *= x/apn
433-
if t <= 1.0e-3
434-
loop = indx
435-
flag = true
436-
break
437-
end
438-
wk[indx] = t
439-
end
440-
if !flag
441-
loop = 20
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
442436
end
437+
438+
# sum the smaller terms directly
443439
sm = t
444-
tol = 0.5*acc #tolerance
445-
while true
446-
apn += 1.0
447-
t *= x/apn
448-
sm += t
449-
if t <= tol
450-
break
451-
end
452-
end
453-
for j = loop-1:-1:1
454-
sm += wk[j]
440+
tolerance = 0.5acc
441+
while t > tolerance
442+
apn += 1.0
443+
t *= x / apn
444+
sm += t
455445
end
456-
p = (rgammax(a, x)/a)*(1.0 + sm)
446+
447+
# Sum terms from small to large
448+
sm += sum(reverse!(@view wk[1:i]))
449+
p = (rgammax(a, x) / a) * (1.0 + sm)
457450
return (p, 1.0 - p)
458451
end
459452
"""
@@ -470,40 +463,71 @@ External links: [DLMF](https://dlmf.nist.gov/8.11.2)
470463
See also: [`gamma_inc(a,x,ind)`](@ref SpecialFunctions.gamma_inc)
471464
"""
472465
function gamma_inc_asym(a::Float64, x::Float64, ind::Integer)
473-
wk = zeros(30)
474-
flag = false
475466
acc = acc0[ind + 1]
476-
amn = a - 1.0
477-
t = amn/x
478-
wk[1] = t
479-
loop = 2
480-
for indx = 2:20
481-
amn -= 1.0
482-
t *= amn/x
483-
if abs(t) <= 1.0e-3
484-
loop = indx
485-
flag = true
486-
break
487-
end
488-
wk[indx] = t
489-
end
490-
if !flag
491-
loop = 20
467+
wk = zeros(20)
468+
amn = a
469+
470+
# compute and store larger terms in wk, to add from small to large
471+
t = 1
472+
i = 0
473+
while i < 20
474+
i += 1
475+
amn -= 1.0
476+
t *= amn / x
477+
abs(t) > 1.0e-3 || break
478+
wk[i] = t
492479
end
480+
481+
# sum the smaller terms directly
493482
sm = t
494-
while true
495-
if abs(t) < acc
496-
break
497-
end
498-
amn -= 1.0
499-
t *= amn/x
500-
sm += t
501-
end
502-
for j = loop-1:-1:1
503-
sm += wk[j]
483+
while abs(t) > acc
484+
amn -= 1.0
485+
t *= amn / x
486+
sm += t
504487
end
505-
q = (rgammax(a, x)/x)*(1.0 + sm)
488+
489+
# Sum terms from small to large
490+
sm += sum(reverse!(@view wk[1:i]))
491+
q = (rgammax(a, x) / x) * (1.0 + sm)
506492
return (1.0 - q, q)
493+
494+
495+
496+
497+
# wk = zeros(30)
498+
# flag = false
499+
# acc = acc0[ind + 1]
500+
# amn = a - 1.0
501+
# t = amn/x
502+
# wk[1] = t
503+
# loop = 2
504+
# for indx = 2:20
505+
# amn -= 1.0
506+
# t *= amn/x
507+
# if abs(t) <= 1.0e-3
508+
# loop = indx
509+
# flag = true
510+
# break
511+
# end
512+
# wk[indx] = t
513+
# end
514+
# if !flag
515+
# loop = 20
516+
# end
517+
# sm = t
518+
# while true
519+
# if abs(t) < acc
520+
# break
521+
# end
522+
# amn -= 1.0
523+
# t *= amn/x
524+
# sm += t
525+
# end
526+
# for j = loop-1:-1:1
527+
# sm += wk[j]
528+
# end
529+
# q = (rgammax(a, x)/x)*(1.0 + sm)
530+
# return (1.0 - q, q)
507531
end
508532
"""
509533
gamma_inc_taylor_x(a,x,ind)

0 commit comments

Comments
 (0)