Hi @Domenico_Lahaye! ForwardDiff expects the input and output of your function to be an array of numbers. You could use stack to make sure of that, but you have to be careful with the shapes and what they mean.

An alternative is ComponentArrays.jl, but honestly I would just recommend stacking everything into a 3-dimensional tensor. Note that the jacobian returned by ForwardDiff will always be a 2d matrix though, it essentially flattens the input and output into vectors.

Note that when you use SArrays you don’t need configuration, jacobian as is works best.
Maybe you don’t need to split and can create a single SArray of dimension 3?

Yes it is known because your first function coerces the inputs into a Float64 type, whereas ForwardDiff needs more generality to function, see Limitations of ForwardDiff · ForwardDiff. You need to allow arbitrary real types to flow through your function, including any containers you generate.

I don’t have any in mind apart from the API reference, but if you’re working solely with SArrays then configuration should be unnecessary. Do you have a complete MWE that I could maybe help you optimize?

You can’t, because your functions both allocate new vectors. ForwardDiff computes Jacobians by running the function once per input dimension^{[1]}, so the number of allocations will necessarily scale with the input.

Still, you can greatly increase efficiency by:

Writing your functions in-place: f!(y, x) instead of y = f(x)

The idea is that steps 2 and 3 only need to be done once, and then you can compute many derivatives reusing the same memory.

You can’t, because ForwardDiff uses a custom tag for each function to identify where the Dual numbers came from. You can disable it but I would advise against it.

Not entirely true, the chunking mechanism means you can actually divide the input dimension by the chunk size. ↩︎