Skip to content

Commit 0671f11

Browse files
committed
Fix negative order case for sphericalbesselk in special integer routine and add tests.
1 parent d090b51 commit 0671f11

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/modifiedsphericalbessel.jl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,14 @@ sphericalbesselk(nu, x) = _sphericalbesselk(nu, float(x))
88

99
function _sphericalbesselk(nu, x::T) where T
1010
if isinteger(nu) && nu < 41.5
11-
return sphericalbesselk_int(nu, x)
11+
# using ifelse here to hopefully cut out a branch on nu < 0 or not. The
12+
# symmetry here is that
13+
# k_{-n} = (...)*K_{-n + 1/2}
14+
# = (...)*K_{|n| - 1/2}
15+
# = (...)*K_{|n|-1 + 1/2}
16+
# = k_{|n|-1}
17+
_nu = ifelse(nu<zero(nu), abs(nu)-one(nu), nu)
18+
return sphericalbesselk_int(Int(_nu), x)
1219
else
1320
return inv(SQRT_PID2(T)*sqrt(x))*besselk(nu+1/2, x)
1421
end

test/sphericalbessel_test.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,9 @@ v, x = -4.0, 5.6
5858
v, x = -6.8, 15.6
5959
@test isapprox(Bessels.sphericalbesselj(v, x), 0.04386355397884301866595, rtol=3e-12)
6060
@test isapprox(Bessels.sphericalbessely(v, x), 0.05061013363904335437354, rtol=3e-12)
61+
62+
# test for negative order of spherical modified besselk in the special integer
63+
# routine:
64+
for v in 1:10
65+
@test Bessels.sphericalbesselk(-v, 1.1) Bessels.sphericalbesselk(v-1, 1.1)
66+
end

0 commit comments

Comments
 (0)