Skip to content

Commit

Permalink
Fix exact reals with forwardiff pow
Browse files Browse the repository at this point in the history
  • Loading branch information
Kolaru committed May 24, 2024
1 parent 4b62ac8 commit 5da7fcf
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
18 changes: 18 additions & 0 deletions ext/IntervalArithmeticForwardDiffExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,22 @@ function Base.:(^)(x::ExactReal, y::Dual{<:Any, I}) where I<:Interval
return convert(I, x)^y
end

function Base.:(^)(x::Dual{<:Tx}, y::ExactReal) where Tx
v = value(x)
expv = v^y
if iszero(y) || all(iszero.(values(partials(x))))
new_partials = zero(partials(x))
else
new_partials = partials(x) * y * v^(y - 1)
end
return Dual{Tx}(expv, new_partials)
end

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

end
2 changes: 2 additions & 0 deletions src/intervals/exact_literals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ struct ExactReal{T<:Real} <: Real
ExactReal(value::T) where {T<:Real} = new{T}(value)
end

Base.iszero(x::ExactReal) = iszero(x.value)

_value(x::ExactReal) = x.value # hook for interval constructor

# allow to index with ExactReal
Expand Down

0 comments on commit 5da7fcf

Please sign in to comment.