I cannot find, either in the documentation of ForwardDiff, or on the web more widely, any information on how to compute the second derivative of a scalar function with a scalar input. How can I do this? (Given a function f that takes x<:Real as input).
Is there a way to compute the function output and first and second derivatives simultaneously, like calling hessian with a DiffResult? Note that this latter approach only works with array-like inputs.
function computehessian(f, x::AbstractArray)
result = DiffResults.HessianResult(x)
result = ForwardDiff.hessian!(result, f, x)
return DiffResults.value(result), DiffResults.gradient(result), DiffResults.hessian(result)
end
computehessian(f, x::T) where T <: Number = map(first, computehessian(f ∘ first, StaticArrays.SVector{1, T}(x)))
Given this slow-down (caused by calling f 4 times instead of once), I think it strange that the hessian! functionality doesn’t exist for scalar x, so I opened an issue about it.