Skip to content

Commit 85ac912

Browse files
authored
Merge pull request #312 from JuliaMath/an/En_gamma
Fix evaluation of continued fraction for En_gamma
2 parents 229d13a + 84df26e commit 85ac912

File tree

2 files changed

+53
-8
lines changed

2 files changed

+53
-8
lines changed

src/expint.jl

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -214,17 +214,19 @@ function En_cf_gamma(ν::Number, z::Number, n::Int=1000)
214214
ϵ = 10*eps(real(B))
215215
scale = sqrt(floatmax(real(A)))
216216

217-
iters = 1
218-
j = 1
219-
for i = 2:n
217+
iters = 0
218+
for i = 1:n
220219
iters += 1
221220

222-
term = iseven(i) ? -(i÷2 - 1 - ν)*z : z*(i÷2)
223-
A, Aprev = (i - ν)*A + term * Aprev, A
224-
B, Bprev = (i - ν)*B + term * Bprev, B
221+
a = iseven(i) ? (i÷2)*z : -((i + 1)÷2 - ν)*z
222+
b = (i + 1 - ν)
223+
224+
A, Aprev = b*A + a*Aprev, A
225+
B, Bprev = b*B + a*Bprev, B
225226

226-
conv = abs(Aprev*B - A*Bprev) < ϵ*abs(B*Bprev)
227-
conv && break
227+
if abs(Aprev*B - A*Bprev) < ϵ*abs(Aprev*B)
228+
break
229+
end
228230

229231
if max(abs(real(A)), abs(imag(A))) > scale
230232
A /= scale
@@ -436,6 +438,7 @@ function _expint(ν::Number, z::Number, niter::Int=1000, ::Val{expscaled}=Val{fa
436438
else
437439
g, cf, _ = zero(z), En_cf_nogamma(ν, z, niter)...
438440
end
441+
439442
g != 0 && (g *= gmult)
440443
cf = expscaled ? cf : En_safeexpmult(-z, cf)
441444
return g + cf

test/expint.jl

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,48 @@ using Base.MathConstants
123123
for (ν, z, E) in zip(complex_orders, complex_inputs, sympy_complex_outputs)
124124
abs(ν) 50 ? (@test expint(ν, z) E) : (@test_throws ArgumentError expint(ν, z) E)
125125
end
126+
127+
xs = range(2.0, stop=5.0, step=0.05)
128+
ys = [0.05702612399289205, 0.05308665097847697, 0.04944294741593074,
129+
0.04607036967463369, 0.04294659589019342, 0.04005137577047593,
130+
0.03736631128005022, 0.03487466387112418, 0.03256118461138553,
131+
0.03041196412241836, 0.02841429970928976, 0.02655657745053252,
132+
0.02482816734246761, 0.0232193298641301, 0.02172113255824923,
133+
0.02032537541727814, 0.01902452402744482, 0.01781164956316038,
134+
0.01668037484292225, 0.01562482575941789, 0.01463958748361088,
135+
0.01371966491744429, 0.01286044693430924, 0.01205767400216573,
136+
0.01130740883247835, 0.01060600974003776, 0.00995010643520129,
137+
0.00933657800187881, 0.00876253284236996, 0.00822529039448117,
138+
0.007722364447687571, 0.007251447903858146, 0.006810398844575473,
139+
0.006397227781646947, 0.006010085980275144, 0.005647254755746198,
140+
0.005307135654593942, 0.004988241440163916, 0.004689187810475,
141+
0.004408685783377122, 0.004145534690336333, 0.003898615725833893,
142+
0.003666886004423625, 0.003449373082020276, 0.00324516990205213,
143+
0.003053430130755408, 0.002873363849164126, 0.0027042335722962,
144+
0.002545350568691675, 0.002396071455853417, 0.002255795049301819,
145+
0.002123959444908601, 0.00200003931594166, 0.001883543407852843,
146+
0.001774012215290864, 0.001671015827136935, 0.00157415192655548,
147+
0.001483043934137846, 0.001397339283204469, 0.001316707817230031,
148+
0.001240840300175119]
149+
150+
@testset "En_cf_nogamma" begin
151+
for (x, y) in zip(xs, ys)
152+
cf, iter = SpecialFunctions.En_cf_nogamma(1 / 2, x)
153+
@test cf*exp(-x) y
154+
end
155+
end
156+
@testset "En_cf_gamma" begin
157+
for (x, y) in zip(xs, ys)
158+
Γ, cf, iter = SpecialFunctions.En_cf_gamma(1 / 2, x)
159+
@test Γ + cf*exp(-x) y
160+
end
161+
end
162+
@testset "En_expand_origin" begin
163+
for (x, y) in zip(xs, ys)
164+
res = SpecialFunctions.En_expand_origin(1 / 2, x, 1000)
165+
@test res y
166+
end
167+
end
126168
end
127169

128170
# issue #259: positive vs. negative integer order for small/larger args

0 commit comments

Comments
 (0)