Skip to content

Add an extension to support ForwardDiff #586

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
fail-fast: false
matrix:
version:
- '1.8'
- '1.9'
- '1'
- 'nightly'
os:
Expand Down
9 changes: 8 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,17 @@ Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
RoundingEmulator = "5eaf0fd0-dfba-4ccb-bf02-d820a40db705"
SetRounding = "3cc68bcd-71a2-5612-b932-767ffbe40ab0"

[weakdeps]
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"

[extensions]
IntervalArithmeticForwardDiffExt = "ForwardDiff"

[compat]
CRlibm = "0.7, 0.8, 1"
EnumX = "1"
FastRounding = "0.2, 0.3"
ForwardDiff = "0.11"
RoundingEmulator = "0.2"
SetRounding = "0.2"
julia = "1.8"
julia = "1.9"
21 changes: 21 additions & 0 deletions ext/IntervalArithmeticForwardDiffExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module IntervalArithmeticForwardDiffExt

using IntervalArithmetic, ForwardDiff
using ForwardDiff: Dual

function Base.:(*)(x::Dual{Tx,Vx,N}, y::Interval{Vy}) where {Tx,Vx<:Real,Vy<:Union{AbstractFloat, Rational},N}
v = x.value * y
V = typeof(v)
Dual{Tx,V,N}(v, ForwardDiff.Partials{N,V}(x.partials.values .* y))
end

@generated function _iszero_tuple(::Type{<:Interval}, tup::NTuple{N,V}) where {N,V}
ex = Expr(:&&, [:(z ≛ tup[$i]) for i=1:N]...)
return quote
z = zero(V)
$(Expr(:meta, :inline))
@inbounds return $ex
end
end

end
1 change: 1 addition & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[deps]
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Polynomials = "f27b6e38-b328-58d1-80ce-0feddd5e7a45"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Expand Down
17 changes: 17 additions & 0 deletions test/autodiff_tests/forwarddiff.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Test
using IntervalArithmetic
using ForwardDiff

@testset "ForwardDiff" begin
x, w = 2.0, interval(-0.5, 0.5)
ϕ(t) = sin(x + (1+t)*w)
ϕ′(t) = cos(x + (1+t)*w) * w
ϕ′′(t) = -sin(x + (1+t)*w) * w * w # note w * w rather than w^2, since FD goes one order at a time
ϕ′′′(t) = -cos(x + (1+t)*w) * w^3
dϕ(t) = ForwardDiff.derivative(ϕ, t)
ddϕ(t) = ForwardDiff.derivative(dϕ, t)
dddϕ(t) = ForwardDiff.derivative(ddϕ, t)
@test ϕ′(0) ≛ dϕ(0)
@test ϕ′′(0) ≛ ddϕ(0)
@test ϕ′′′(0) ≛ dddϕ(0)
end
1 change: 1 addition & 0 deletions test/autodiff_tests/runall.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include("forwarddiff.jl")
3 changes: 3 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ include_test("multidim_tests/multidim.jl")

# ITF1788 tests
include_test("test_ITF1788/run_ITF1788.jl") # TODO fix these tests

# Extensions
include_test("autodiff_tests/runall.jl")