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
Changes from 1 commit
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
102 changes: 66 additions & 36 deletions src/dual.jl
Original file line number Diff line number Diff line change
Expand Up @@ -552,43 +552,73 @@
# exponentiation #
#----------------#

for f in (:(Base.:^), :(NaNMath.pow))
jClugstor marked this conversation as resolved.
Show resolved Hide resolved
@eval begin
@define_binary_dual_op(
$f,
begin
vx, vy = value(x), value(y)
expv = ($f)(vx, vy)
powval = vy * ($f)(vx, vy - 1)
if isconstant(y)
logval = one(expv)
elseif iszero(vx) && vy > 0
logval = zero(vx)
else
logval = expv * log(vx)
end
new_partials = _mul_partials(partials(x), partials(y), powval, logval)
return Dual{Txy}(expv, new_partials)
end,
begin
v = value(x)
expv = ($f)(v, y)
if y == zero(y) || iszero(partials(x))
new_partials = zero(partials(x))
else
new_partials = partials(x) * y * ($f)(v, y - 1)
end
return Dual{Tx}(expv, new_partials)
end,
begin
v = value(y)
expv = ($f)(x, v)
deriv = (iszero(x) && v > 0) ? zero(expv) : expv*log(x)
return Dual{Ty}(expv, deriv * partials(y))
end
)
@define_binary_dual_op(
Base.:^,
begin
vx, vy = value(x), value(y)
expv = (^)(vx, vy)
powval = vy * (^)(vx, vy - 1)
if isconstant(y)
logval = one(expv)
elseif iszero(vx) && vy > 0
logval = zero(vx)
else
logval = expv * log(vx)
end
new_partials = _mul_partials(partials(x), partials(y), powval, logval)
return Dual{Txy}(expv, new_partials)
end,
begin
v = value(x)
expv = (^)(v, y)
if y == zero(y) || iszero(partials(x))
new_partials = zero(partials(x))
else
new_partials = partials(x) * y * (^)(v, y - 1)
end
return Dual{Tx}(expv, new_partials)
end,
begin
v = value(y)
expv = (^)(x, v)
deriv = (iszero(x) && v > 0) ? zero(expv) : expv * log(x)
return Dual{Ty}(expv, deriv * partials(y))
end
end
)

@define_binary_dual_op(
NaNMath.pow,
begin
vx, vy = value(x), value(y)
expv = NaNMath.pow(vx, vy)
powval = vy * NaNMath.pow(vx, vy - 1)
if isconstant(y)
logval = one(expv)
elseif iszero(vx) && vy > 0
logval = zero(vx)

Check warning on line 598 in src/dual.jl

View check run for this annotation

Codecov / codecov/patch

src/dual.jl#L597-L598

Added lines #L597 - L598 were not covered by tests
else
logval = expv * NaNMath.log(vx)

Check warning on line 600 in src/dual.jl

View check run for this annotation

Codecov / codecov/patch

src/dual.jl#L600

Added line #L600 was not covered by tests
end
new_partials = _mul_partials(partials(x), partials(y), powval, logval)
return Dual{Txy}(expv, new_partials)
end,
begin
v = value(x)
expv = NaNMath.pow(v, y)
if y == zero(y) || iszero(partials(x))
new_partials = zero(partials(x))

Check warning on line 609 in src/dual.jl

View check run for this annotation

Codecov / codecov/patch

src/dual.jl#L609

Added line #L609 was not covered by tests
else
new_partials = partials(x) * y * NaNMath.pow(v, y - 1)
end
return Dual{Tx}(expv, new_partials)
end,
begin
v = value(y)
expv = NaNMath.pow(x, v)
deriv = (iszero(x) && v > 0) ? zero(expv) : expv*NaNMath.log(x)
return Dual{Ty}(expv, deriv * partials(y))
end
)

@inline Base.literal_pow(::typeof(^), x::Dual{T}, ::Val{0}) where {T} =
Dual{T}(one(value(x)), zero(partials(x)))
Expand Down
Loading