# Help to create a structure to use ForwardDiff package

**URL:** <https://discourse.julialang.org/t/help-to-create-a-structure-to-use-forwarddiff-package/33657>\
**Category:** General Usage\
**Created:** [January 22, 2020, 12:26pm UTC](https://discourse.julialang.org/t/help-to-create-a-structure-to-use-forwarddiff-package/33657 "2020-01-22T12:26:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [January 22, 2020, 12:26pm UTC](https://discourse.julialang.org/t/help-to-create-a-structure-to-use-forwarddiff-package/33657/1 "2020-01-22T12:26:55Z")

</div>

Hi!

I want to use the **ForwardDiff** package to perform automatic differentiation of a function. However, this function requires an structure in which I am declaring like this:

```julia
@with_kw mutable struct MyStruct{T<:Real}
    a::T = 0
    b::T = 0
    c::T = 0
end

```

However, this is leading to problems related to `ForwardDiff` due to the types. The solution seems to declare something like:

```julia
@with_kw mutable struct MyStruct{T1,T2,T3}
    a::T1 = 0
    b::T2 = 0
    c::T3 = 0
end

```

However, I have more than 30 variables. Is there a way to make this cleaner instead of writing `{T1,T2,T3,T4,...,T30}`?

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [January 22, 2020, 1:27pm UTC](https://discourse.julialang.org/t/help-to-create-a-structure-to-use-forwarddiff-package/33657/2 "2020-01-22T13:27:30Z")

</div>

One option is to construct (and deconstruct) a vector with those numbers, and call that from ForwardDiff. but they are all of different types? Maybe parametrizing with one type can help

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [January 22, 2020, 2:34pm UTC](https://discourse.julialang.org/t/help-to-create-a-structure-to-use-forwarddiff-package/33657/3 "2020-01-22T14:34:19Z")

</div>

I do not think this will help. The structure is something that is used inside a complicated algorithm. Those are not the values I want to differentiate. The problem is that, when the structure is allocated, some are dual numbers and some are not.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 22, 2020, 3:41pm UTC](https://discourse.julialang.org/t/help-to-create-a-structure-to-use-forwarddiff-package/33657/4 "2020-01-22T15:41:50Z")

</div>

> [@Ronis\_BR](#):
>
> some are dual numbers and some are not

Either you can promote them to the same type, or introduce more granular type parameters.

Hard to say more without an MWE.

---

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [January 22, 2020, 5:43pm UTC](https://discourse.julialang.org/t/help-to-create-a-structure-to-use-forwarddiff-package/33657/5 "2020-01-22T17:43:24Z")

</div>

`@qstruct_fp` in [https://github.com/cstjean/QuickTypes.jl](https://github.com/cstjean/QuickTypes.jl) is useful for this

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [January 22, 2020, 5:50pm UTC](https://discourse.julialang.org/t/help-to-create-a-structure-to-use-forwarddiff-package/33657/6 "2020-01-22T17:50:33Z")

</div>

> [@Tamas\_Papp](#):
>
> Hard to say more without an MWE.

It is very difficult to provide a MWE because it is the SGP4 propagator. The code is big, and I am not sure where the error is created. I only know that using a different type for each variable solves the problem.

> [@tkf](#):
>
> `@qstruct_fp` in [GitHub - cstjean/QuickTypes.jl: Concise type definitions](https://github.com/cstjean/QuickTypes.jl) is useful for this

Thanks! This should help me 🙂
