Skip to content

Define SpecialFunctions.gamma_inc for ForwardDiff.Dual #587

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

Merged
merged 4 commits into from
Aug 4, 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
4 changes: 2 additions & 2 deletions 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.30"
version = "0.10.31"

[deps]
CommonSubexpressions = "bbf7d656-a473-5ed7-a52c-81e309532950"
Expand All @@ -24,7 +24,7 @@ DiffTests = "0.0.1, 0.1"
LogExpFunctions = "0.3"
NaNMath = "0.2.2, 0.3, 1"
Preferences = "1"
SpecialFunctions = "0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 0.10, 1.0, 2"
SpecialFunctions = "0.8, 0.9, 0.10, 1.0, 2"
StaticArrays = "0.8.3, 0.9, 0.10, 0.11, 0.12, 1.0"
julia = "1"

Expand Down
14 changes: 11 additions & 3 deletions src/dual.jl
Original file line number Diff line number Diff line change
Expand Up @@ -755,16 +755,24 @@ 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 #
#----------------------------------------#
# Functions in SpecialFunctions which return tuples #
# Their derivatives are 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)
end

# Derivatives wrt to first parameter and precision setting are not supported
function SpecialFunctions.gamma_inc(a::Real, d::Dual{T,<:Real}, ind::Integer) where {T}
x = value(d)
p, q = SpecialFunctions.gamma_inc(a, x, ind)
∂p = exp(-x) * x^(a - 1) / SpecialFunctions.gamma(a) * partials(d)
return (Dual{T}(p, ∂p), Dual{T}(q, -∂p))
end

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

# Functions in Specialfunctions that return tuples and
# therefore are not supported by DiffRules
@test dual_isapprox(logabsgamma(FDNUM)[1], loggamma(abs(FDNUM)))
@test dual_isapprox(logabsgamma(FDNUM)[2], sign(gamma(FDNUM)))

a = rand(float(V))
fdnum = Dual{TestTag()}(1 + PRIMAL, PARTIALS) # 1 + PRIMAL avoids issues with finite differencing close to 0
for ind in ((), (0,), (1,), (2,))
# Only test if primal method exists
# (e.g., older versions of SpecialFunctions don't define `gamma_inc(a, x)` but only `gamma_inc(a, x, ind)`
hasmethod(gamma_inc, typeof((a, 1 + PRIMAL, ind...))) || continue

pq = gamma_inc(a, fdnum, ind...)
@test pq isa Tuple{Dual{TestTag()},Dual{TestTag()}}
# We have to adjust tolerances if lower accuracy is requested
# Therefore we don't use `dual_isapprox`
tol = V === Float32 ? 5f-4 : 1e-6
tol = tol^(one(tol) / 2^(isempty(ind) ? 0 : first(ind)))
for i in 1:2
@test value(pq[i]) ≈ gamma_inc(a, 1 + PRIMAL, ind...)[i] rtol=tol
@test partials(pq[i]) ≈ PARTIALS * Calculus.derivative(x -> gamma_inc(a, x, ind...)[i], 1 + PRIMAL) rtol=tol
end
end
end

@testset "Exponentiation of zero" begin
Expand Down