I have the following scenario: I want to propagate the uncertainty of the positions of particles in a simulation. At each time step, I compute the forces, a computation that starts with setting the forces to zero. The code reads like:
forces .= zero(T)
The problem I’m having is that zero(T)
zeroes the uncertainty if T
is a measurement:
julia> x = measurement(1.0,0.1)
1.0 ± 0.1
julia> x = zero(x)
0.0 ± 0.0
And thus the uncertainty is not propagated through the force calculation. Then I thought: ok, lets zero the forces by subtracting the same value, but subtraction of the same value zeroes the uncertainty as well:
julia> x = measurement(1.0,0.1)
1.0 ± 0.1
julia> x - x
0.0 ± 0.0
I would need actually something like this:
julia> x = measurement(1.0,0.1)
1.0 ± 0.1
julia> y = measurement(x.val,0.)
1.0 ± 0.0
julia> x - y
0.0 ± 0.1
But if my function is expected to receive more arbitrary types, like static vectors of measurements:
julia> x = SVector{2,Measurement{Float64}}(measurement(1.,0.1),measurement(1.,0.1))
2-element SVector{2, Measurement{Float64}} with indices SOneTo(2):
1.0 ± 0.1
1.0 ± 0.1
julia> zero(x)
2-element SVector{2, Measurement{Float64}} with indices SOneTo(2):
0.0 ± 0.0
0.0 ± 0.0
supporting this becomes cumbersome and way too specific.
Is there a proper way to avoid the zeroing of the uncertainty of a measurement
that is general enough to replace zero(T)
in such a function body?
As a side note, is this expected?
julia> x = measurement(1.0,0.1)
1.0 ± 0.1
julia> y = deepcopy(x)
1.0 ± 0.1
julia> x - y
0.0 ± 0.0
# I was expecting this as a result:
julia> x = measurement(1.0,0.1)
1.0 ± 0.1
julia> y = measurement(1.0,0.1)
1.0 ± 0.1
julia> x - y
0.0 ± 0.14