Creating a macro for defining differential equation constraints in NLOptControl.jl

@miles.lubin I know that you are probably very busy with the meetup, but I would greatly appreciate any help that you can spare with this, I finally got it working for for the scalar case:

  function test(n,exp,x)
    @eval begin
      x1=$x[1,1]
      x1=eval(x1);
      eq=@NLexpression($n,$exp)
    end
    return eq
  end

the above can be tested with this made-up example:

  using JuMP, Ipopt
  n = Model(solver=IpoptSolver(print_level=0))
  @variable(n,x[1:4,1:4])
  exp=test(n,:(sin(x1)),x)

The issue that I am having is that I am dealing with arrays of expressions, so I tried to extend the above to an N-d case:

  function test(n,exp_arr,x)
    code=quote
      x1=$x[1,1]
      x1=eval(x1);
      @NLexpression($n,$exp_arr[1])
      end
    return eval(code)
  end

which can be tested with:

  using JuMP, Ipopt
  n = Model(solver=IpoptSolver(print_level=0))
  @variable(n,x[1:4,1:4])
  exp_arr=[:(sin(x1)),:(sin(x1))]
  exp=test(n,exp_arr,x)

which fails with:

julia> exp=test(n,exp_arr,x)
ERROR: MethodError: no method matching parseNLExpr_runtime(::JuMP.Model, ::Expr, ::Array{ReverseDiffSparse.NodeData,1}, ::Int64, ::Array{Float64,1})
Closest candidates are:
  parseNLExpr_runtime(::JuMP.Model, ::Number, ::Any, ::Any, ::Any) at /home/febbo/.julia/v0.5/JuMP/src/parsenlp.jl:196
  parseNLExpr_runtime(::JuMP.Model, ::JuMP.Variable, ::Any, ::Any, ::Any) at /home/febbo/.julia/v0.5/JuMP/src/parsenlp.jl:202
  parseNLExpr_runtime(::JuMP.Model, ::JuMP.NonlinearExpression, ::Any, ::Any, ::Any) at /home/febbo/.julia/v0.5/JuMP/src/parsenlp.jl:208
  ...
 in eval(::Module, ::Any) at ./boot.jl:234
 in test(::JuMP.Model, ::Array{Expr,1}, ::Array{JuMP.Variable,2}) at ./REPL[9]:7

The difference is that between an array indexed at a spot and a the item at that spot in a quoted expression can be seen with this simple example:

julia> A=[:(2+3),:(4),:(9-8)];
julia> t=A[1];
julia> eval(quote 
       @show isequal($A[1],$t)
       @show $A[1]
       @show $t
       end)
isequal((Any[:(2 + 3),4,:(9 - 8)])[1],2 + 3) = false
(Any[:(2 + 3),4,:(9 - 8)])[1] = :(2 + 3)
2 + 3 = 5
5

I have tried all kinds of things (which I will not go into), and I cannot get things working. Again, any help would be very nice. Thanks