Hi,

I am really stuck for a while now, I cannot seem to use ForwardDiff to calculate second derivatives of a function. It works for the first derivative, but the same approach applied to the first derivative crashes with the error.

I am trying to pre-allocate storage, because this will be called millions of times. The non ! version of the jacobian on the non ! version of the function does work, but rather slow with 80GB of allocations overall.

I have also looked into TaylorSeries: the second derivative has to be function, i.e., the derivative needs to be calculated not just at a single point but anywhere, so I found this not applicable.

```
ERROR: LoadError: MethodError: Cannot `convert` an object of type ForwardDiff.Dual{ForwardDiff.Tag{dummy.##3#4{dummy.rhsConfig,Int64},Float64},Float64,4} to an object of type Float64
This may have arisen from a call to the constructor Float64(...),
since type constructors fall back to convert methods.
```

My code is

```
module dummy
using ForwardDiff
function rhs!(y, x, p)
y[1] = x[2]
y[2] = -3*(1*x[1] + 2*x[3])^3 - 6*x[2] - x[1]*12
y[3] = x[4]
y[4] = -4*(1*x[1] + 2*x[3])^3 - 8*x[4] - x[3]*16
return nothing
end
mutable struct rhsConfig
y
jac
hess
rhsConfig(n) = new(zeros(n), zeros(n,n), zeros(n*n, n))
end
function rhsJac!(res, z, conf::rhsConfig, p)
ForwardDiff.jacobian!(res, (dy, z1) -> rhs!(dy, z1, p), conf.y, z)
return nothing
end
function rhsHess!(res, z1, z2, conf::rhsConfig, p)
ForwardDiff.jacobian!(res, (j, z) -> rhsJac!(j, z, conf, p), conf.jac, z1)
return nothing
end
function testJac()
p=0
cf=rhsConfig(4)
x=0.2*ones(4)
jac=zeros(4,4)
@time rhsJac!(jac, x, cf, p)
end
function testHess()
p=0
cf=rhsConfig(4)
z1=0.2*ones(4)
z2=0.2*ones(4)
res=zeros(4*4,4)
@time rhsHess!(res, z1, z2, cf, p)
end
testJac()
testJac()
testHess()
testHess()
end
```

Thanks…