# MonteCarloMeasurements.jl - Object with Particles Passed to Function with Comparison Operator

**URL:** <https://discourse.julialang.org/t/montecarlomeasurements-jl-object-with-particles-passed-to-function-with-comparison-operator/105843>\
**Category:** General Usage\
**Tags:** monte-carlo, measurements\
**Created:** [November 5, 2023, 11:53pm UTC](https://discourse.julialang.org/t/montecarlomeasurements-jl-object-with-particles-passed-to-function-with-comparison-operator/105843 "2023-11-05T23:53:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![matthew3245](https://avatars.discourse-cdn.com/v4/letter/m/ba8739/32.png) [@matthew3245](https://discourse.julialang.org/u/matthew3245)\
**Post date:** [November 5, 2023, 11:53pm UTC](https://discourse.julialang.org/t/montecarlomeasurements-jl-object-with-particles-passed-to-function-with-comparison-operator/105843/1 "2023-11-05T23:53:38Z")

</div>

I’m trying to pass an object that contains particles to a function that uses comparison operators. Consider the following simple example:

```julia
using MonteCarloMeasurements

mutable struct Foo{T}
    p::T
end

function add_one!(foo::Foo)
    if foo.p > 1
        foo.p += 1
    end
end

```

Here, the mutable struct `Foo` contains a parameter `p` which I would like to work with particles. The function `add_one!` simply adds one to the parameter `p` if its greater than 1.

I would like to pass a `Foo` object that contains particles to the `add_one!` function such that each particle is compared to the `>1` operator. The following code doesn’t work due to unsafe comparisons.

```julia
p = Particles([1, 2, 3])
foo = Foo(p)
add_one!(foo)

ERROR: Comparison of uncertain values using comparison mode safe failed...

```

The error above makes sense, however, I do not want to turn on unsafe comparison as I want the comparison to occur for each particle separately. Is this possible? I saw there was a `with_workspace` function, but I don’t properly understand how to use it. Thanks.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [November 6, 2023, 4:56am UTC](https://discourse.julialang.org/t/montecarlomeasurements-jl-object-with-particles-passed-to-function-with-comparison-operator/105843/2 "2023-11-06T04:56:47Z")

</div>

You have reached more or less the limit of what is possible to handle automatically with the current design of MCM. If you have a single function that you need to handle, the easiest way would probably be to implement that by hand for `Foo{Particles}`. If you have many such functions, code generation is an option.

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [November 6, 2023, 10:19am UTC](https://discourse.julialang.org/t/montecarlomeasurements-jl-object-with-particles-passed-to-function-with-comparison-operator/105843/3 "2023-11-06T10:19:33Z")

</div>

I’m curious why it doesn’t work?

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [November 6, 2023, 11:02am UTC](https://discourse.julialang.org/t/montecarlomeasurements-jl-object-with-particles-passed-to-function-with-comparison-operator/105843/4 "2023-11-06T11:02:08Z")

</div>

MCM works by implementing a type that behaves like a real number, but internally the type actually represents several samples. Each function that acts on numbers is then overloaded to act on all samples. The problem appears in an expression like

```julia
if foo.p > 1
end

```

`foo.p > 1` has to result in a boolean, but since `foo.p` represents `N` different values of a number, it’s not always clear what boolean to produce. For functions that take numbers as arguments (`fun(x::Real)`), we have a function `register_primitive fun` that says that when this function is called with uncertain numbers, you should call this function on each of the internal numbers, but when the uncertain numbers are stored in the field of a struct, it’s not as easy. For example, how should we overload `fun(x::Foo)` to properly handle the fact that one of the fields of `foo` is an uncertain number? Short of manually implementing a custom method of `fun(x::Foo{Particles})` that is aware of the type `Foo` and the fact that it internally stores uncertain numbers, I’m not sure how to do it. To handle this, it would probably be better to implement the package as a compiler pass rather than with method overloading, but this is currently beyond me. MCM has some experimental functionality that uses run-time introspection to figure out that there are internal fields of type `Particles`, but this is very brittle and not recommended in general.

This limitation is described in part here  
[https://baggepinnen.github.io/MonteCarloMeasurements.jl/latest/#Limitations-1](https://baggepinnen.github.io/MonteCarloMeasurements.jl/latest/#Limitations-1)

---

<div class="post-metadata">

**Author:** ![matthew3245](https://avatars.discourse-cdn.com/v4/letter/m/ba8739/32.png) [@matthew3245](https://discourse.julialang.org/u/matthew3245)\
**Post date:** [November 6, 2023, 11:21am UTC](https://discourse.julialang.org/t/montecarlomeasurements-jl-object-with-particles-passed-to-function-with-comparison-operator/105843/5 "2023-11-06T11:21:32Z")

</div>

Thanks for the detailed reply - I understand why this is a limitation.

Cheers.

---

<div class="post-metadata">

**Author:** ![lrnv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lrnv/32/19373_2.png) [@lrnv](https://discourse.julialang.org/u/lrnv)\
**Post date:** [November 6, 2023, 12:56pm UTC](https://discourse.julialang.org/t/montecarlomeasurements-jl-object-with-particles-passed-to-function-with-comparison-operator/105843/6 "2023-11-06T12:56:06Z")

</div>

I agree that using full control flow is complicated, and that

```julia
if p > 1 
    ...
end

```

should fail for `p::Particle`. However, using algebra on booleans should work properly: I bet that `p > 1` should provide a Particle with all atoms being `true` or `false`. Then using algebra should work:

```julia
p = p + (p > 1) # this should work

```

If it does not, then maybe adding a few methods to the package might be enough, and you could open an issue for that. Sometimes, algebra on booleans in enough to express a lot of stuff!
