A way to obtain the value and derivatives with ForwardDiff?

I have a function

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:

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


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

but this suffers from perturbation confusion, 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

What is A? Did you mean a?

1 Like

Yes, thanks, I corrected it

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.

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.