# A way to obtain the value and derivatives with ForwardDiff?

**URL:** <https://discourse.julialang.org/t/a-way-to-obtain-the-value-and-derivatives-with-forwarddiff/22862>\
**Category:** General Usage\
**Tags:** package\
**Created:** [April 7, 2019, 5:55am UTC](https://discourse.julialang.org/t/a-way-to-obtain-the-value-and-derivatives-with-forwarddiff/22862 "2019-04-07T05:55:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [April 7, 2019, 5:55am UTC](https://discourse.julialang.org/t/a-way-to-obtain-the-value-and-derivatives-with-forwarddiff/22862/1 "2019-04-07T05:55:30Z")

</div>

I have a function

```julia
using ForwardDiff
a(x) = 2x #(in reality this is a long function)
dadx(x) = ForwardDiff.derivative(a, x)
P(x) = a(x) +x*dadx(x)

```

ForwardDiff calculates at the same time the value and the derivative, the first definition of P(x) calls A(x) two times, what i don’t want.  
A workaround is to use directly the dual number:

```julia
x2 = ForwardDiff.Dual{Float64}(x,one(x))

```

so:

```julia
function P(x)
    res = ForwardDiff.Dual{Float64}(x,one(x)))
    return res.value + x*res.partials.values[1]
end

```

but this suffers from [perturbation confusion](https://github.com/JuliaDiff/ForwardDiff.jl/issues/83), and blocks the aplication of a second derivative over that function.  
Other way is with the use of DiffResults, but it needs to create an struct first. Is not bad, as i’m using this in other parts were i need to store those results, but for simple functions gives an overhead.

so the question is if there is any way to obtain the value and the derivative of the function at the same time, like using the dual number, but without suffering with the perturbation problem.

I’m aware that to avoid that problem, ForwardDiff uses a tag system, but i didn’t find how to implement it

---

<div class="post-metadata">

**Author:** ![sagartewari01](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sagartewari01/32/7810_2.png) [@sagartewari01](https://discourse.julialang.org/u/sagartewari01)\
**Post date:** [April 7, 2019, 8:13am UTC](https://discourse.julialang.org/t/a-way-to-obtain-the-value-and-derivatives-with-forwarddiff/22862/2 "2019-04-07T08:13:07Z")

</div>

> [@longemen3000](#):
>
> dadx(x) = ForwardDiff.derivative(A,x)

What is `A`? Did you mean `a`?

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [April 7, 2019, 8:15am UTC](https://discourse.julialang.org/t/a-way-to-obtain-the-value-and-derivatives-with-forwarddiff/22862/3 "2019-04-07T08:15:58Z")

</div>

Yes, thanks, I corrected it

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [April 7, 2019, 5:53pm UTC](https://discourse.julialang.org/t/a-way-to-obtain-the-value-and-derivatives-with-forwarddiff/22862/4 "2019-04-07T17:53:22Z")

</div>

> [@longemen3000](#):
>
> Other way is with the use of DiffResults, but it needs to create an struct first. Is not bad, as i’m using this in other parts were i need to store those results, but for simple functions gives an overhead.

I don’t think that there is any significant overhead if you have the buffer available for the gradient, construct a `DiffResult` from it, then deconstruct it the way you want. See eg [this example](https://github.com/tpapp/LogDensityProblems.jl/blob/81e37919dcca4e0910fd822732772c31c31d1753/src/AD_ForwardDiff.jl#L40).

---

<div class="post-metadata">

**Author:** ![aaowens](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aaowens/32/12101_2.png) [@aaowens](https://discourse.julialang.org/u/aaowens)\
**Post date:** [April 7, 2019, 7:00pm UTC](https://discourse.julialang.org/t/a-way-to-obtain-the-value-and-derivatives-with-forwarddiff/22862/5 "2019-04-07T19:00:41Z")

</div>

I don’t think there should be any overhead at all actually for a `DiffResult` with numbers. Have you benchmarked it? For vectors you can use StaticArrays and get the same thing.
