# Best practices for dealing with functions taking multiple vector variables

**URL:** <https://discourse.julialang.org/t/best-practices-for-dealing-with-functions-taking-multiple-vector-variables/86759>\
**Category:** General Usage\
**Tags:** argument\
**Created:** [September 4, 2022, 2:37pm UTC](https://discourse.julialang.org/t/best-practices-for-dealing-with-functions-taking-multiple-vector-variables/86759 "2022-09-04T14:37:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![samerb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samerb/32/9242_2.png) [@samerb](https://discourse.julialang.org/u/samerb)\
**Post date:** [September 4, 2022, 2:37pm UTC](https://discourse.julialang.org/t/best-practices-for-dealing-with-functions-taking-multiple-vector-variables/86759/1 "2022-09-04T14:37:01Z")

</div>

Sometimes I want to use a function like f(x,y,z,w), where x,y,z,w are themselves vectors of potentially different lengths. But sometimes when I call solvers (e.g. NLSolve, Optim, Implicit Differentiation, JuMP) or other functions from packages, they want me to pass in a function with just one argument or perhaps 2 arguments, but I think not, say, 4 arguments.

What is the recommended way to deal with this? I could join x,y,z,w into one big vector using `vcat` and then pass it into the solvers as a single argument, but then it might be annoying to have to keep track of which part parts of the concatenated vector correspond to which of the four initial vectors.

I know this is a broad question, but does anyone have suggestions on this? Thanks

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [September 4, 2022, 2:44pm UTC](https://discourse.julialang.org/t/best-practices-for-dealing-with-functions-taking-multiple-vector-variables/86759/2 "2022-09-04T14:44:35Z")

</div>

Usually, I do the following:

`optimize(x -> f(x, y, z, w), args...)`

where `x` is a vector of parameters to be optimized. There might be more performant ways of passing additional arguments.

If x,y, z and w are parameters that you are optimizing, I think they need to be put into a single vector.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [September 4, 2022, 2:56pm UTC](https://discourse.julialang.org/t/best-practices-for-dealing-with-functions-taking-multiple-vector-variables/86759/3 "2022-09-04T14:56:18Z")

</div>

If all are variables of your problem, you may have to put them in a single vector. You could do that and create something like:

```julia
vars = (
    x = @view(vec[1:10]),
    y = @view(vec[11:30]),
    etc.
)

```

Where `vec` is the complete vector. Then you can access each set with `vars.x`, `vars.y`, etc.

> [@Christopher\_Fisher](#):
>
> optimize(x → f(x, y, z, w), args…)

Be careful to put all that inside a function, such that `y, z, w` are not globals.

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [September 4, 2022, 3:17pm UTC](https://discourse.julialang.org/t/best-practices-for-dealing-with-functions-taking-multiple-vector-variables/86759/4 "2022-09-04T15:17:16Z")

</div>

> [@lmiq](#):
>
> Be careful to put all that inside a function, such that `y, z, w` are not globals.

I think that is good advice. If I remember correctly, another possibility is to return a new function that captures the other arguments, but there is a bug causing performance issues. I think it might be [Fix type instability of closures capturing types (2) by simeonschaub · Pull Request #40985 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/40985)

But in principle, I think you could also do the following once the bug is fixed:

```julia
gen(x, y, z, w) = x -> f(x, y, z, w)
f = gen(x, y z, w)
optimize(f, args...)

```

Is that true, or am I misunderstanding?

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [September 4, 2022, 3:36pm UTC](https://discourse.julialang.org/t/best-practices-for-dealing-with-functions-taking-multiple-vector-variables/86759/5 "2022-09-04T15:36:42Z")

</div>

I think it should be

> [@Christopher\_Fisher](#):
>
> ```julia
> gen(y, z, w) = x -> f(x, y, z, w)
> f = gen(y, z, w)
> optimize(f, args...)
> 
> ```

Still, this to be inside a function, otherwise `f` is a non constant global.

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [September 4, 2022, 3:50pm UTC](https://discourse.julialang.org/t/best-practices-for-dealing-with-functions-taking-multiple-vector-variables/86759/6 "2022-09-04T15:50:31Z")

</div>

[ArrayPartition](https://recursivearraytools.sciml.ai/stable/array_types/#RecursiveArrayTools.ArrayPartition) from RecursiveArrayTools is a nice way to handle this. It behaves like the concatenated array but allows access to the original arrays by property name.
