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

Fix exponentiation for NaNMath.pow #717

Merged
merged 8 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
6 changes: 3 additions & 3 deletions src/dual.jl
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ end
# exponentiation #
#----------------#

for f in (:(Base.:^), :(NaNMath.pow))
jClugstor marked this conversation as resolved.
Show resolved Hide resolved
for (f, log) in ((:(Base.:^), :(Base.log)), (:(NaNMath.pow), :(NaNMath.log)))
@eval begin
@define_binary_dual_op(
$f,
Expand All @@ -565,7 +565,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 @@ -583,7 +583,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
13 changes: 13 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 LinearAlgebra
Expand Down Expand Up @@ -200,6 +201,18 @@ end
@test ForwardDiff.gradient(L -> logdet(L), Matrix(L)) ≈ [1.0 -1.3333333333333337; 0.0 1.666666666666667]
end

@testset "gradient for exponential with NaNMath"
@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

devmotion marked this conversation as resolved.
Show resolved Hide resolved

jClugstor marked this conversation as resolved.
Show resolved Hide resolved
@testset "branches in mul!" begin
a, b = rand(3,3), rand(3,3)

Expand Down
Loading