Skip to content

Commit

Permalink
Define SpecialFunctions.gamma_inc for ForwardDiff.Dual (#587)
Browse files Browse the repository at this point in the history
* Define `SpecialFunctions.gamma_inc` for `ForwardDiff.Dual`

* Try to fix tolerances

* Check if primal method exists

* Fix tolerances
  • Loading branch information
devmotion authored Aug 4, 2022
1 parent f365d41 commit 61a7c5b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
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

2 comments on commit 61a7c5b

@devmotion
Copy link
Member Author

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/65671

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.10.31 -m "<description of version>" 61a7c5b62d150e640c6a177e7c46a7bcd47100f2
git push origin v0.10.31

Please sign in to comment.