# Autodifferentiation of model with fixed and mutable parameters

**URL:** <https://discourse.julialang.org/t/autodifferentiation-of-model-with-fixed-and-mutable-parameters/123208>\
**Category:** General Usage\
**Tags:** question, optim, autodiff\
**Created:** [November 28, 2024, 10:46am UTC](https://discourse.julialang.org/t/autodifferentiation-of-model-with-fixed-and-mutable-parameters/123208 "2024-11-28T10:46:34Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/224680_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [November 28, 2024, 11:59am UTC](https://discourse.julialang.org/t/autodifferentiation-of-model-with-fixed-and-mutable-parameters/123208/3 "2024-11-28T11:59:49Z")

</div>

Hi @EminentCoder, welcome to the community!

What the answer above (ChatGPT ?) means to say is that your vector `all_params` is initialized to be full of `Float64`. Thus, ForwardDiff cannot use it in differentiation because it needs to work on `Dual` numbers (see [limitations of ForwardDiff](https://juliadiff.org/ForwardDiff.jl/stable/user/limitations/)).  
I think something along this line would work, where you re-create a new vector of global parameters with the same element type as the variable you’re using in differentiation:

```julia
function separator(obj_func, selector_array, all_params, mutable_params)
    new_all_params = similar(all_params, eltype(mutable_params)) # can accommodate Dual numbers if necessary
    new_all_params[selector_array] .= mutable_params
    new_all_params[.!selector_array] = all_params[.!selector_array]
    return obj_func(all_params)
end

```

If that code is too slow for you, you may also want take a look at the [performance tips](https://docs.julialang.org/en/v1/manual/performance-tips/). In particular, using of global variables that are not passed as function arguments is often a bad idea performance-wise.

---

_[View the full topic](https://discourse.julialang.org/t/autodifferentiation-of-model-with-fixed-and-mutable-parameters/123208)._
