# Nonlinear Constraints with Splatting

**URL:** https://discourse.julialang.org/t/nonlinear-constraints-with-splatting/8677
**Category:** Optimization (Mathematical)
**Tags:** question
**Created:** [January 29, 2018, 6:49pm UTC](https://discourse.julialang.org/t/nonlinear-constraints-with-splatting/8677 "2018-01-29T18:49:40Z")
**Posts on this page:** 1
**Showing post:** 7

<div class="post-metadata">

### Author: ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)
#### Post date: [January 29, 2018, 8:58pm UTC](https://discourse.julialang.org/t/nonlinear-constraints-with-splatting/8677/7 "2018-01-29T20:58:53Z")

</div>

Please:

1. Quote your code using triple backticks as described in [PSA: how to quote code with backticks](https://discourse.julialang.org/t/psa-how-to-quote-code-with-backticks/7530) to make it more readable, and
2. Provide enough data in your code to run it by just copying and pasting it to help people to help you.

Adapting the solution from the link you posted, in particular, the comment below, you can try:

```julia
foc_constraint = @eval @NLexpression(m, [i=1:length(q_a)], foc_constraint_i(index_vector[i], gamma, $(q_b...)))

```

It seems that the `@NLconstraint` macro cannot parse the splatting because it works when you manually splat, so you can intercept the expression by the `@eval` macro to “pseudo-manually” splat `q_b` for you before passing the expression to the `@NLconstraint` macro to do its magic on the splatted version. I didn’t test this solution because of point 2 above, so I may very well be wrong.

> [@Passing an array of variables to a user-defined non-linear function](https://discourse.julialang.org/t/passing-an-array-of-variables-to-a-user-defined-non-linear-function/4132/6):
>
> Perhaps what you are looking for can be achieved with @eval. For example, taking n = 3, the code looks like: m = Model(solver=IpoptSolver(print\_level=0)) n=3 @variable(m, 0 \<= x[1:n] \<= 1) f(x...) = rand() df(g,x...) = g[:] = rand(n) JuMP.register(m, :obj, n, f, df) The problematic @NLobjective could be specialized for n=3 as: @NLobjective(m, Max, obj(x[1],x[2],x[3])) and this would work. To make it work for other n defined at runtime, we could build-up this expression and @eval it, as foll…

Also consider doing away with this pattern completely if you can as @ExpandingMan mentioned because it simply looks ugly in my opinion and Julia gives you plenty of tools to make your code look elegant and often even be simultaneously more efficient.

---

_[View the full topic](https://discourse.julialang.org/t/nonlinear-constraints-with-splatting/8677)._
