Skip to content

Commit 253dfc8

Browse files
committed
Switch to FiniteDifferences for @uncertain
1 parent 8475efb commit 253dfc8

File tree

6 files changed

+36
-23
lines changed

6 files changed

+36
-23
lines changed

Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ authors = ["Mosè Giordano <[email protected]>"]
44
version = "2.5.0"
55

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

1212
[compat]
13-
Calculus = "0.4.1, 0.5"
13+
FiniteDifferences = "0.11.5, 0.12"
1414
RecipesBase = "0.6.0, 0.7, 0.8, 1.0"
1515
Requires = "0.5.0, 1"
1616
julia = "1"

benchmark/Manifest.toml

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,6 @@ git-tree-sha1 = "9e62e66db34540a0c919d72172cc2f642ac71260"
2626
uuid = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
2727
version = "0.5.0"
2828

29-
[[Calculus]]
30-
deps = ["LinearAlgebra"]
31-
git-tree-sha1 = "f641eb0a4f00c343bbc32346e1217b86f3ce9dad"
32-
uuid = "49dc2e85-a5d0-5ad3-a950-438e2897f1b9"
33-
version = "0.5.1"
34-
3529
[[ChainRulesCore]]
3630
deps = ["Compat", "LinearAlgebra", "SparseArrays"]
3731
git-tree-sha1 = "de4f08843c332d355852721adb1592bce7924da3"
@@ -82,6 +76,12 @@ uuid = "8ba89e20-285c-5b6f-9357-94700520ee1b"
8276
deps = ["ArgTools", "LibCURL", "NetworkOptions"]
8377
uuid = "f43a241f-c20a-4ad4-852c-f6b1247861c6"
8478

79+
[[FiniteDifferences]]
80+
deps = ["ChainRulesCore", "LinearAlgebra", "Printf", "Random", "Richardson", "StaticArrays"]
81+
git-tree-sha1 = "a1c802a1407e4ff5a4733dedb8ed40bd6fb46021"
82+
uuid = "26cc04aa-876d-5657-8c51-4c34ba976000"
83+
version = "0.12.2"
84+
8585
[[Future]]
8686
deps = ["Random"]
8787
uuid = "9fa8497b-333b-5362-9e8d-4d0656e87820"
@@ -172,7 +172,7 @@ deps = ["Artifacts", "Libdl"]
172172
uuid = "c8ffd9c3-330d-5841-b78e-0817d7145fa1"
173173

174174
[[Measurements]]
175-
deps = ["Calculus", "LinearAlgebra", "RecipesBase", "Requires"]
175+
deps = ["FiniteDifferences", "LinearAlgebra", "RecipesBase", "Requires"]
176176
path = ".."
177177
uuid = "eff96d63-e80a-5855-80a2-b1b0885c5ab7"
178178
version = "2.5.0"
@@ -248,6 +248,12 @@ git-tree-sha1 = "cfbac6c1ed70c002ec6361e7fd334f02820d6419"
248248
uuid = "ae029012-a4dd-5104-9daa-d747884805df"
249249
version = "1.1.2"
250250

251+
[[Richardson]]
252+
deps = ["LinearAlgebra"]
253+
git-tree-sha1 = "e03ca566bec93f8a3aeb059c8ef102f268a38949"
254+
uuid = "708f8203-808e-40c0-ba2d-98a6953ed40d"
255+
version = "1.4.0"
256+
251257
[[SHA]]
252258
uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce"
253259

@@ -283,6 +289,12 @@ git-tree-sha1 = "5919936c0e92cff40e57d0ddf0ceb667d42e5902"
283289
uuid = "276daf66-3868-5448-9aa4-cd146d93841b"
284290
version = "1.3.0"
285291

292+
[[StaticArrays]]
293+
deps = ["LinearAlgebra", "Random", "Statistics"]
294+
git-tree-sha1 = "9da72ed50e94dbff92036da395275ed114e04d49"
295+
uuid = "90137ffa-7385-5640-81b9-e52037218182"
296+
version = "1.0.1"
297+
286298
[[Statistics]]
287299
deps = ["LinearAlgebra", "SparseArrays"]
288300
uuid = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"

docs/Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
[deps]
22
AutoGrad = "6710c13c-97f1-543f-91c5-74e8f7d95b35"
3-
Calculus = "49dc2e85-a5d0-5ad3-a950-438e2897f1b9"
43
Cuba = "8a292aeb-7a57-582c-b821-06e4c11590b1"
54
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
5+
FiniteDifferences = "26cc04aa-876d-5657-8c51-4c34ba976000"
66
QuadGK = "1fd47b50-473d-5c70-9696-f719f8f3bcdc"
77
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"
88
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"

