SpecialFunctions.jl icon indicating copy to clipboard operation
SpecialFunctions.jl copied to clipboard

`besselk(x, 0im)` fails

Open theogf opened this issue 3 years ago • 5 comments

besselk(x, 0im) should just return T(Inf) for any x but returns an AmosException instead. Here is a MWE:

using SpecialFunctions
besselk(2, 0im)
ERROR: AmosException with id 1: input error.
Stacktrace:
 [1] _besselk(nu::Float64, z::ComplexF64, kode::Int32)
   @ SpecialFunctions ~/.julia/packages/SpecialFunctions/9pXme/src/bessel.jl:274
 [2] besselk
   @ ~/.julia/packages/SpecialFunctions/9pXme/src/bessel.jl:408 [inlined]
 [3] besselk(nu::Int64, z::Complex{Int64})
   @ SpecialFunctions ~/.julia/packages/SpecialFunctions/9pXme/src/bessel.jl:586
 [4] top-level scope
   @ REPL[14]:1

Error noted in https://github.com/JuliaStats/Distributions.jl/pull/1504

theogf avatar Feb 10 '22 16:02 theogf

That seems reasonable, but it is complicated by the fact that besselk has a branch cut along the negative real axis, so the exact limit isn't well-defined (i.e. it's a singularity, but the arg depends on which direction you approach from).

Other functions have similar issues (e.g. log), and there are some conventions for dealing with this (https://people.freebsd.org/~das/kahan86branch.pdf).

simonbyrne avatar Feb 10 '22 17:02 simonbyrne

I think it might make sense to define it as complex(T(Inf), -zero(T)*real(z)*imag(z))?

simonbyrne avatar Feb 10 '22 17:02 simonbyrne

How does https://github.com/heltonmc/Bessels.jl do on some of these cases?

ViralBShah avatar Feb 28 '22 14:02 ViralBShah

It looks like besselk in Bessels.jl does not allow for complex arguments...

theogf avatar Jun 06 '22 18:06 theogf

yeah. They're in scope, but not implemented yet. If anyone wants to start working on it, it would be a good addition.

oscardssmith avatar Aug 24 '22 21:08 oscardssmith