Skip to content

Commit 5320155

Browse files
eprovstsimonbyrne
authored andcommitted
Added faster method for besselh with real input (#200)
* Added faster method for besselh with real input * Added tests for issue #29
1 parent 8b4f545 commit 5320155

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

src/bessel.jl

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,18 @@ function besselh(nu::Float64, k::Integer, z::Complex{Float64})
314314
return _besselh(nu,Int32(k),z,Int32(1))
315315
end
316316

317+
function besselh(nu::Float64, k::Integer, x::Float64)
318+
# Given that x is real, Jnu(x) and Ynu(x) are also real.
319+
if k == 1
320+
return complex(besselj(nu, x), bessely(nu, x))
321+
elseif k == 2
322+
return complex(besselj(nu, x), -bessely(nu, x))
323+
else
324+
# We emulate ZBESH's behaviour
325+
throw(AmosException(1))
326+
end
327+
end
328+
317329
"""
318330
besselhx(nu, [k=1,] z)
319331
@@ -583,9 +595,8 @@ end
583595
for bfn in (:besselh, :besselhx)
584596
@eval begin
585597
$bfn(nu, z) = $bfn(nu, 1, z)
586-
$bfn(nu::Real, k::Integer, x::Real) = $bfn(nu, k, float(x))
587-
$bfn(nu::Real, k::Integer, x::AbstractFloat) = $bfn(float(nu), k, complex(x))
588-
598+
$bfn(nu::Real, k::Integer, x::Real) = $bfn(float(nu), k, float(x))
599+
$bfn(nu::AbstractFloat, k::Integer, x::AbstractFloat) = $bfn(float(nu), k, complex(x))
589600
function $bfn(nu::Real, k::Integer, z::Complex)
590601
Tf = promote_type(float(typeof(nu)),float(typeof(real(z))))
591602
$bfn(Tf(nu), k, Complex{Tf}(z))
@@ -596,6 +607,9 @@ for bfn in (:besselh, :besselhx)
596607
end
597608
end
598609

610+
besselh(nu::Float16, k::Integer, x::Float16) = Complex{Float16}(besselh(Float32(nu), k, Float32(x)))
611+
besselh(nu::Float32, k::Integer, x::Float32) = Complex{Float32}(besselh(Float64(nu), k, Float64(x)))
612+
599613
"""
600614
besselj0(x)
601615

test/runtests.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,10 @@ end
260260
@test besselh(Float16(3),1,Float16(3)) true_h133
261261
@test besselh(3,2,3) conj(true_h133)
262262
@test besselh(-3,2,3) -conj(true_h133)
263+
@test besselh(1,1,0) complex(0,-Inf)
264+
@test besselh(1,2,0) complex(0,Inf)
263265
@testset "Error throwing" begin
264-
@test_throws AmosException besselh(1,0)
266+
@test_throws AmosException besselh(1,5,0)
265267
@test_throws MethodError besselh(1,big(1.0))
266268
@test_throws MethodError besselh(1,complex(big(1.0)))
267269
@test_throws MethodError besselhx(1,big(1.0))

0 commit comments

Comments
 (0)