Nonlinear Constraints with Splatting

Please:

  1. Quote your code using triple backticks as described in PSA: how to quote code with backticks 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:

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.

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.

1 Like