# Custom types in ForwardDiff

**URL:** https://discourse.julialang.org/t/custom-types-in-forwarddiff/24610
**Category:** Optimization (Mathematical)
**Tags:** jump
**Created:** [May 25, 2019, 7:15pm UTC](https://discourse.julialang.org/t/custom-types-in-forwarddiff/24610 "2019-05-25T19:15:36Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)
#### Post date: [May 25, 2019, 7:15pm UTC](https://discourse.julialang.org/t/custom-types-in-forwarddiff/24610/1 "2019-05-25T19:15:36Z")

</div>

This might be trivial but I am currently fighting with `JuMP` to accept my quality function for the minimisation procedure. I figured out that my custom type:

```julia
struct Position <: FieldVector{3, Float64}
    x::Float64
    y::Float64
    z::Float64
end

```

is causing this error:

```julia
MethodError: no method matching Float64(::ForwardDiff.Dual{ForwardDiff.Tag{getfield(JuMP, Symbol("##84#86")){KM3NeT.MultiDUMinimiser},Float64},Float64,6})
Closest candidates are:
  Float64(::Real, !Matched::RoundingMode) where T<:AbstractFloat at rounding.jl:194
  Float64(::T<:Number) where T<:Number at boot.jl:741
  Float64(!Matched::Int8) at float.jl:60

```

when I try to

```julia
register(model, :qfunc, 6, m, autodiff=true)
...
...
...
@NLobjective(model, Min, qfunc(x, y, z, θ, ϕ, t₀))
optimize!(model) # this is the line which fails with the message above

```

Given the candidates, I guess I have to somehow implement something related to roundings 😉 ? Can someone toss me in the right direction?

I found this discussion but still clueless: [https://github.com/JuliaDiffEq/DiffEqBase.jl/issues/169](https://github.com/JuliaDiffEq/DiffEqBase.jl/issues/169)

---

<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 25, 2019, 7:16pm UTC](https://discourse.julialang.org/t/custom-types-in-forwarddiff/24610/2 "2019-05-25T19:16:40Z")

</div>

```julia
struct Position{T} <: FieldVector{3, T}
    x::T
    y::T
    z::T
end

```

---

<div class="post-metadata">

### Author: ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)
#### Post date: [May 25, 2019, 7:29pm UTC](https://discourse.julialang.org/t/custom-types-in-forwarddiff/24610/3 "2019-05-25T19:29:38Z")

</div>

Thanks Chris, I tried it but I still get the same error 😕

---

<div class="post-metadata">

### Author: ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)
#### Post date: [May 25, 2019, 7:34pm UTC](https://discourse.julialang.org/t/custom-types-in-forwarddiff/24610/4 "2019-05-25T19:34:44Z")

</div>

I just checked, I think it’s caused by this type now (the `Position` stuff works, and I also added it to `Direction`):

```julia
struct Track
    dir::Direction
    pos::Position
    time::Float64
end

```

I’ll now try to parametrise that with

```julia
  2 struct Track{T}
  1 dir::Direction{T}
89 pos::Position{T}
  1 time::T
  2 end

```

---

<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 25, 2019, 7:35pm UTC](https://discourse.julialang.org/t/custom-types-in-forwarddiff/24610/5 "2019-05-25T19:35:58Z")

</div>

Any place where you write `Float64`, replace it with a parameterization.

---

<div class="post-metadata">

### Author: ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)
#### Post date: [May 25, 2019, 7:36pm UTC](https://discourse.julialang.org/t/custom-types-in-forwarddiff/24610/6 "2019-05-25T19:36:58Z")

</div>

Makes sense now, thanks 😉

---

<div class="post-metadata">

### Author: ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)
#### Post date: [May 25, 2019, 7:41pm UTC](https://discourse.julialang.org/t/custom-types-in-forwarddiff/24610/7 "2019-05-25T19:41:29Z")

</div>

The problem is, now I destroyed some other calls, namely when I create a track using a standard vector:

```julia
MethodError: no method matching KM3NeT.Track(::Array{Float64,1}, ::Array{Float64,1}, ::Float64)
Closest candidates are:
  KM3NeT.Track(!Matched::Direction{T}, !Matched::Position{T}, ::T) where T at /home/tgal/.julia/dev/KM3NeT/src/types.jl:88

```

I am now lost in parametric hell…

---

<div class="post-metadata">

### Author: ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)
#### Post date: [May 25, 2019, 7:49pm UTC](https://discourse.julialang.org/t/custom-types-in-forwarddiff/24610/8 "2019-05-25T19:49:29Z")

</div>

OK, this one works 😉

```julia
struct Track
    dir::Direction
    pos::Position
    time
end

```

---

<div class="post-metadata">

### Author: ![jlperla](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlperla/32/34332_2.png) [@jlperla](https://discourse.julialang.org/u/jlperla)
#### Post date: [May 25, 2019, 9:06pm UTC](https://discourse.julialang.org/t/custom-types-in-forwarddiff/24610/9 "2019-05-25T21:06:21Z")

</div>

Frequently you need to make the types parametric and allow different types between the fields (ie, you only want dual numbers in one of them).
