Skip to content

Commit 2b880b8

Browse files
committed
refactor: share the Temme (P,Q) combine step; tighten wide/narrow-band tolerances
Extract the c/w/t -> (P,Q) combine into _gamma_inc_temme_combine, used by both gamma_inc_temme_1 and the ind=2 wide-band branch in _gamma_inc that previously duplicated it inline. Measured actual error against the mpmath references: wide-band tops out at 1.3e-4, narrow-band at 4.6e-5, so tighten rtol from 5e-4 to 2e-4/1e-4 respectively (~1.5-2x margin) to catch regressions sooner.
1 parent e2faf49 commit 2b880b8

2 files changed

Lines changed: 19 additions & 18 deletions

File tree

src/gamma_inc.jl

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,19 @@ Used instead of it's previous function when ``\sigma \leq e_{0}/\sqrt{a}``.
659659
660660
External links: [DLMF 8.12.8](https://dlmf.nist.gov/8.12.8)
661661
"""
662+
# Combine the Temme series value `t` with the Gaussian-tail term `c*w` into (P, Q);
663+
# shared by gamma_inc_temme_1's three branches and the ind=2 wide-band case in
664+
# _gamma_inc, which recomputes c/w/t itself instead of calling gamma_inc_temme_1.
665+
function _gamma_inc_temme_combine(c::Float64, w::Float64, t::Float64, rta::Float64, l::Float64)
666+
if l < 1.0
667+
p = c*(w - rt2pin*t/rta)
668+
return (p, 1.0 - p)
669+
else
670+
q = c*(w + rt2pin*t/rta)
671+
return (1.0 - q, q)
672+
end
673+
end
674+
662675
function gamma_inc_temme_1(a::Float64, x::Float64, z::Float64, ind::Integer)
663676
iop = ind + 1
664677
l = x/a
@@ -689,13 +702,7 @@ function gamma_inc_temme_1(a::Float64, x::Float64, z::Float64, ind::Integer)
689702
t = @evalpoly(z, d00, d0[1])
690703

691704
end
692-
if l < 1.0
693-
p = c*(w - rt2pin*t/sqrt(a))
694-
return (p, 1.0 - p)
695-
else
696-
q = c*(w + rt2pin*t/sqrt(a))
697-
return (1.0 - q, q)
698-
end
705+
return _gamma_inc_temme_combine(c, w, t, sqrt(a), l)
699706
end
700707

701708
"""
@@ -919,13 +926,7 @@ function _gamma_inc(a::Float64, x::Float64, ind::Integer)
919926
return gamma_inc_temme(a, x, z)
920927
else
921928
t = @evalpoly(z, d00, d0[1], d0[2], d0[3])
922-
if l < 1.0
923-
p = c*(w - rt2pin*t/rta)
924-
return (p, 1.0 - p)
925-
else
926-
q = c*(w + rt2pin*t/rta)
927-
return (1.0 - q, q)
928-
end
929+
return _gamma_inc_temme_combine(c, w, t, rta, l)
929930
end
930931
else
931932
return _gamma_inc_choose_algorithm(a, x, ind)

test/gamma_inc.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,17 +113,17 @@ end
113113
(1000.0, 650.0, 2.957759012559303e-37, 1.0),
114114
(1000.0, 1350.0, 1.0, 7.635408516228592e-24),
115115
)
116-
@test gamma_inc(a, x, 2)[1] p rtol=5e-4
117-
@test gamma_inc(a, x, 2)[2] q rtol=5e-4
116+
@test gamma_inc(a, x, 2)[1] p rtol=2e-4
117+
@test gamma_inc(a, x, 2)[2] q rtol=2e-4
118118
end
119119
@testset "ind=2 narrow band: a=$a, x=$x" for (a, x, p, q) in (
120120
(10.0, 9.778640563788214, 0.5140881193058611, 0.485911880694139),
121121
(10.0, 10.221359436211785, 0.5694405131522309, 0.43055948684776907),
122122
(100000.0, 99977.86405637882, 0.47251429019620883, 0.5274857098037912),
123123
(100000.0, 100022.13594362116, 0.5283205849613148, 0.4716794150386851),
124124
)
125-
@test gamma_inc(a, x, 2)[1] p rtol=5e-4
126-
@test gamma_inc(a, x, 2)[2] q rtol=5e-4
125+
@test gamma_inc(a, x, 2)[1] p rtol=1e-4
126+
@test gamma_inc(a, x, 2)[2] q rtol=1e-4
127127
end
128128
@testset "ind=0 unchanged: a=$a, x=$x" for (a, x, p, q) in (
129129
(14.0, 13.911135637064119, 0.5261046500921642, 0.4738953499078358),

0 commit comments

Comments
 (0)