Skip to content

Commit

Permalink
Merge pull request #629 from Kolaru/fowarddiff_pow
Browse files Browse the repository at this point in the history
ForwardDiff extension for power
  • Loading branch information
Kolaru authored Feb 23, 2024
2 parents 98a235b + ff4307b commit 5275e5c
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 9 deletions.
5 changes: 4 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
name = "IntervalArithmetic"
uuid = "d1acc4aa-44c8-5952-acd4-ba5d80a2a253"
repo = "https://github.com/JuliaIntervals/IntervalArithmetic.jl.git"
version = "0.22.7"
version = "0.22.8"

[deps]
CRlibm_jll = "4e9b3aee-d8a1-5a3d-ad8b-7d824db253f0"
RoundingEmulator = "5eaf0fd0-dfba-4ccb-bf02-d820a40db705"

[weakdeps]
DiffRules = "b552c78f-8df3-52c6-915a-8e097449b14b"
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
RecipesBase = "3cdcf5f2-1ef4-517c-9805-6587b60abb01"

[extensions]
IntervalArithmeticDiffRulesExt = "DiffRules"
IntervalArithmeticForwardDiffExt = "ForwardDiff"
IntervalArithmeticRecipesBaseExt = "RecipesBase"

[compat]
CRlibm_jll = "1"
DiffRules = "1"
ForwardDiff = "0.10"
RecipesBase = "1"
RoundingEmulator = "0.2"
julia = "1.9"
51 changes: 51 additions & 0 deletions ext/IntervalArithmeticForwardDiffExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
module IntervalArithmeticForwardDiffExt

using IntervalArithmetic, ForwardDiff
using ForwardDiff: Dual, , value, partials

function isconstant_interval(x)
all(isthinzero.(values(partials(x))))
end

function Base.:(^)(x::Dual{Txy, <:Interval}, y::Dual{Txy, <:Interval}) where Txy
vx, vy = value(x), value(y)
expv = vx^vy
powval = vy * vx^(vy - interval(1))
if isconstant_interval(y)
logval = one(expv)
elseif isthinzero(vx) && inf(vy) > 0
logval = zero(vx)
else
logval = expv * log(vx)
end
new_partials = ForwardDiff._mul_partials(partials(x), partials(y), powval, logval)
return Dual{Txy}(expv, new_partials)
end

function Base.:(^)(x::Dual{Tx, <:Interval}, y::Dual{Ty, <:Interval}) where {Tx, Ty}
if Ty Tx
return x^value(y)
else
return value(x)^y
end
end

function Base.:(^)(x::Dual{Tx, <:Interval}, y::Interval) where Tx
v = value(x)
expv = v^y
if isthinzero(y) || isconstant_interval(x)
new_partials = zero(partials(x))
else
new_partials = partials(x) * y * v^(y - interval(1))
end
return Dual{Tx}(expv, new_partials)
end

function Base.:(^)(x::Interval, y::Dual{Ty, <:Interval}) where Ty
v = value(y)
expv = x^v
deriv = (isthinzero(x) && inf(v) > 0) ? zero(expv) : expv*log(x)
return Dual{Ty}(expv, deriv * partials(y))
end

end
46 changes: 39 additions & 7 deletions test/interval_tests/forwarddiff.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ end
@test ForwardDiff.derivative(abs, interval(-2, 2)) === interval(-1, 1, trv)

f(x) = abs(x)^interval(2)
@test_broken ForwardDiff.derivative(f, interval(-1, 1)) === interval(-2, 2, trv)
@test ForwardDiff.derivative(f, interval(-1, 1)) === interval(-2, 2, trv)

g(x) = abs(x)^2
@test ForwardDiff.derivative(g, interval(-1, 1) ) === interval(convert(Interval{Float64}, -2), convert(Interval{Float64}, 2), trv)
Expand Down Expand Up @@ -56,12 +56,44 @@ end
end

@testset "Power" begin
f(x) = interval(2)^x
f′(x) = log(interval(2)) * f(x)
df(t) = ForwardDiff.derivative(f, t)
fxy(xy) = xy[1]^xy[2]

# g(x) = 2^x # not guaranteed
for x in [0.0, 1.1, 2.2]
for y in [-3.3, 0.0, 4.4]
fx(xx) = xx^y
fxi(xx) = xx^interval(y)
fy(yy) = x^yy
fyi(yy) = interval(x)^yy

@test f′(0) === df(0)
dfdx = ForwardDiff.derivative(fxi, interval(x))
dfdy = ForwardDiff.derivative(fyi, interval(y))
grad = ForwardDiff.gradient(fxy, [interval(x), interval(y)])

@test isguaranteed(dfdx)
@test isguaranteed(dfdy)
@test isguaranteed(grad[1])
@test isguaranteed(grad[2])

if iszero(x) && y < 0
@test decoration(dfdx) == trv
else
@test in_interval(ForwardDiff.derivative(fx, x), dfdx)
end

if iszero(x) && y <= 0
@test decoration(dfdy) == trv
else
@test in_interval(ForwardDiff.derivative(fy, y), dfdy)
end

if iszero(x) && iszero(y)
@test decoration(grad[1]) == trv
@test decoration(dfdx) == com
else
@test isequal_interval(dfdx, grad[1])
end
@test isequal_interval(dfdy, grad[2])
end
end
end
end
end
4 changes: 3 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Test

using ForwardDiff
using IntervalArithmetic
using InteractiveUtils

Expand All @@ -22,4 +24,4 @@ for f ∈ readdir("ITF1788_tests"; join = true)
@testset "$f" begin
include(f)
end
end
end

2 comments on commit 5275e5c

@Kolaru
Copy link
Collaborator Author

@Kolaru Kolaru commented on 5275e5c Feb 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/101546

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.22.8 -m "<description of version>" 5275e5cdbc78019e790799c50e1bf0f80dfaa4e1
git push origin v0.22.8

Please sign in to comment.