Skip to content

Commit

Permalink
Sink measurement(val::Symbolics.Num, err::Symbolics.Num) overload i…
Browse files Browse the repository at this point in the history
…nto the `Symbolics` extension

The idea is that we first coerce `val` and `err` into same type,
then they are either `AbstractFloat` and are accepted,
or are `Num` and are accepted, or they are then re-promoted to `float`
and hopefully accepted as `AbstractFloat` (or we get stuck
in an infinite recursion...)
  • Loading branch information
LebedevRI committed May 18, 2023
1 parent 3537e25 commit e6e21de
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
2 changes: 2 additions & 0 deletions ext/MeasurementsSymbolicsExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ Measurements._is_symbolic(::Symbolics.Num) = true

Measurements.measurement(val::Symbolics.Num) = Measurements._measurement(val)

Measurements.measurement(val::Symbolics.Num, err::Symbolics.Num) = Measurements._measurement(val, err)

end
11 changes: 6 additions & 5 deletions src/Measurements.jl
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,7 @@ end
measurement(val::AbstractFloat) = _measurement(val)
measurement(val::Real) = measurement(float(val))

function measurement(val::T, err::T) where {T<:Real}
if !(T<:AbstractFloat) && !_is_symbolic(val)
return measurement(promote(float(val), float(err))...)
end
function _measurement(val::T, err::T) where {T<:Real}
newder = empty_der2(val)
if iszero(err)
Measurement{T}(val, err, UInt64(0), newder)
Expand All @@ -107,8 +104,12 @@ function measurement(val::T, err::T) where {T<:Real}
return Measurement{T}(val, err, tag, Derivatives(newder, (val, err, tag)=>one(T)))
end
end
measurement(val::Real, err::Real) = measurement(promote(val, err)...)
measurement(val::T, err::T) where {T<:AbstractFloat} = _measurement(val, err)
measurement(val::T, err::T) where {T<:Real} = measurement(float(val), float(err))
measurement(val::V, err::E) where {V<:Real, E<:Real} = measurement(promote(val, err)...)

measurement(::Missing, ::Union{Real,Missing} = missing) = missing

const ± = measurement

"""
Expand Down

0 comments on commit e6e21de

Please sign in to comment.