Skip to content

Commit 277bd35

Browse files
committed
actually optimize gamma call away
1 parent 3252399 commit 277bd35

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

src/besselk.jl

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ function besselk(nu, x::T) where T <: Union{Float32, Float64, BigFloat}
211211

212212
if nu > 25.0 || x > 35.0
213213
return besselk_large_orders(nu, x)
214-
elseif x < 2.0 || nu > 1.6x - 1.0
214+
elseif x < 2.0
215215
return besselk_power_series(nu, x)
216216
else
217217
return besselk_continued_fraction(nu, x)
@@ -287,12 +287,15 @@ function besselk_power_series(v, x::T) where T
287287
lxd2 = log(xd2)
288288
xd2_v = exp(v*lxd2)
289289
xd2_nv = exp(-v*lxd2)
290-
# use the gamma function a couple times to start:
290+
291291
gam_v = gamma(v)
292+
# use the reflection identify to calculate gamma(-v)
293+
# use relation gamma(v)*v = gamma(v+1)
292294
xp1 = abs(v) + one(T)
293-
gam_nv = π / (sinpi(xp1) * _gamma(xp1))
294-
gam_1mv = -gam_nv*v # == gamma(one(T)-v)
295-
gam_1mnv = gam_v*v # == gamma(one(T)+v)
295+
gam_nv = π / (sinpi(xp1) * gam_v*v)
296+
297+
gam_1mv = -gam_nv*v
298+
gam_1mnv = gam_v*v
296299

297300
# One final re-compression of a few things:
298301
_t1 = gam_v*xd2_nv*gam_1mv

0 commit comments

Comments
 (0)