# How to register functions from an array in JuMP?

**URL:** https://discourse.julialang.org/t/how-to-register-functions-from-an-array-in-jump/6884
**Category:** Optimization (Mathematical)
**Tags:** question
**Created:** [November 4, 2017, 7:42pm UTC](https://discourse.julialang.org/t/how-to-register-functions-from-an-array-in-jump/6884 "2017-11-04T19:42:55Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)
#### Post date: [November 4, 2017, 7:42pm UTC](https://discourse.julialang.org/t/how-to-register-functions-from-an-array-in-jump/6884/1 "2017-11-04T19:42:55Z")

</div>

The signature to register a function in JuMP is as follows

```julia
JuMP.register(m::Model, s::Symbol, dimension::Integer, f::Function, ∇f::Function, ∇²f::Function)

```

I have an array of unnamed functions, like:

```julia
funs = [x -> x^2, x -> x + 1]

```

that I need to use in the definition of a non-linear programming problem. _A priori_ I do not know what functions I will receive in the array, therefore I need my code to be generic.

I tried to register them like this:

```julia
for (i,f) in enumerate(funs)
    JuMP.register(m, Symbol("f$i"), 1, f, autodiff=true)
end

```

But then I can’t use them, in macros like @NLobjective:

```julia
JuMP.@NLobjective(m, Min, eval(Symbol("f1")) + 100(y-x^2)^2)

```

gives an error. Is there any other way to do this?

---

<div class="post-metadata">

### Author: ![miles.lubin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/miles.lubin/32/279_2.png) [@miles.lubin](https://discourse.julialang.org/u/miles.lubin)
#### Post date: [November 4, 2017, 9:24pm UTC](https://discourse.julialang.org/t/how-to-register-functions-from-an-array-in-jump/6884/2 "2017-11-04T21:24:05Z")

</div>

You can use the [raw expression input](http://www.juliaopt.org/JuMP.jl/0.18/nlp.html#raw-expression-input) format instead of `@NLobjective`.

But I’m a bit confused, why not write `f1` instead of `eval(Symbol("f1"))`?

---

<div class="post-metadata">

### Author: ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)
#### Post date: [November 4, 2017, 11:35pm UTC](https://discourse.julialang.org/t/how-to-register-functions-from-an-array-in-jump/6884/3 "2017-11-04T23:35:38Z")

</div>

@miles.lubin Because I do not know the length of `funs` _a priori_. It could be 10, or 1000, so I need to do loops. For example, I would define an NLobjective that sums `f1(x) + f2(x) + ....`. I might also need to do constrains that involve some of these functions. To do all of this the only way I can think of is somthing like `eval(Symbol("f$i"))`

---

<div class="post-metadata">

### Author: ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)
#### Post date: [November 5, 2017, 8:28pm UTC](https://discourse.julialang.org/t/how-to-register-functions-from-an-array-in-jump/6884/4 "2017-11-05T20:28:22Z")

</div>

related:

> [@Slurping and splatting in JuMP](https://discourse.julialang.org/t/slurping-and-splatting-in-jump/6892):
>
> I need to define a function that takes a large number of arguments. I am trying to follow JuMP documentation: All arguments to user-defined functions are scalars, not vectors. To define a function which takes a large number of arguments, you may use the splatting syntax f(x…) = … So did this, but it does not work: m = JuMP.Model(solver=Ipopt.IpoptSolver()) f(x...) = x[1] + x[2] JuMP.register(m, :f, 2, f, autodiff=true) JuMP.@variable(m, x[i=1:2], start = 0.) JuMP.@NLobjective(m, :Min, f(x..…

> <https://stackoverflow.com/questions/44710900/juliajump-variable-number-of-arguments-to-function>
