Skip to content

Commit ccfa140

Browse files
committed
fix inf and zero cases
1 parent 99dd324 commit ccfa140

File tree

5 files changed

+19
-13
lines changed

5 files changed

+19
-13
lines changed

src/airy.jl

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function _airyai(x::T) where T <: Union{Float32, Float64}
2424
z = 2 * x_abs^(T(3)/2) / 3
2525

2626
if x > zero(T)
27-
return sqrt(x / 3) * besselk(one(T)/3, z) / π
27+
return isinf(z) ? zero(T) : sqrt(x / 3) * besselk(one(T)/3, z) / π
2828
elseif x < zero(T)
2929
Jv, Yv = besseljy_positive_args(one(T)/3, z)
3030
Jmv = (Jv - sqrt(T(3)) * Yv) / 2
@@ -48,7 +48,7 @@ function _airyaiprime(x::T) where T <: Union{Float32, Float64}
4848
z = 2 * x_abs^(T(3)/2) / 3
4949

5050
if x > zero(T)
51-
return -x * besselk(T(2)/3, z) / (sqrt(T(3)) * π)
51+
return isinf(z) ? zero(T) : -x * besselk(T(2)/3, z) / (sqrt(T(3)) * π)
5252
elseif x < zero(T)
5353
Jv, Yv = besseljy_positive_args(T(2)/3, z)
5454
Jmv = -(Jv + sqrt(T(3))*Yv) / 2
@@ -72,7 +72,7 @@ function _airybi(x::T) where T <: Union{Float32, Float64}
7272
z = 2 * x_abs^(T(3)/2) / 3
7373

7474
if x > zero(T)
75-
return sqrt(x / 3) * (besseli(-one(T)/3, z) + besseli(one(T)/3, z))
75+
return isinf(z) ? z : sqrt(x / 3) * (besseli(-one(T)/3, z) + besseli(one(T)/3, z))
7676
elseif x < zero(T)
7777
Jv, Yv = besseljy_positive_args(one(T)/3, z)
7878
Jmv = (Jv - sqrt(T(3)) * Yv) / 2
@@ -86,13 +86,17 @@ end
8686
airybiprime(x)
8787
Derivative of the Airy function of the second kind ``\\operatorname{Bi}'(x)``.
8888
"""
89-
function airybiprime(x::T) where T
89+
airybiprime(x::Real) = _airybiprime(float(x))
90+
91+
_airybiprime(x::Float16) = Float16(_airybiprime(Float32(x)))
92+
93+
function _airybiprime(x::T) where T <: Union{Float32, Float64}
9094
isnan(x) && return x
9195
x_abs = abs(x)
9296
z = 2 * x_abs^(T(3)/2) / 3
9397

9498
if x > zero(T)
95-
return x * (besseli(T(2)/3, z) + besseli(-T(2)/3, z)) / sqrt(T(3))
99+
return isinf(z) ? z : x * (besseli(T(2)/3, z) + besseli(-T(2)/3, z)) / sqrt(T(3))
96100
elseif x < zero(T)
97101
Jv, Yv = besseljy_positive_args(T(2)/3, z)
98102
Jmv = -(Jv + sqrt(T(3))*Yv) / 2

src/besseli.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ Modified Bessel function of the first kind of order nu, ``I_{nu}(x)`` for positi
207207
function besseli_positive_args(nu, x::T) where T <: Union{Float32, Float64}
208208
iszero(nu) && return besseli0(x)
209209
isone(nu) && return besseli1(x)
210+
isinf(x) && return T(Inf)
210211

211212
# use large argument expansion if x >> nu
212213
besseli_large_argument_cutoff(nu, x) && return besseli_large_argument(nu, x)

src/besselk.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ Modified Bessel function of the second kind of order nu, ``K_{nu}(x)`` valid for
217217
"""
218218
function besselk_positive_args(nu, x::T) where T <: Union{Float32, Float64}
219219
iszero(x) && return T(Inf)
220+
isinf(x) && return zero(T)
220221

221222
# dispatch to avoid uniform expansion when nu = 0
222223
iszero(nu) && return besselk0(x)

test/airy_test.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ for x in [0.0; rand(10000)*100]
1010
@test isapprox(airyaiprime(-x), SpecialFunctions.airyaiprime(-x), rtol=1e-9)
1111

1212
@test isapprox(airybi(x), SpecialFunctions.airybi(x), rtol=1e-12)
13-
@test isapprox(airybi(-x), SpecialFunctions.airybi(-x), rtol=1e-9)
13+
@test isapprox(airybi(-x), SpecialFunctions.airybi(-x), rtol=5e-8)
1414

1515
@test isapprox(airybiprime(x), SpecialFunctions.airybiprime(x), rtol=1e-12)
1616
@test isapprox(airybiprime(-x), SpecialFunctions.airybiprime(-x), rtol=1e-9)
1717
end
1818

1919
# test Inf
2020
@test iszero(airyai(Inf))
21-
@test iszer(airyaiprime(Inf))
21+
@test iszero(airyaiprime(Inf))
2222
@test isinf(airybi(Inf))
2323
@test isinf(airybiprime(Inf))
2424

test/runtests.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ using Bessels
22
using Test
33
import SpecialFunctions
44

5-
#@time @testset "besseli" begin include("besseli_test.jl") end
6-
#@time @testset "besselk" begin include("besselk_test.jl") end
7-
#@time @testset "besselj" begin include("besselj_test.jl") end
8-
#@time @testset "bessely" begin include("bessely_test.jl") end
9-
#@time @testset "hankel" begin include("hankel_test.jl") end
10-
#@time @testset "gamma" begin include("gamma_test.jl") end
5+
@time @testset "besseli" begin include("besseli_test.jl") end
6+
@time @testset "besselk" begin include("besselk_test.jl") end
7+
@time @testset "besselj" begin include("besselj_test.jl") end
8+
@time @testset "bessely" begin include("bessely_test.jl") end
9+
@time @testset "hankel" begin include("hankel_test.jl") end
10+
@time @testset "gamma" begin include("gamma_test.jl") end
1111
@time @testset "airy" begin include("airy_test.jl") end

0 commit comments

Comments
 (0)