src/Measurements.jl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@ __precompile__()
2121

2222
module Measurements
2323

24-
# Calculus is used to calculate numerical derivatives in "@uncertain" macro.
25-
using Calculus
26-
2724
using Requires
2825

2926
# Functions provided by this package and exposed to users

src/math.jl

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
#
2626
### Code:
2727

28+
# FiniteDifferences is used to calculate numerical derivatives in "@uncertain" macro.
29+
using FiniteDifferences
30+
2831
export @uncertain
2932

3033
# This function is to be used by methods of mathematical operations to produce a
@@ -147,14 +150,14 @@ according to rules of linear error propagation theory.
147150
148151
Function `f` can accept any number of real arguments.
149152
"""
150-
macro uncertain(expr::Expr)
153+
macro uncertain(expr::Expr, p::Int=4)
151154
f = esc(expr.args[1]) # Function name
152155
n = length(expr.args) - 1
153156
if n == 1
154157
a = esc(expr.args[2]) # Argument, of Measurement type
155158
return quote
156159
x = measurement($a)
157-
result($f(x.val), Calculus.derivative($f, x.val), x)
160+
result($f(x.val), FiniteDifferences.central_fdm($(esc(p)), 1)($f, x.val), x)
158161
end
159162
else
160163
a = expr.args[2:end] # Arguments, as an array of expressions
@@ -163,7 +166,8 @@ macro uncertain(expr::Expr)
163166
argsval =:([]) # Build up the array of values of arguments
164167
[push!(argsval.args, :($(args.args[i]).val)) for i=1:n] # Fill the array
165168
return :( result($f($argsval...),
166-
Calculus.gradient(x -> $f(x...), $argsval),
169+
FiniteDifferences.grad(FiniteDifferences.central_fdm($(esc(p)), 1),
170+
x -> $f(x...), $argsval)[1],
167171
$args) )
168172
end
169173
end

test/runtests.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Measurements, SpecialFunctions, QuadGK, Calculus
1+
using Measurements, SpecialFunctions, QuadGK, FiniteDifferences
22
using Test, LinearAlgebra, Statistics, Unitful
33

44
import Base: isapprox
@@ -295,7 +295,7 @@ end
295295
end
296296
end
297297
for c in (-1 ± 1, 0 ± 1, 1 ± 1)
298-
@test sinc(c) @uncertain(sinc(c)) rtol = 1e-7
298+
@test sinc(c) @uncertain(sinc(c)) atol = 1e-7
299299
@test cosc(c) @uncertain(cosc(c)) rtol = 1e-7
300300
@test Measurements.value(sinc(c) ^ 2 + cosc(c) ^ 2) == 1
301301
end
@@ -755,16 +755,16 @@ end
755755
@test QuadGK.quadgk(cos, x, y)[1]
756756
@uncertain(((x,y) -> QuadGK.quadgk(cos, x, y)[1])(x, y))
757757
@test QuadGK.quadgk(sin, -y, y)[1]
758-
@uncertain((x -> QuadGK.quadgk(sin, -x, x)[1])(y)) atol = 1e-10
758+
@uncertain((x -> QuadGK.quadgk(sin, -x, x)[1])(y)) atol = 1.5e-10
759759
@test QuadGK.quadgk(exp, 0.4, x)[1]
760760
@uncertain((x -> QuadGK.quadgk(exp, 0.4, x)[1])(x))
761761
end
762762

763-
@testset "Calculus" begin
763+
@testset "FiniteDifferences" begin
764764
for a in (w, x, y)
765-
@test Calculus.derivative(exp, a + w) exp(a + w)
766-
@test Calculus.derivative(t -> sin(t * x), a) x * cos(a * x)
767-
@test Calculus.derivative(t -> y * cos(t), a) -y * sin(a)
765+
@test FiniteDifferences.central_fdm(5, 1)(exp, a + w) exp(a + w)
766+
@test FiniteDifferences.central_fdm(5, 1)(t -> sin(t * x), a) x * cos(a * x)
767+
@test FiniteDifferences.central_fdm(5, 1)(t -> y * cos(t), a) -y * sin(a)
768768
end
769769
end
770770

0 commit comments

Comments
 (0)