# Method error composing quadqk and forwarddiff

**URL:** <https://discourse.julialang.org/t/method-error-composing-quadqk-and-forwarddiff/112461>\
**Category:** Numerics\
**Tags:** question, package\
**Created:** [April 3, 2024, 10:05am UTC](https://discourse.julialang.org/t/method-error-composing-quadqk-and-forwarddiff/112461 "2024-04-03T10:05:45Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Domenico\_Lahaye](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/domenico_lahaye/32/203728_2.png) [@Domenico\_Lahaye](https://discourse.julialang.org/u/Domenico_Lahaye)\
**Post date:** [April 3, 2024, 10:05am UTC](https://discourse.julialang.org/t/method-error-composing-quadqk-and-forwarddiff/112461/1 "2024-04-03T10:05:45Z")

</div>

Greetings! Listed below is a small piece of code that composes QuadGK and ForwardDiff and that fails. Including the resulting method error message for the function extract\_derivative(). I have two questions regarding this code.

1/ Is there a way to see that the quadqk() call results in the function u(x) = x - 0.5 other then by sampling the output u(x)?

2/ How do I make the ForwardDiff.derivative() call work (i.e. avoid the method error)?

Thx!

```julia
using QuadGK
using ForwardDiff

# define two input integrand
integrand(x,y) = x - y

# compute integral by quadrature over second input - results in u(x) = x - 0.5
u(x) = quadgk(y -> integrand(x,y), 0, 1)

# compute derivative - should result in du/dx = 1
# toy model ForwardDiff.derivative(x -> x^2, 1) works just fine
# alternative attempts ForwardDiff.derivative(x -> u(x), 1) or
# ForwardDiff.derivative(x -> u(x), 1) result in same method error
ForwardDiff.derivative(u, 1)

```

**ERROR:** MethodError: no method matching extract\_derivative(::Type{ForwardDiff.Tag{var"#9#10", Int64}}, ::typeof(u))

Closest candidates are:  
extract\_derivative(::Type{T}, ::ForwardDiff.Dual) where T at ~/.julia/packages/ForwardDiff/PcZ48/src/derivative.jl:84  
extract\_derivative(::Type{T}, ::Real) where T at ~/.julia/packages/ForwardDiff/PcZ48/src/derivative.jl:82  
extract\_derivative(::Type{T}, ::AbstractArray) where T at ~/.julia/packages/ForwardDiff/PcZ48/src/derivative.jl:85

…

Stacktrace:

[1] **derivative(f::var"#9#10", x::Int64)**

@ ForwardDiff ~/.julia/packages/ForwardDiff/PcZ48/src/derivative.jl:14

[2] top-level scope

@ REPL[27]:1

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [April 3, 2024, 10:13am UTC](https://discourse.julialang.org/t/method-error-composing-quadqk-and-forwarddiff/112461/2 "2024-04-03T10:13:50Z")

</div>

I’m not sure that is the only issue but IIRC `quadgk` returns a tuple of outputs: the integral and the error estimate. You are only interested in the first one

---

<div class="post-metadata">

**Author:** ![Domenico\_Lahaye](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/domenico_lahaye/32/203728_2.png) [@Domenico\_Lahaye](https://discourse.julialang.org/u/Domenico_Lahaye)\
**Post date:** [April 3, 2024, 10:18am UTC](https://discourse.julialang.org/t/method-error-composing-quadqk-and-forwarddiff/112461/3 "2024-04-03T10:18:57Z")

</div>

Let me check …

---

<div class="post-metadata">

**Author:** ![Domenico\_Lahaye](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/domenico_lahaye/32/203728_2.png) [@Domenico\_Lahaye](https://discourse.julialang.org/u/Domenico_Lahaye)\
**Post date:** [April 3, 2024, 10:28am UTC](https://discourse.julialang.org/t/method-error-composing-quadqk-and-forwarddiff/112461/4 "2024-04-03T10:28:40Z")

</div>

Replacing

```julia
ForwardDiff.derivative(x -> u(x) ,5.)

```

by

```julia
ForwardDiff.derivative(x -> u(x)[1] ,5.)

```

seems to do the trick (for now - requires more elaborate check). Thx!

Q: Is there a way to verify the output of the quadqk() call that avoids sampling?

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [April 3, 2024, 10:40am UTC](https://discourse.julialang.org/t/method-error-composing-quadqk-and-forwarddiff/112461/5 "2024-04-03T10:40:33Z")

</div>

what do you mean by verify?

---

<div class="post-metadata">

**Author:** ![Domenico\_Lahaye](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/domenico_lahaye/32/203728_2.png) [@Domenico\_Lahaye](https://discourse.julialang.org/u/Domenico_Lahaye)\
**Post date:** [April 3, 2024, 10:45am UTC](https://discourse.julialang.org/t/method-error-composing-quadqk-and-forwarddiff/112461/6 "2024-04-03T10:45:56Z")

</div>

By “verify” I mean checking that u(x) = x - 0.5 after the quadgk() call (in the toy example provided above). Maybe something like @code\_lowered u(1), but more informative. Not sure that I am making sense here (?).

```julia
code_lowered u(1)
CodeInfo(
1 ─ %1 = Main.:(var"#17#18")
│ %2 = Core.typeof(x)
│ %3 = Core.apply_type(%1, %2)
│ #17 = %new(%3, x)
│ %5 = #17
│ %6 = Main.quadgk(%5, 0, 1)
└── return %6
)

```

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [April 3, 2024, 10:47am UTC](https://discourse.julialang.org/t/method-error-composing-quadqk-and-forwarddiff/112461/7 "2024-04-03T10:47:10Z")

</div>

> [@Domenico\_Lahaye](#):
>
> Not sure that I am making sense here (?).

not completely ^^ for which `x` do you want to verify that?

---

<div class="post-metadata">

**Author:** ![Domenico\_Lahaye](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/domenico_lahaye/32/203728_2.png) [@Domenico\_Lahaye](https://discourse.julialang.org/u/Domenico_Lahaye)\
**Post date:** [April 3, 2024, 10:50am UTC](https://discourse.julialang.org/t/method-error-composing-quadqk-and-forwarddiff/112461/8 "2024-04-03T10:50:01Z")

</div>

I will make some homework and get back here.

Hurray for your input for now!

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [April 3, 2024, 10:51am UTC](https://discourse.julialang.org/t/method-error-composing-quadqk-and-forwarddiff/112461/9 "2024-04-03T10:51:26Z")

</div>

do you mean getting the value along with the derivative? there’s no built in way to do that with ForwardDiff but other packages can offer that option
