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

NaN gradient occurring in SpecialFunctions.gamma, when using float arguments. #705

Open
vwiela opened this issue Jul 12, 2024 · 2 comments

Comments

@vwiela
Copy link

vwiela commented Jul 12, 2024

I tried to use ForwardDiff on the upper incomplete gamma function as provided in SpecialFunctions.jl

Calling it with the first argument being an integer did work fine, but changing it to a float, caused the derivative being NaN.
I tried to dig into it a bit and the following MwE does show the problem.

using ForwardDiff
using SpecialFunctions

function testing_gamma(a::Number, x::Number)
    return iszero(x) ? gamma(one(x)*a) : x^a * expint(1 - a, x)
end


promotereal(x::Real, y::Real) = promote(x, y)
test_gamma(a::Number,x::Number) = testing_gamma(promotereal(float(a), float(x))...)

If we want to get the gradient of testing_gamma wrt. to the second argument, this works fine

ForwardDiff.derivative(x -> testing_gamma(2.0, -x), 0.01)

However, on test_gamma it will return a NaN value.

ForwardDiff.derivative(x -> test_gamma(2.0, -x), 0.01)

Maybe someone has an idea why this behavior occurs and how it can best be fixed.

@vwiela
Copy link
Author

vwiela commented Jul 12, 2024

@gdalle This is the issue.

@ChrisRackauckas
Copy link
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants