Skip to content

Commit

Permalink
Define @Uncertain macro to calculate uncertainty for any function
Browse files Browse the repository at this point in the history
Limitation: function must take only one real argument.

The derivative is calculated using Calculus package.  We need numerical
derivatives because we want to support any possible function, not only
those implemented in Julia, so I don’t think automatic differentiation
is an option in this case.

Fixes issue #4.
  • Loading branch information
giordano committed Jun 13, 2016
1 parent 88005fc commit d68c4d2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
1 change: 1 addition & 0 deletions REQUIRE
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
julia 0.4
Calculus
20 changes: 19 additions & 1 deletion src/Measurements.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@

module Measurements

# This is used to calculate numerical derivatives in "@uncertain" macro.
using Calculus

# Functions to handle new type
import Base: show, convert, promote_rule, float

# Functions provided by this package and exposed to users
export Measurement, ±, stdscore, weightedmean
export Measurement, ±, stdscore, weightedmean, @uncertain

# Define the new type
immutable Measurement{T<:AbstractFloat} <: AbstractFloat
Expand Down Expand Up @@ -62,6 +65,21 @@ function show(io::IO, measure::Measurement)
print(io, measure.val, " ± ", measure.err)
end

# @uncertain macro
"""
@uncertain f(value ± stddev)
A macro to calculate \$f(value) ± uncertainty\$, with \$uncertainty\$ derived
from \$stddev\$ according to rules of linear error propagation theory. Function
\$f\$ must accept only one real argument, the type of the argument provided must
be `Measurement`.
"""
macro uncertain(expr::Expr)
f = esc(expr.args[1]) # Function name
a = esc(expr.args[2]) # Argument, of Measurement type
return :( Measurement($f($a.val), abs(Calculus.derivative($f, $a.val)*$a.err)) )
end

# Standard Score
"""
stdscore(measure::Measurement, expected_value::Real) -> standard_score
Expand Down
8 changes: 8 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ test_approx_eq{T1<:Real,T2<:Real}(a::Complex{Measurement{T1}}, b::Measurement{T2
test_approx_eq(a, complex(b))
test_approx_eq{T1<:Real,T2<:Real}(a::Measurement{T1}, b::Complex{Measurement{T2}}) =
test_approx_eq(complex(a), b)
test_approx_eq_eps(a::Measurement, b::Measurement, tol::Real) =
(@test_approx_eq_eps(a.val, b.val, tol) ; @test_approx_eq_eps(a.err, b.err, tol))

w = -0.5 ± 0.03
x = 3 ± 0.1
Expand Down Expand Up @@ -320,3 +322,9 @@ test_approx_eq(mean((w, x, y)), (w + x + y)/3)

# sort
@test sort([y, w, x]) == [w, x, y]

##### Test @uncertain macro
test_approx_eq_eps(@uncertain(tan(x)), tan(x), 2e-11)
test_approx_eq_eps(@uncertain((a -> a + a + a)(x)), 3x, 3e-12)
test_approx_eq(@uncertain(zeta(x)),
Measurement(1.2020569031595951, 0.019812624290876782))

0 comments on commit d68c4d2

Please sign in to comment.