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 Feb 24, 2021
1 parent e18b75a commit eb16b51
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 23 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.5.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.5, 0.12"
RecipesBase = "0.6.0, 0.7, 0.8, 1.0"
Requires = "0.5.0, 1"
julia = "1"
Expand Down
26 changes: 19 additions & 7 deletions benchmark/Manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@ git-tree-sha1 = "9e62e66db34540a0c919d72172cc2f642ac71260"
uuid = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
version = "0.5.0"

[[Calculus]]
deps = ["LinearAlgebra"]
git-tree-sha1 = "f641eb0a4f00c343bbc32346e1217b86f3ce9dad"
uuid = "49dc2e85-a5d0-5ad3-a950-438e2897f1b9"
version = "0.5.1"

[[ChainRulesCore]]
deps = ["Compat", "LinearAlgebra", "SparseArrays"]
git-tree-sha1 = "de4f08843c332d355852721adb1592bce7924da3"
Expand Down Expand Up @@ -65,6 +59,12 @@ uuid = "8ba89e20-285c-5b6f-9357-94700520ee1b"
deps = ["ArgTools", "LibCURL", "NetworkOptions"]
uuid = "f43a241f-c20a-4ad4-852c-f6b1247861c6"

[[FiniteDifferences]]
deps = ["ChainRulesCore", "LinearAlgebra", "Printf", "Random", "Richardson", "StaticArrays"]
git-tree-sha1 = "a1c802a1407e4ff5a4733dedb8ed40bd6fb46021"
uuid = "26cc04aa-876d-5657-8c51-4c34ba976000"
version = "0.12.2"

[[InteractiveUtils]]
deps = ["Markdown"]
uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
Expand Down Expand Up @@ -121,7 +121,7 @@ deps = ["Artifacts", "Libdl"]
uuid = "c8ffd9c3-330d-5841-b78e-0817d7145fa1"

[[Measurements]]
deps = ["Calculus", "LinearAlgebra", "RecipesBase", "Requires"]
deps = ["FiniteDifferences", "LinearAlgebra", "RecipesBase", "Requires"]
path = ".."
uuid = "eff96d63-e80a-5855-80a2-b1b0885c5ab7"
version = "2.5.0"
Expand Down Expand Up @@ -197,6 +197,12 @@ git-tree-sha1 = "cfbac6c1ed70c002ec6361e7fd334f02820d6419"
uuid = "ae029012-a4dd-5104-9daa-d747884805df"
version = "1.1.2"

[[Richardson]]
deps = ["LinearAlgebra"]
git-tree-sha1 = "e03ca566bec93f8a3aeb059c8ef102f268a38949"
uuid = "708f8203-808e-40c0-ba2d-98a6953ed40d"
version = "1.4.0"

[[SHA]]
uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce"

Expand All @@ -220,6 +226,12 @@ git-tree-sha1 = "5919936c0e92cff40e57d0ddf0ceb667d42e5902"
uuid = "276daf66-3868-5448-9aa4-cd146d93841b"
version = "1.3.0"

[[StaticArrays]]
deps = ["LinearAlgebra", "Random", "Statistics"]
git-tree-sha1 = "9da72ed50e94dbff92036da395275ed114e04d49"
uuid = "90137ffa-7385-5640-81b9-e52037218182"
version = "1.0.1"

[[Statistics]]
deps = ["LinearAlgebra", "SparseArrays"]
uuid = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
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=4)
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
14 changes: 7 additions & 7 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 @@ -755,16 +755,16 @@ end
@test QuadGK.quadgk(cos, x, y)[1]
@uncertain(((x,y) -> QuadGK.quadgk(cos, x, y)[1])(x, y))
@test QuadGK.quadgk(sin, -y, y)[1]
@uncertain((x -> QuadGK.quadgk(sin, -x, x)[1])(y)) atol = 1e-10
@uncertain((x -> QuadGK.quadgk(sin, -x, x)[1])(y)) atol = 1.5e-10
@test QuadGK.quadgk(exp, 0.4, x)[1]
@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 eb16b51

Please sign in to comment.