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

[deps]
CRlibm = "96374032-68de-5a5b-8d9e-752f78720389"
DiffRules = "b552c78f-8df3-52c6-915a-8e097449b14b"
FastRounding = "fa42c844-2597-5d31-933b-ebd51ab2693f"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Markdown = "d6f4376e-aef5-505a-96c1-9c027394607a"
Expand All @@ -16,8 +17,9 @@ StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"

[compat]
CRlibm = "0.7, 0.8, 1"
IntervalContractors = "0.4"
DiffRules = "1.0"
FastRounding = "0.2, 0.3"
IntervalContractors = "0.4"
Polynomials = "0.7, 1, 2, 3"
RecipesBase = "1.0"
RoundingEmulator = "0.2"
Expand All @@ -26,10 +28,11 @@ StaticArrays = "0.8, 0.9, 0.10, 0.11, 0.12, 1.0"
julia = "1.5"

[extras]
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
IntervalContractors = "15111844-de3b-5229-b4ba-526f2f385dc9"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Polynomials = "f27b6e38-b328-58d1-80ce-0feddd5e7a45"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["IntervalContractors", "LinearAlgebra", "Test", "Polynomials"]
test = ["ForwardDiff", "IntervalContractors", "LinearAlgebra", "Test", "Polynomials"]
3 changes: 3 additions & 0 deletions src/IntervalArithmetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ import Base.MPFR: MPFRRoundUp, MPFRRoundDown, MPFRRoundNearest, MPFRRoundToZero,

import .Broadcast: broadcasted

import DiffRules._abs_deriv

export
AbstractInterval, Interval,
interval,
Expand Down Expand Up @@ -108,6 +110,7 @@ include("intervals/intervals.jl")
include("multidim/multidim.jl")
include("bisect.jl")
include("decorations/decorations.jl")
include("ad.jl")

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

@testset "AD" begin
@test ForwardDiff.derivative(abs, -2.0 .. 2.0) == -1.0 .. 1.0
agerlach marked this conversation as resolved.
Show resolved Hide resolved
@test ForwardDiff.derivative(abs, 1.0 .. 2.0) == 1.0 .. 1.0
@test ForwardDiff.derivative(abs, -2.0 .. -1.0) == -1.0 .. -1.0
@test ForwardDiff.derivative(abs, Interval(0.0)) == Interval(1.0)
@test ForwardDiff.derivative(abs, -2.0 .. 0.0) == -1.0 .. 1.0
@test ForwardDiff.derivative(abs, 0.0 .. 2.0) == Interval(1.0)

# Test proper handeling at abs(0)
@test ForwardDiff.hessian(t -> abs(t[1])^2, [Interval(0.0)])[1] == Interval(2.0)
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ include("interval_tests/intervals.jl")
include("decoration_tests/decoration_tests.jl")

include("rand.jl")
include("interval_tests/ad.jl")

# Display tests:
include("display_tests/display.jl")
Expand Down