# ForwardDiff problem: Cannot \`convert\` an object of type ForwardDiff.Dual{

**URL:** https://discourse.julialang.org/t/forwarddiff-problem-cannot-convert-an-object-of-type-forwarddiff-dual/11167
**Category:** General Usage
**Tags:** question
**Created:** [May 26, 2018, 2:58pm UTC](https://discourse.julialang.org/t/forwarddiff-problem-cannot-convert-an-object-of-type-forwarddiff-dual/11167 "2018-05-26T14:58:28Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![fastwave](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fastwave/32/14725_2.png) [@fastwave](https://discourse.julialang.org/u/fastwave)
#### Post date: [May 26, 2018, 2:58pm UTC](https://discourse.julialang.org/t/forwarddiff-problem-cannot-convert-an-object-of-type-forwarddiff-dual/11167/1 "2018-05-26T14:58:28Z")

</div>

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.

```julia
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

```julia
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…

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [May 26, 2018, 3:05pm UTC](https://discourse.julialang.org/t/forwarddiff-problem-cannot-convert-an-object-of-type-forwarddiff-dual/11167/2 "2018-05-26T15:05:30Z")

</div>

A solution to this is in the DiffEq FAQ:

[http://docs.juliadiffeq.org/latest/basics/faq.html#I-get-Dual-number-errors-when-I-solve-my-ODE-with-Rosenbrock-or-SDIRK-methods](http://docs.juliadiffeq.org/latest/basics/faq.html#I-get-Dual-number-errors-when-I-solve-my-ODE-with-Rosenbrock-or-SDIRK-methods)…?-1

The real solution will be Capstan.jl.

---

<div class="post-metadata">

### Author: ![fastwave](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fastwave/32/14725_2.png) [@fastwave](https://discourse.julialang.org/u/fastwave)
#### Post date: [May 27, 2018, 3:12pm UTC](https://discourse.julialang.org/t/forwarddiff-problem-cannot-convert-an-object-of-type-forwarddiff-dual/11167/3 "2018-05-27T15:12:29Z")

</div>

Hi Chris, this has been very helpful, looking forward to Capstan.jl and Casette.jl, both are very promising.
