# How to create an user-defined function with multiple vector as inputs

**URL:** https://discourse.julialang.org/t/how-to-create-an-user-defined-function-with-multiple-vector-as-inputs/99068
**Category:** Optimization (Mathematical)
**Tags:** question, jump, nlopt
**Created:** [May 18, 2023, 3:11pm UTC](https://discourse.julialang.org/t/how-to-create-an-user-defined-function-with-multiple-vector-as-inputs/99068 "2023-05-18T15:11:32Z")
**Posts on this page:** 1
**Showing post:** 2

<div class="post-metadata">

### Author: ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)
#### Post date: [May 18, 2023, 8:40pm UTC](https://discourse.julialang.org/t/how-to-create-an-user-defined-function-with-multiple-vector-as-inputs/99068/2 "2023-05-18T20:40:21Z")

</div>

Hi @PatrickeTownsend, welcome to the forum.

I think you’re slightly off in how you’re you’re approaching JuMP’s user-defined functions.

With a slightly simpler example, your code looks something like this:

```julia
using JuMP
function state_f(x, y...)
    x[1] = sum(sin(y[i]) for i in 1:3)
    x[2] = sum(cos(y[i]) for i in 1:3)
end
model = Model()
register(model, :state_f, 4, state_f; autodiff = true)
@variable(model, x[1:2])
@variable(model, y[1:3])
state_f(x, y...)
@optimize(model, Min, sum(x))

```

This has a few things wrong with it:

- User-defined functions must take scalars as input
- User-defined functions must return a scalar
- User-defined functions must be used in `@NL` macros

This means that you cannot write a function like `StateFunc` which takes a vector as the first argument and modifies in-place, and it also means that calling `StateFunc` and `Collocation_state` outside the macros is not adding a constraint to the model. It’s just evaluating the function.

Relatedly:

> It is possible to create an user defined function with multiple arrays as inputs?

is “no” because you cannot have a user-defined function which takes even one array as input 😄.

To change my example somewhat, here’s something that you could write in JuMP:

```julia
using JuMP
state_f1(y...) = sum(sin(y[i]) for i in 1:3)
state_f2(y...) = sum(cos(y[i]) for i in 1:3)
model = Model()
register(model, :state_f1, 3, state_f; autodiff = true)
register(model, :state_f2, 3, state_f; autodiff = true)
@variable(model, x[1:2])
@variable(model, y[1:3])
@NLconstraint(model, x[1] == state_f1(y...))
@NLconstraint(model, x[2] == state_f2(y...))
@optimize(model, Min, sum(x))

```

Now writing out `state_f1`, `state_f2` etc can be painful. So you _can_ write functions which return a vector as output, but then you need to follow this tutorial: [Tips and tricks · JuMP](https://jump.dev/JuMP.jl/stable/tutorials/nonlinear/tips_and_tricks/#User-defined-functions-with-vector-outputs)

---

_[View the full topic](https://discourse.julialang.org/t/how-to-create-an-user-defined-function-with-multiple-vector-as-inputs/99068)._
