JuliaPhysics / Measurements.jl

Error propagation calculator and library for physical measurements. It supports real and complex numbers with uncertainty, arbitrary precision calculations, operations with arrays, and numerical integration.
https://juliaphysics.github.io/Measurements.jl/stable/
MIT License
486 stars 37 forks source link

Proposal: new types for independent measurements and dependent measurements #7

Open giordano opened 7 years ago

giordano commented 7 years ago

We could make Measurement type abstract, and define two concrete types, IndependentMeasurement and DependentMeasurement (the latter may be an abuse of language but gives the idea):

abstract Measurement{T<:AbstractFloat} <: AbstractFloat

immutable IndependentMeasurement{T<:AbstractFloat} <: Measurement{T}
    val::T
    err::T
    tag::Float64
end

immutable DependentMeasurement{T<:AbstractFloat} <: Measurement{T}
    val::T
    err::T
    der::Derivatives{Tuple{T, T, Float64}, T}
end

measurement function would create IndependentMeasurement objects, while result function in src/math.jl would create DependentMeasurement objects. This function could have a specialized method for IndependentMeasurement only arguments, which could be fairly efficient.

Ideally, there would not be user-visible changes, apart from performance improvements.

I'm not sure this proposal will actually improve performance, I'm leaving this as a memo for me open for discussion.

giordano commented 7 years ago

Different approach in dispatch-on-constant branch: no new type, the only change is the dispatch of result for 1-arg functions is based on constants (Val{...}), but this isn't very fast. The branch is left there only as a proof-of-concept.

alusiani commented 3 years ago

We could make Measurement type abstract, and define two concrete types, IndependentMeasurement and DependentMeasurement (the latter may be an abuse of language but gives the idea):

I support this evolution. As far as I understand, the Python uncertainties packages works in this way. I have written code to do elaborations of uncertain and correlated quantities in R and resorted to the same concepts. It seems to me the most straightforward way to easily track down correlations when doing elaborations using quantities that themselves depend on overlapping sets of uncertain variables.

giordano commented 3 years ago

The reason why that I never went on with this idea is that it creates lots of type-instabilities, or it makes it more likely to have inhomogeneous containers which include both IndependentMeasurements and DependentMeasurements, which is really not a good idea performance-wise.

If one wants to distinguish an "independent" measure from a derived quantity, I believe traits would be a better approach.