Skip to content

Commit

Permalink
Switch to FiniteDifferences for @uncertain
Browse files Browse the repository at this point in the history
  • Loading branch information
giordano committed Nov 30, 2020
1 parent 16da057 commit 533d66a
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 15 deletions.
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ authors = ["Mosè Giordano <[email protected]>"]
version = "2.3.0"

[deps]
Calculus = "49dc2e85-a5d0-5ad3-a950-438e2897f1b9"
FiniteDifferences = "26cc04aa-876d-5657-8c51-4c34ba976000"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
RecipesBase = "3cdcf5f2-1ef4-517c-9805-6587b60abb01"
Requires = "ae029012-a4dd-5104-9daa-d747884805df"

[compat]
Calculus = "0.4.1, 0.5"
FiniteDifferences = "0.11"
RecipesBase = "0.6.0, 0.7, 0.8, 1.0"
Requires = "0.5.0, 1"
julia = "1"
Expand Down
2 changes: 1 addition & 1 deletion docs/Project.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[deps]
AutoGrad = "6710c13c-97f1-543f-91c5-74e8f7d95b35"
Calculus = "49dc2e85-a5d0-5ad3-a950-438e2897f1b9"
Cuba = "8a292aeb-7a57-582c-b821-06e4c11590b1"
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
FiniteDifferences = "26cc04aa-876d-5657-8c51-4c34ba976000"
QuadGK = "1fd47b50-473d-5c70-9696-f719f8f3bcdc"
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"
Expand Down
3 changes: 0 additions & 3 deletions src/Measurements.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ __precompile__()

module Measurements

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

using Requires

# Functions provided by this package and exposed to users
Expand Down
10 changes: 7 additions & 3 deletions src/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
#
### Code:

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

export @uncertain

# This function is to be used by methods of mathematical operations to produce a
Expand Down Expand Up @@ -147,14 +150,14 @@ according to rules of linear error propagation theory.
Function `f` can accept any number of real arguments.
"""
macro uncertain(expr::Expr)
macro uncertain(expr::Expr, p::Int=8)
f = esc(expr.args[1]) # Function name
n = length(expr.args) - 1
if n == 1
a = esc(expr.args[2]) # Argument, of Measurement type
return quote
x = measurement($a)
result($f(x.val), Calculus.derivative($f, x.val), x)
result($f(x.val), FiniteDifferences.central_fdm($(esc(p)), 1)($f, x.val), x)
end
else
a = expr.args[2:end] # Arguments, as an array of expressions
Expand All @@ -163,7 +166,8 @@ macro uncertain(expr::Expr)
argsval =:([]) # Build up the array of values of arguments
[push!(argsval.args, :($(args.args[i]).val)) for i=1:n] # Fill the array
return :( result($f($argsval...),
Calculus.gradient(x -> $f(x...), $argsval),
FiniteDifferences.grad(FiniteDifferences.central_fdm($(esc(p)), 1),
x -> $f(x...), $argsval)[1],
$args) )
end
end
Expand Down
12 changes: 6 additions & 6 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Measurements, SpecialFunctions, QuadGK, Calculus
using Measurements, SpecialFunctions, QuadGK, FiniteDifferences
using Test, LinearAlgebra, Statistics, Unitful

import Base: isapprox
Expand Down Expand Up @@ -295,7 +295,7 @@ end
end
end
for c in (-1 ± 1, 0 ± 1, 1 ± 1)
@test sinc(c) @uncertain(sinc(c)) rtol = 1e-7
@test sinc(c) @uncertain(sinc(c)) atol = 1e-7
@test cosc(c) @uncertain(cosc(c)) rtol = 1e-7
@test Measurements.value(sinc(c) ^ 2 + cosc(c) ^ 2) == 1
end
Expand Down Expand Up @@ -758,11 +758,11 @@ end
@uncertain((x -> QuadGK.quadgk(exp, 0.4, x)[1])(x))
end

@testset "Calculus" begin
@testset "FiniteDifferences" begin
for a in (w, x, y)
@test Calculus.derivative(exp, a + w) exp(a + w)
@test Calculus.derivative(t -> sin(t * x), a) x * cos(a * x)
@test Calculus.derivative(t -> y * cos(t), a) -y * sin(a)
@test FiniteDifferences.central_fdm(5, 1)(exp, a + w) exp(a + w)
@test FiniteDifferences.central_fdm(5, 1)(t -> sin(t * x), a) x * cos(a * x)
@test FiniteDifferences.central_fdm(5, 1)(t -> y * cos(t), a) -y * sin(a)
end
end

Expand Down

0 comments on commit 533d66a

Please sign in to comment.