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))
so:
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, 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