# How to update function coefficients during run time

**URL:** <https://discourse.julialang.org/t/how-to-update-function-coefficients-during-run-time/108700>\
**Category:** General Usage\
**Created:** [January 11, 2024, 6:35pm UTC](https://discourse.julialang.org/t/how-to-update-function-coefficients-during-run-time/108700 "2024-01-11T18:35:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex-s-gardner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alex-s-gardner/32/30210_2.png) [@alex-s-gardner](https://discourse.julialang.org/u/alex-s-gardner)\
**Post date:** [January 11, 2024, 6:35pm UTC](https://discourse.julialang.org/t/how-to-update-function-coefficients-during-run-time/108700/1 "2024-01-11T18:35:51Z")

</div>

I need to define a function:

`ddx(v) = ((v[4] + v[6]) / 2 - v[5]) / X.^ 2`

where X is only known at run time and changes with each high level iteration of a loop.

I’ve tried using @eval:  
`@eval ddx(v) = ((v[4] + v[6]) / 2 - v[5]) / $(X.^ 2)`

but I get a `applicable method may be too new` error when running the program.

What is the proper way to create a compilable function where the coefficients change at run time?

---

<div class="post-metadata">

**Author:** ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)\
**Post date:** [January 11, 2024, 6:49pm UTC](https://discourse.julialang.org/t/how-to-update-function-coefficients-during-run-time/108700/2 "2024-01-11T18:49:50Z")

</div>

Are you sure the original version you wrote (without `@eval`) doesn’t already do what you want? I would expect it is, making a closure over `X` so that any changes to the variable `X` are seen in its run-time evaluation.

If not, you could do something like the following:

```julia
_ddx(v,X) = ((v[4] + v[6]) / 2 - v[5]) / X.^ 2 # define once

ddx = let X=X;
    v -> _ddx(v,X)
end # run this every loop to capture the updated X

```

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [January 11, 2024, 7:52pm UTC](https://discourse.julialang.org/t/how-to-update-function-coefficients-during-run-time/108700/3 "2024-01-11T19:52:52Z")

</div>

> [@alex-s-gardner](#):
>
> What is the proper way to create a compilable function where the coefficients change at run time?

In case you really _do_ want to invoke the compiler (which might be the case if a high-level iteration of your loop takes a long time to execute, otherwise it wouldn’t be worth it performance-wise), here are two options.

Note that `isbits(X)` has to hold if you want to move `X` into the type domain, so in practice it probably has to be a static array, as in StaticArrays.jl.

Untested:

```julia
# Example function

some_function_scalar(x, a, b, c) = ((a + b)/2 - c) / x .^ 2

function some_function_vector(x, v)
  (a, b, c) = v
  some_function_scalar(x, a, b, c)
end

# Option 1, move `X` into the type domain

compiled1_impl(f::F, ::Val{x}) where {F, x} = let f = f
  v -> f(x, v)
end

compiled1(f::F, x) where {F} = compiled1_impl(f, Val(x))

# Option 2, move the entire function into the type domain

closure(f::F, x) where {F} = let f = f, x = x
  v -> f(x, v)
end

compiled2_impl(::Val{f}) where {f} = v -> f(v)
compiled2(f::F) where {F} = compiled2_impl(Val(f))
compiled2(f::F, x) where {F} = compiled2(closure(f, x))

```

Then you can create your compileable function like `compiled1(some_function_vector, x)` or `compiled2(some_function_vector, x)`, and it will get compiled (with `x` as a compile-time constant) the first time you run it.

Keep in mind that you also need to use function barriers properly (see the [_Performance tips_](https://docs.julialang.org/en/v1/manual/performance-tips/) page in the Manual), so your inner loops will have to be in their own functions.

See these two previous threads for more:

> [@Tricks to compile away step in calculation?](https://discourse.julialang.org/t/tricks-to-compile-away-step-in-calculation/105760):
>
> Is there a way to compile away a step in a calculation internal to some function called in a closure but without manually decomposing the function into its constituent calculations within the closure? Below I provide a MWE in OLS because this is the simplest example I could think of: suppose we are going to run a loop of regressions using the same covariates but different outcome variables. We use a closure to create a new function that treats covariates as a constant. my\_pinv(X) = (X'X)^-1 \*…

> [@Some notes on performance of callable structs](https://discourse.julialang.org/t/some-notes-on-performance-of-callable-structs/108042):
>
> Hello together. I want to showcase some performance characteristics and slight problems with ‘homemade closures’. By that I mean some kind of struct that carries some data and has a call method in which this data is used, something like Base.Fix1. In case you don’t know what that is, here is the (simplified) definition for it (from operators.jl) struct Fix1{F,T} \<: Function f::F x::T end (f::Fix1)(y) = f.f(f.x, y) I got send down this rabbit hole after starting to toy with the packag…
