# Trouble with Measurements.jl and Bessel Functions

**URL:** <https://discourse.julialang.org/t/trouble-with-measurements-jl-and-bessel-functions/4562>\
**Category:** New to Julia\
**Tags:** question, measurements\
**Created:** [June 30, 2017, 11:16am UTC](https://discourse.julialang.org/t/trouble-with-measurements-jl-and-bessel-functions/4562 "2017-06-30T11:16:04Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [July 2, 2017, 3:06pm UTC](https://discourse.julialang.org/t/trouble-with-measurements-jl-and-bessel-functions/4562/6 "2017-07-02T15:06:02Z")

</div>

@Jason_Kilpatrick I just [added](https://github.com/JuliaPhysics/Measurements.jl/commit/7463ade7d8c735b1b705dfb4fcdff0924d3de4bc) 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](https://measurementsjl.readthedocs.io/en/latest/usage.html#propagate-uncertainty-for-arbitrary-functions) which works only for functions of real arguments:

```julia
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.

---

_[View the full topic](https://discourse.julialang.org/t/trouble-with-measurements-jl-and-bessel-functions/4562)._
