Skip to content

Commit

Permalink
Merge pull request #718 from JuliaDiff/dw/backport_nanmath
Browse files Browse the repository at this point in the history
Backport #717 to 0.10
  • Loading branch information
devmotion authored Nov 8, 2024
2 parents 228d40d + eb5ddeb commit 0a35a80
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "ForwardDiff"
uuid = "f6369f11-7733-5829-9624-2563aa707210"
version = "0.10.37"
version = "0.10.38"

[deps]
CommonSubexpressions = "bbf7d656-a473-5ed7-a52c-81e309532950"
Expand Down
6 changes: 3 additions & 3 deletions src/dual.jl
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ end
# exponentiation #
#----------------#

for f in (:(Base.:^), :(NaNMath.pow))
for (f, log) in ((:(Base.:^), :(Base.log)), (:(NaNMath.pow), :(NaNMath.log)))
@eval begin
@define_binary_dual_op(
$f,
Expand All @@ -532,7 +532,7 @@ for f in (:(Base.:^), :(NaNMath.pow))
elseif iszero(vx) && vy > 0
logval = zero(vx)
else
logval = expv * log(vx)
logval = expv * ($log)(vx)
end
new_partials = _mul_partials(partials(x), partials(y), powval, logval)
return Dual{Txy}(expv, new_partials)
Expand All @@ -550,7 +550,7 @@ for f in (:(Base.:^), :(NaNMath.pow))
begin
v = value(y)
expv = ($f)(x, v)
deriv = (iszero(x) && v > 0) ? zero(expv) : expv*log(x)
deriv = (iszero(x) && v > 0) ? zero(expv) : expv*($log)(x)
return Dual{Ty}(expv, deriv * partials(y))
end
)
Expand Down
12 changes: 12 additions & 0 deletions test/DerivativeTest.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module DerivativeTest

import Calculus
import NaNMath

using Test
using Random
Expand Down Expand Up @@ -93,6 +94,17 @@ end
@test (x -> ForwardDiff.derivative(y -> x^y, 1.5))(0.0) === 0.0
end

@testset "exponentiation with NaNMath" begin
@test isnan(ForwardDiff.derivative(x -> NaNMath.pow(NaN, x), 1.0))
@test isnan(ForwardDiff.derivative(x -> NaNMath.pow(x,NaN), 1.0))
@test !isnan(ForwardDiff.derivative(x -> NaNMath.pow(1.0, x),1.0))
@test isnan(ForwardDiff.derivative(x -> NaNMath.pow(x,0.5), -1.0))

@test isnan(ForwardDiff.derivative(x -> x^NaN, 2.0))
@test ForwardDiff.derivative(x -> x^2.0,2.0) == 4.0
@test_throws DomainError ForwardDiff.derivative(x -> x^0.5, -1.0)
end

@testset "dimension error for derivative" begin
@test_throws DimensionMismatch ForwardDiff.derivative(sum, fill(2pi, 3))
end
Expand Down
11 changes: 11 additions & 0 deletions test/GradientTest.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module GradientTest

import Calculus
import NaNMath

using Test
using ForwardDiff
Expand Down Expand Up @@ -168,4 +169,14 @@ end
@test ForwardDiff.gradient(f, [0.3, 25.0]) == [3486.0, 0.0]
end

@testset "gradient for exponential with NaNMath" begin
@test isnan(ForwardDiff.gradient(x -> NaNMath.pow(x[1],x[1]), [NaN, 1.0])[1])
@test ForwardDiff.gradient(x -> NaNMath.pow(x[1], x[2]), [1.0, 1.0]) == [1.0, 0.0]
@test isnan(ForwardDiff.gradient((x) -> NaNMath.pow(x[1], x[2]), [-1.0, 0.5])[1])

@test isnan(ForwardDiff.gradient(x -> x[1]^x[2], [NaN, 1.0])[1])
@test ForwardDiff.gradient(x -> x[1]^x[2], [1.0, 1.0]) == [1.0, 0.0]
@test_throws DomainError ForwardDiff.gradient(x -> x[1]^x[2], [-1.0, 0.5])
end

end # module

2 comments on commit 0a35a80

@devmotion
Copy link
Member Author

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/119017

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.10.38 -m "<description of version>" 0a35a807e9a39a37d99d9928ea7d61247d8e2882
git push origin v0.10.38

Please sign in to comment.