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

Add definition for logabsgamma #585

Merged
merged 4 commits into from
May 18, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
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.29"
version = "0.10.30"

[deps]
CommonSubexpressions = "bbf7d656-a473-5ed7-a52c-81e309532950"
Expand Down
9 changes: 9 additions & 0 deletions src/dual.jl
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,15 @@ function LinearAlgebra.eigen(A::SymTridiagonal{<:Dual{Tg,T,N}}) where {Tg,T<:Rea
Eigen(λ,Dual{Tg}.(Q, tuple.(parts...)))
end

# SpecialFunctions.logabsgamma #
# Derivative is not defined in DiffRules #
#----------------------------------------#

function SpecialFunctions.logabsgamma(d::Dual{T,<:Real}) where {T}
x = value(d)
y, s = SpecialFunctions.logabsgamma(x)
return (Dual{T}(y, SpecialFunctions.digamma(x) * partials(d)), s)
Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe this should return rather

Suggested change
return (Dual{T}(y, SpecialFunctions.digamma(x) * partials(d)), s)
return (Dual{T}(y, SpecialFunctions.digamma(x) * partials(d)), Dual{T}(s, zero(s)))

to be consistent with sign(::Dual)?

Copy link
Member

Choose a reason for hiding this comment

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

it doesn't really matter one way or the other does it? promotion will fix it when it matters.

Copy link
Member Author

Choose a reason for hiding this comment

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

No, I don't think it matters. It felt easier for downstream computations and somewhat stronger to drop the partials completely. Only afterwards I noticed the inconsistency with sign and started to wonder if we should return a zero partial instead.

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm.... Maybe we should be consistent with sign at all since it makes a difference in an example such as:

julia> using ForwardDiff

julia> f(x) = Inf * x
f (generic function with 1 method)

julia> g(x) = sign(x)
g (generic function with 1 method)

julia> g(x::ForwardDiff.Dual) = sign(ForwardDiff.value(x))
g (generic function with 2 methods)

julia> ForwardDiff.derivative(f  g, 1.0)
0.0

julia> ForwardDiff.derivative(f  sign, 1.0)
NaN

The "stronger" definition that drops the partial completely doesn't run into the NaN-safe mode issues and hence does not care about the non-finite result.

Maybe it's an argument for defining

Base.sign(x::Dual{<:Any,<:Real}) = sign(value(x))

rather than changing this PR though.

end

###################
# Pretty Printing #
Expand Down
3 changes: 3 additions & 0 deletions test/DualTest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,9 @@ for N in (0,3), M in (0,4), V in (Int, Float32)
@test dual_isapprox(f(FDNUM, PRIMAL2, PRIMAL3), Dual{TestTag()}(f(PRIMAL, PRIMAL2, PRIMAL3), PRIMAL2*PARTIALS))
@test dual_isapprox(f(PRIMAL, PRIMAL2, FDNUM3), Dual{TestTag()}(f(PRIMAL, PRIMAL2, PRIMAL3), PARTIALS3))
end

@test dual_isapprox(logabsgamma(FDNUM)[1], loggamma(abs(FDNUM)))
@test dual_isapprox(logabsgamma(FDNUM)[2], sign(gamma(FDNUM)))
end

@testset "Exponentiation of zero" begin
Expand Down