Help to create a structure to use ForwardDiff package

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:

@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:

@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}?

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

1 Like

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.

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

Hard to say more without an MWE.

@qstruct_fp in https://github.com/cstjean/QuickTypes.jl is useful for this

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.

Thanks! This should help me :slight_smile: