Trouble with Measurements.jl and Bessel Functions

@Jason_Kilpatrick I just added the missing method for result function. The arguments are described in the comments before the new method.

In order to be able to add support to besselj0 you should compute the derivatives of the real and imaginary parts of the result with respect to the real and imaginary parts of the argument, so they’re 4 derivatives in total. If you find out those quantities, besselj0 with complex argument can be readily defined.

In the meantime, you can propagate the uncertainty of besselj0 with complex argument (or any complex-valued function of one complex argument) by using the following macro, similar to the @uncertain macro which works only for functions of real arguments:

julia> using Measurements

julia> macro uncertain_complex(expr::Expr)
           f = esc(expr.args[1]) # Function name
           a = esc(expr.args[2]) # Argument, of Complex{Measurement} type
           return :( Measurements.result($f(Measurements.value($a)),
                                         vcat(Calculus.gradient(x -> real($f(complex(x[1], x[2]))),
                                                                [reim(Measurements.value($a))...]),
                                              Calculus.gradient(x -> imag($f(complex(x[1], x[2]))),
                                                                [reim(Measurements.value($a))...])),
                                         $a) )
       end
@uncertain_complex (macro with 1 method)

julia> @uncertain_complex besselj0(complex(1, 1 ± 0.5))
(0.9376084768060292 ± 0.18251401441177362) - (0.4965299476091221 ± 0.30708016745937555)im

julia> @uncertain_complex besselj0(complex(1 ± 0.5))
(0.7651976865579666 ± 0.22002529286918884) + (0.0 ± 0.0)im

julia> besselj0(1 ± 0.5)
0.7651976865579666 ± 0.22002529287246675

The last two lines shows that the macro correctly works in the case of a complex measurement with null imaginary parts (within numerical accuracy of Calculus.gradient), which is a good test case. This macro requires checking out master branch of the package, but I’ll probably tag a new version next week.