# How to use autodiff with Quadrature.jl

**URL:** <https://discourse.julialang.org/t/how-to-use-autodiff-with-quadrature-jl/77437>\
**Category:** General Usage\
**Tags:** integral\
**Created:** [March 5, 2022, 5:41am UTC](https://discourse.julialang.org/t/how-to-use-autodiff-with-quadrature-jl/77437 "2022-03-05T05:41:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![samerb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samerb/32/9242_2.png) [@samerb](https://discourse.julialang.org/u/samerb)\
**Post date:** [March 5, 2022, 5:41am UTC](https://discourse.julialang.org/t/how-to-use-autodiff-with-quadrature-jl/77437/1 "2022-03-05T05:41:51Z")

</div>

I’m trying to use autodiff on a function that uses Quadrature.jl numerical integration. I’m getting an error and not sure why. Any suggestions? Thanks.

The function `f(y)` is `\int_0^y x dx - y` (computed numerically using Quadrature.jl) which is just `0.5y^2 - y`. I want to take the derivative of that using autodiff. This is just meant to be a minimal example which will be made more complicated and eventually used in JuMP.

```julia
using Quadrature
function f(y)
    g(x,p) = x
    prob = QuadratureProblem(g, 0.0, y)
    sol = solve(prob,HCubatureJL(),reltol=1e-3,abstol=1e-3)
    return sol[1] - y
end
f(1) # = -0.5

using ForwardDiff
g = x -> ForwardDiff.derivative(f, x)
g(2)
# MethodError: no method matching kronrod(::Type{ForwardDiff.Dual{ForwardDiff.Tag{typeof(f), Int64}, Float64, 1}}, ::Int64)

```

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [March 6, 2022, 4:15am UTC](https://discourse.julialang.org/t/how-to-use-autodiff-with-quadrature-jl/77437/2 "2022-03-06T04:15:23Z")

</div>

Double post. Hasn’t been added yet. Someone would just need to extend [https://github.com/SciML/Quadrature.jl/blob/master/src/Quadrature.jl#L583](https://github.com/SciML/Quadrature.jl/blob/master/src/Quadrature.jl#L583) which wouldn’t be too hard.

---

<div class="post-metadata">

**Author:** ![ffkk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffkk/32/51482_2.png) [@ffkk](https://discourse.julialang.org/u/ffkk)\
**Post date:** [July 12, 2023, 3:18pm UTC](https://discourse.julialang.org/t/how-to-use-autodiff-with-quadrature-jl/77437/3 "2023-07-12T15:18:33Z")

</div>

Any updates on this?  
this issue has gotten some activity recently: [Differentiation for a limit of an integral · Issue #56 · SciML/Integrals.jl · GitHub](https://github.com/SciML/Integrals.jl/issues/56),  
but [https://github.com/SciML/Quadrature.jl/blob/master/src/Quadrature.jl#L583](https://github.com/SciML/Quadrature.jl/blob/master/src/Quadrature.jl#L583) leads to a page that says ‘404-page not found’ on github.  
does Quadrature.jl still exist? if so, where would one extend the corresponding line?

---

<div class="post-metadata">

**Author:** ![flmuk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/flmuk/32/204871_2.png) [@flmuk](https://discourse.julialang.org/u/flmuk)\
**Post date:** [February 20, 2024, 9:25am UTC](https://discourse.julialang.org/t/how-to-use-autodiff-with-quadrature-jl/77437/4 "2024-02-20T09:25:48Z")

</div>

Any news on this? Would be greatly appreciated. I would also be glad to contribute to speed this up, but might need some guidance.

---

<div class="post-metadata">

**Author:** ![lxvm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lxvm/32/50010_2.png) [@lxvm](https://discourse.julialang.org/u/lxvm)\
**Post date:** [February 20, 2024, 7:47pm UTC](https://discourse.julialang.org/t/how-to-use-autodiff-with-quadrature-jl/77437/5 "2024-02-20T19:47:39Z")

</div>

Nobody is working on this actively, but we would have to implement the [Leibniz Integral Rule](https://en.wikipedia.org/wiki/Leibniz_integral_rule), and if any pr is opened for Integrals.jl I would be able to help.

In the long term this might end up in SciMLSensitivity.jl, would have to be implemented for both forward and reverse mode, be compatible with in-place and batched integral functions, and ideally give the user a choice about whether to differentiate-and-discretize or discretize-and-differentiate.
