From ebb2e344f3b0f26ddd5bded64d07c1864a7d93c8 Mon Sep 17 00:00:00 2001 From: David Sanders Date: Sat, 2 Apr 2016 08:44:14 -0400 Subject: [PATCH] Replace autodiff by ForwardDiff.jl package --- REQUIRE | 1 + src/ValidatedNumerics.jl | 2 +- src/root_finding/automatic_differentiation.jl | 90 ------------------- src/root_finding/root_finding.jl | 6 +- test/root_finding_tests/auto_diff.jl | 58 ------------ test/root_finding_tests/root_finding.jl | 2 + test/runtests.jl | 1 - 7 files changed, 9 insertions(+), 151 deletions(-) delete mode 100644 src/root_finding/automatic_differentiation.jl delete mode 100644 test/root_finding_tests/auto_diff.jl diff --git a/REQUIRE b/REQUIRE index b97990e..10c5300 100644 --- a/REQUIRE +++ b/REQUIRE @@ -2,3 +2,4 @@ julia 0.4 CRlibm 0.2.2 Compat 0.7.11 FixedSizeArrays +ForwardDiff diff --git a/src/ValidatedNumerics.jl b/src/ValidatedNumerics.jl index aed9694..d00c539 100644 --- a/src/ValidatedNumerics.jl +++ b/src/ValidatedNumerics.jl @@ -54,7 +54,7 @@ export ## Root finding export newton, krawczyk, - differentiate, D, # should these be exported? + derivative, # reexport derivative from ForwardDiff Root, is_unique, find_roots, find_roots_midpoint diff --git a/src/root_finding/automatic_differentiation.jl b/src/root_finding/automatic_differentiation.jl deleted file mode 100644 index be1bff1..0000000 --- a/src/root_finding/automatic_differentiation.jl +++ /dev/null @@ -1,90 +0,0 @@ -# This file is part of the ValidatedNumerics.jl package; MIT licensed - -## Automatic differentiation -## Represents the jet of a function u at the point a by (u(a), u'(a)) - -immutable Jet{T} <: Number # is this really a Number? Then promotion rules work - val::T # value u(a) - der::T # derative u'(a) -end - -#Jet{T}(a::T, b::T) = Jet{T}(a, b) -#Jet(a, b) = Jet{T}(a,b) - -# import Base: -# convert, promote_rule, zero, one - -convert(::Type{Jet}, c::Real) = Jet(c) -promote_rule{T<:Real, S<:Real}(::Type{Jet{T}}, ::Type{S}) = Jet - -# Constants: -Jet(c::Real) = Jet(c, zero(c)) - -zero(x::Jet) = Jet(zero(x.val), zero(x.der)) -one(x::Jet) = Jet(one(x.val), zero(x.der)) - - -# Arithmetic between two Jets -+(x::Jet, y::Jet) = Jet(x.val + y.val, x.der + y.der) --(x::Jet, y::Jet) = Jet(x.val - y.val, x.der - y.der) -*(x::Jet, y::Jet) = Jet(x.val*y.val, x.val*y.der + y.val*x.der) - -function /(x::Jet, y::Jet) - quotient = x.val / y.val - der = (x.der - quotient*y.der) / y.val - - Jet(quotient, der) -end - --(x::Jet) = Jet(-x.val, -x.der) - - -# Elementary functions - -sin(x::Jet) = Jet(sin(x.val), x.der*cos(x.val)) -cos(x::Jet) = Jet(cos(x.val), -x.der*sin(x.val)) -tan(x::Jet) = Jet(tan(x.val), x.der / cos(x.val)^2 ) - -asin(x::Jet) = Jet(asin(x.val), x.der / sqrt(1-x.val^2)) -acos(x::Jet) = Jet(acos(x.val), -x.der / sqrt(1-x.val^2)) -atan(x::Jet) = Jet(atan(x.val), x.der / (1+x.val^2)) - -exp(x::Jet) = Jet(exp(x.val), x.der * exp(x.val)) -log(x::Jet) = Jet(log(x.val), x.der / x.val) - -function ^(x::Jet, n::Integer) - n == 0 && return one(x) - n == 1 && return x - Jet( (x.val)^n, n * (x.val)^(n-1) * x.der ) -end - -^(x::Jet, r::Rational) = (x^(r.num))^(1/r.den) -^(x::Jet, y::Real) = Jet( (x.val)^y, y * (x.val)^(y-1) * x.der ) - -differentiate(f::Function, a::Number) = f( Jet(a, one(a)) ).der - -differentiate(f::Function) = x -> differentiate(f, x) -# caution: anonymous functions are currently slow (v0.3 of Julia) - -const D = differentiate - -function jacobian(f, a) - - f1(x) = f(x)[1] - f2(x) = f(x)[2] - - f11(x1) = f1([x1, a[2]]) - J11 = D(f11, a[1]) - - f12(x2) = f1([a[1], x2]) - J12 = D(f12, a[2]) - - f21(x1) = f2([x1, a[2]]) - J21 = D(f21, a[1]) - - f22(x2) = f2([a[1], x2]) - J22 = D(f22, a[2]) - - [J11 J12; J21 J22] - -end diff --git a/src/root_finding/root_finding.jl b/src/root_finding/root_finding.jl index bc7f511..4ef75f7 100644 --- a/src/root_finding/root_finding.jl +++ b/src/root_finding/root_finding.jl @@ -1,5 +1,9 @@ # This file is part of the ValidatedNumerics.jl package; MIT licensed +using ForwardDiff + +const D = ForwardDiff.derivative + immutable Root{T<:Real} interval::Interval{T} root_type::Symbol @@ -13,7 +17,7 @@ is_unique{T}(root::Root{T}) = root.root_type == :unique ⊆(a::Root, b::Root) = a.interval ⊆ b.interval -include("automatic_differentiation.jl") +# include("automatic_differentiation.jl") include("newton.jl") include("krawczyk.jl") diff --git a/test/root_finding_tests/auto_diff.jl b/test/root_finding_tests/auto_diff.jl deleted file mode 100644 index f0b648f..0000000 --- a/test/root_finding_tests/auto_diff.jl +++ /dev/null @@ -1,58 +0,0 @@ -# AutoDiff tests - -using ValidatedNumerics -using FactCheck - -Jet = ValidatedNumerics.Jet - -facts("Jet tests") do - a = Jet(1, 1) - - @fact zero(a) == Jet(0, 0) --> true - @fact one(a) == Jet(1, 0) --> true - @fact -a == Jet(-1, -1) --> true - @fact +a == 2a - a --> true - -end - -facts("AutoDiff tests") do - - @fact D(x -> x^2, 3) == 6 --> true - @fact D(x -> (x+1)/(x-2), 1) == -3.0 --> true - - f(x) = sin(2x) - x - - for a in (-3, 3, 7, 11) - @fact D(f, a) == 2*cos(2a) - 1 --> true - @fact D(tan, a) == 1. / (cos(a)^2) --> true - - if a > 0. - @fact D(log, a) == 1. / a --> true - @fact D(x -> x^(1//2), a) == 0.5*a^(-0.5) --> true - @fact D(x -> x^0.5, a) == 0.5*a^(-0.5) --> true - - end - - end - - for a in (-0.3, 0.0, 0.1) - @fact D(acos, a) == -1. / sqrt(1 - a^2) --> true - @fact D(atan, a) == 1. / (1 + a^2) --> true - end - - - -end - - -jacobian = ValidatedNumerics.jacobian - -facts("Jacobian tests") do - f(xvec) = ( (x,y)=xvec; [x+y, x-y] ) - - for a in ([1, 2], [3, 4]) - @fact jacobian(f, a) == [1 1; 1 -1] --> true - end - -end - diff --git a/test/root_finding_tests/root_finding.jl b/test/root_finding_tests/root_finding.jl index f173af3..9f634b0 100644 --- a/test/root_finding_tests/root_finding.jl +++ b/test/root_finding_tests/root_finding.jl @@ -1,6 +1,8 @@ using ValidatedNumerics using FactCheck +const D = ValidatedNumerics.derivative + include("wilkinson.jl") diff --git a/test/runtests.jl b/test/runtests.jl index 5002db9..220133e 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -19,7 +19,6 @@ include("multidim_tests/multidim.jl") # Root-finding tests: -include("root_finding_tests/auto_diff.jl") include("root_finding_tests/root_finding.jl") # Multidimensional boxes tests: