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

stack overflow for integrating other Number types #13

Closed
MasonProtter opened this issue Jan 28, 2018 · 9 comments
Closed

stack overflow for integrating other Number types #13

MasonProtter opened this issue Jan 28, 2018 · 9 comments

Comments

@MasonProtter
Copy link

I have a function if I have a function f(x) which is the integral another function (using QuadGK) and x is the upper limit of integration. If I want to take an automatic derivative of that function using ForwardDiff, it results in a stack overflow. Here are instructions to reproduce the error

using QuadGK
using ForwardDiff
D(f::Function, x::Number) = ForwardDiff.derivative(f, x)

f(x) = quadgk(y -> y^2, 0, x)
D(x -> quadgk(y -> y^2, 0, x), 1)

ERROR: StackOverflowError:
Stacktrace:
 [1] #quadgk#15(::Array{Any,1}, ::Function, ::Function, ::ForwardDiff.Dual{ForwardDiff.Tag{##3#5,Int64},Float64,1}, ::ForwardDiff.Dual{ForwardDiff.Tag{##3#5,Int64},Float64,1}) at /Users/mason/.julia/v0.6/QuadGK/src/QuadGK.jl:241 (repeats 65479 times)
 [2] derivative(::##3#5, ::Int64) at /Users/mason/.julia/v0.6/ForwardDiff/src/derivative.jl:14
 [3] D(::Function, ::Int64) at ./REPL[4]:1
@stevengj
Copy link
Member

Thanks for the report. The quadgk endpoints only support real and complex numbers right now, and probably there is an infinite loop in the promotion rules for other number types.

It would be good to make this work, if only to make sure the methods are defined generically enough to work with any number type.

(But even if we get this working, I’m guessing that using the analytical derivative “manually” will always be much more efficient.)

@MasonProtter
Copy link
Author

@stevengj Yeah, I wasn't expecting this to work but I decided to give it a try anyways and ended up segfaulting due to the stack overflow so it was suggested I submit an issue.

@stevengj stevengj changed the title Interaction between ForwardDiff.Derivative and QuadGK causing a stack overflow stack overflow for integrating other Number types Apr 25, 2018
@stevengj
Copy link
Member

Also shows up for dimensionful quantities (#17).

@stevengj
Copy link
Member

stevengj commented Jun 7, 2019

The case of dimensionful quantities is fixed by #36.

I'm not sure how to fix this in general — the problem is that I need a package-independent way to get the underlying real-numeric types out of things like Measurement and Dual numbers, and there doesn't seem to be an API for this right now.

@pdeffebach
Copy link

I also encountered this error. I have a non-linear problem trying to find zeros. I have a working solution in NLSolve but needed additional constraints, so I turned to JuMP.

Is there a solution for Dual numbers that exists now?

@stevengj
Copy link
Member

If you want the derivative with respect to the endpoints of a function that involves integration, you can just take the derivative analytically and expose it to automatic differentiation via the ChainRules package. The https://github.com/SciML/Quadrature.jl package (which wraps around QuadGK) does this for you.

@pdeffebach
Copy link

pdeffebach commented Aug 18, 2021

Leibniz integral rule strikes again! Yes I will port over the Quadrature, thanks for the feedback.

@Moelf
Copy link

Moelf commented Aug 5, 2022

SciML actually doesn't work:

julia> f(z) = solve(IntegralProblem((x, _)->sin(x)^2, 0.0, z), QuadGKJL())[1]
f (generic function with 2 methods)

julia> f(3.14)
1.5707963254482846

julia> ForwardDiff.derivative(f, 3.14)
ERROR: StackOverflowError:
Stacktrace:
 [1] cachedrule(#unused#::Type{ForwardDiff.Dual{ForwardDiff.Tag{typeof(f), Float64}, Float64, 1}}, n::Int64) (repeats 79984 times)
   @ QuadGK ~/.julia/packages/QuadGK/ENhXl/src/gausskronrod.jl:253

@stevengj
Copy link
Member

stevengj commented Feb 14, 2023

The original StackOverflowError error is gone, I'm going to close this issue.

It now gives the error:

ERROR: MethodError: no method matching kronrod(::Type{ForwardDiff.Dual{ForwardDiff.Tag{var"#3#5", Int64}, Float64, 1}}, ::Int64)
Closest candidates are:
  kronrod(::Type{T}, ::Integer) where T<:AbstractFloat at ~/.julia/dev/QuadGK/src/gausskronrod.jl:150

which stems from this line. I think it could be fixed if one(Dual) returned 1 instead of a dual number, which seems better to me, but in any case it seems like a separate issue (JuliaDiff/ForwardDiff.jl#624).

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

4 participants