Skip to content

Commit b6916c0

Browse files
committed
Address formating, T-specific cutoffs, remove compat method wrapper.
1 parent 4dde04e commit b6916c0

File tree

2 files changed

+48
-48
lines changed

2 files changed

+48
-48
lines changed

src/besselk.jl

Lines changed: 45 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ function besselk_positive_args(nu, x::T) where T <: Union{Float32, Float64}
233233
(isinteger(nu-1/2) && !debye_cut) && return sphericalbesselk(nu-1/2, x)*SQRT_PID2(T)*sqrt(x)
234234

235235
# check if the standard asymptotic expansion can be used:
236-
besselk_asexp_cutoff(nu, x) && return besselk_asymptoticexp(nu, x)
236+
besselk_asexp_cutoff(nu, x) && return besselk_asymptoticexp(nu, x, 10)
237237

238238
# use uniform debye expansion if x or nu is large
239239
debye_cut && return besselk_large_orders(nu, x)
@@ -438,64 +438,61 @@ besselk_power_series_cutoff(nu, x::Float32) = x < 10.0f0 || nu > 1.65f0*x - 8.0f
438438
"""
439439
besselk_asymptoticexp(v, x, order)
440440
441-
Computes the asymptotic expansion of K_ν w.r.t. argument. Accurate for large x, and faster than uniform asymptotic expansion for small to small-ish orders. The default order of the expansion is 10.
441+
Computes the asymptotic expansion of K_ν w.r.t. argument. Accurate for large x, and faster than uniform asymptotic expansion for small to small-ish orders. The default order of the expansion in `Bessels.besselk` is 10.
442442
"""
443-
function besselk_asymptoticexp(v, x::T) where T
444-
_besselk_asymptoticexp(v, float(x), 10)
445-
end
446-
besselk_asexp_cutoff(nu, x) = (nu < 20.0) && (x > 25.0)
443+
besselk_asexp_cutoff(nu, x::T) where T = (nu < 20.0) && (x > ASEXP_CUTOFF(T))
447444

448445
# Compute the function for (_v, _v+1), where _v = v-floor(v), and then do
449446
# forward recursion. If the argument is really large, this should be faster than
450447
# the uniform asymptotic expansion and plenty accurate.
451-
function _besselk_asymptoticexp(v, x::T, order) where T
452-
_v = v - floor(v)
453-
(kv, kvp1) = _besselk_as_pair(_v, x, order)
454-
v == _v && return kv
455-
_v += one(v)
456-
twodx = T(2)*inv(x)
457-
while _v < v
458-
(kv, kvp1) = (kvp1, muladd(kvp1, twodx*_v, kv))
459-
_v += one(_v)
460-
end
461-
kvp1
448+
function besselk_asymptoticexp(v, x::T, order) where T
449+
_v = v - floor(v)
450+
(kv, kvp1) = _besselk_as_pair(_v, x, order)
451+
v == _v && return kv
452+
_v += one(v)
453+
twodx = T(2)*inv(x)
454+
while _v < v
455+
(kv, kvp1) = (kvp1, muladd(kvp1, twodx*_v, kv))
456+
_v += one(_v)
457+
end
458+
kvp1
462459
end
463460

464461
# For now, no exponential improvement. It requires the exponential integral
465462
# function, which would either need to be lifted from SpecialFunctions.jl or
466463
# re-implemented. And with an order of, like, 10, this seems to be pretty
467464
# accurate and still faster than the uniform asymptotic expansion.
468465
function _besselk_as_pair(v, x::T, order) where{T}
469-
twox = T(2)
470-
fv = 4*v*v
471-
fvp1 = 4*(v+one(v))^2
472-
_z = x
473-
ser = zero(T)
474-
ser_v = one(T)
475-
ser_vp1 = one(T)
476-
floatj = one(T)
477-
ak_numv = fv - floatj
478-
ak_numvp1 = fvp1 - floatj
479-
factj = one(T)
480-
twofloatj = one(T)
481-
eightj = T(8)
482-
for _ in 1:order
483-
# add to the series:
484-
term_v = ak_numv/(factj*_z*eightj)
485-
ser_v += term_v
486-
term_vp1 = ak_numvp1/(factj*_z*eightj)
487-
ser_vp1 += term_vp1
488-
# update ak and _z:
489-
floatj += one(T)
490-
twofloatj += T(2)
491-
factj *= floatj
492-
fourfloatj = twofloatj*twofloatj
493-
ak_numv *= (fv - fourfloatj)
494-
ak_numvp1 *= (fvp1 - fourfloatj)
495-
_z *= x
496-
eightj *= T(8)
497-
end
498-
pre_multiply = SQRT_PID2(T)*exp(-x)/sqrt(x)
499-
(pre_multiply*ser_v, pre_multiply*ser_vp1)
466+
twox = T(2)
467+
fv = 4*v*v
468+
fvp1 = 4*(v+one(v))^2
469+
_z = x
470+
ser = zero(T)
471+
ser_v = one(T)
472+
ser_vp1 = one(T)
473+
floatj = one(T)
474+
ak_numv = fv - floatj
475+
ak_numvp1 = fvp1 - floatj
476+
factj = one(T)
477+
twofloatj = one(T)
478+
eightj = T(8)
479+
for _ in 1:order
480+
# add to the series:
481+
term_v = ak_numv/(factj*_z*eightj)
482+
ser_v += term_v
483+
term_vp1 = ak_numvp1/(factj*_z*eightj)
484+
ser_vp1 += term_vp1
485+
# update ak and _z:
486+
floatj += one(T)
487+
twofloatj += T(2)
488+
factj *= floatj
489+
fourfloatj = twofloatj*twofloatj
490+
ak_numv *= (fv - fourfloatj)
491+
ak_numvp1 *= (fvp1 - fourfloatj)
492+
_z *= x
493+
eightj *= T(8)
494+
end
495+
pre_multiply = SQRT_PID2(T)*exp(-x)/sqrt(x)
496+
(pre_multiply*ser_v, pre_multiply*ser_vp1)
500497
end
501498

src/constants.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,3 +287,6 @@ const Q3_k1(::Type{Float64}) = (
287287

288288
const SQRT_PID2(::Type{Float64}) = 1.2533141373155003
289289
const SQRT_PID2(::Type{Float32}) = 1.2533141f0
290+
291+
const ASEXP_CUTOFF(::Type{Float64}) = 35.0
292+
const ASEXP_CUTOFF(::Type{Float32}) = 35.0f0 # temporary

0 commit comments

Comments
 (0)