You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
jax_source: no translation for SpecialFunctions.besselj, bessely and besselk (JAX has no accurate Bessel J, Y or K) #61
jax_source cannot translate SpecialFunctions.besselj(n, x), bessely(n, x) or besselk(n, x). JAX has no accurate Bessel function of the first kind, the second kind or the modified second kind: jax.scipy.special (JAX 0.12) offers i0, i0e, i1, i1e and bessel_jn, and bessel_jn loses accuracy from moderate arguments (table below). ReactiveKernels translates the modified Bessel function of the first kind (besseli/besselix, through _rk_besselix for a general order) but none of these three.
Downstream, StanBlocks' Stan builtins bessel_first_kind, bessel_second_kind and modified_bessel_second_kind cannot reach JAX from a @deffun @juliacompat helper either. From StanBlocks 0.2.47 they raise a named JAXEmissionError at this boundary, while its modified_bessel_first_kind composes RK's besseli translation.
Reproduction
ReactiveKernels 0.1.226, SpecialFunctions 2, Julia 1.10.12:
using ReactiveKernels, SpecialFunctions
@kernelbessel_j(n::Int, x::Float64) =begin
result =besselj(n, x)
return result
endjax_source(bessel_j; have=(:n, :x), want=:result)
raises
JAXEmissionError: the function `besselj` has loops or branches, so its lowered code is not translated in recipe `result = besselj(n, x)`. Define it with `@traceable` (its authored body is translated), author it as a kernel, or give it a `ReactiveKernels._jax_function_call` method; …
bessely(n, x) and besselk(n, x) fail the same way.
jax.scipy.special.bessel_jn accuracy
bessel_jn(x, v=2) (JAX 0.12.0.1, float64) against SciPy's jv(0, x):
x
J_0, JAX bessel_jn
J_0, SciPy jv
3
-0.26005195490193345
-0.2600519549019334
30
-0.08636798402910277
-0.08636798358104021
100
-1.4585557072738466
0.01998585030422312
700
-12.539342015799374
-0.006288272465068766
So it is not a usable translation of besselj. Nothing in jax.scipy.special computes Y or K.
What a translation needs
Module helpers in the style of _rk_besselix (a series for small arguments, asymptotic or uniform expansions for large arguments or orders, a recurrence between), each with a derivative rule (J_n' = (J_(n-1) - J_(n+1)) / 2, the same for Y_n, and K_n' = -(K_(n-1) + K_(n+1)) / 2). They should match SpecialFunctions' values and domain behaviour: bessely and besselk require x >= 0, besselk(n, 0) = Inf, and AMOS rejects besselk(n, Inf). Once RK translates them, StanBlocks' extension can replace its boundary error with a composition over RK's translation, as it does for besseli.
Activity