Skip to content
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

add AD hook for abs() per DiffRules #565

Closed
wants to merge 6 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: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ version = "0.21.1"

[deps]
CRlibm = "96374032-68de-5a5b-8d9e-752f78720389"
DiffRules = "b552c78f-8df3-52c6-915a-8e097449b14b"
EnumX = "4e289a0a-7415-4d19-859d-a7e5c4648b56"
FastRounding = "fa42c844-2597-5d31-933b-ebd51ab2693f"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Expand All @@ -13,6 +14,7 @@ SetRounding = "3cc68bcd-71a2-5612-b932-767ffbe40ab0"

[compat]
CRlibm = "0.7, 0.8, 1"
DiffRules = "1"
EnumX = "1"
FastRounding = "0.2, 0.3"
RoundingEmulator = "0.2"
Expand Down
2 changes: 2 additions & 0 deletions src/IntervalArithmetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ export bisect
include("decorations/decorations.jl")
export decoration, DecoratedInterval, com, dac, def, trv, ill

include("ad.jl")

include("rand.jl")

include("parsing.jl")
Expand Down
11 changes: 11 additions & 0 deletions src/ad.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function _abs_deriv(x::Interval{T}) where T
agerlach marked this conversation as resolved.
Show resolved Hide resolved
if inf(x) == 0
return Interval{T}(1)
elseif sup(x) == 0
return Interval{T}(-1, 1)
else
return sign(x)
end
end

_abs_deriv(x::DecoratedInterval) = DecoratedInterval(_abs_deriv(interval(x)), decoration(x))
16 changes: 16 additions & 0 deletions test/interval_tests/ad.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using IntervalArithmetic, ForwardDiff
using Test

@testset "AD" begin
for F in (Interval, DecoratedInterval)
@test ForwardDiff.derivative(abs, F(-2.0 .. 2.0)) == F(-1.0 .. 1.0)
@test ForwardDiff.derivative(abs, F(1.0 .. 2.0)) == F(1.0 .. 1.0)
@test ForwardDiff.derivative(abs, F(-2.0 .. -1.0)) == F(-1.0 .. -1.0)
@test ForwardDiff.derivative(abs, F(0.0)) == F(1.0)
@test ForwardDiff.derivative(abs, F(-2.0 .. 0.0)) == F(-1.0 .. 1.0)
@test ForwardDiff.derivative(abs, F(0.0 .. 2.0)) == F(1.0)

# Test proper handeling at abs(0)
@test ForwardDiff.hessian(t -> abs(t[1])^2, [F(0.0)])[1] == F(2.0)
end
end
11 changes: 11 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ end
include_test("interval_tests/intervals.jl")
include_test("decoration_tests/decoration_tests.jl")

include_test("interval_tests/ad.jl")

include_test("rand.jl")

# Display tests:
Expand All @@ -24,3 +26,12 @@ include_test("multidim_tests/multidim.jl")

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

# Display tests:
include_test("display_tests/display.jl")

# Multidim tests
include_test("multidim_tests/multidim.jl")

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