I need to write some equations that loop over several dimensions, and I created my own function getData(parName,dim1,dim2)
and setData(value,dim1,dim2)
that, using anonymous functions, I dynamically wrapped to parName(dim1,dim2)
and parName!(value,dim1,dim2)
.
So I can now write my model as:
[par1!( par2(d1,d2)+par3(d1,d2) ,d1,d2,dfix3) for d1 in DIM1, d2 in DIM2]
where the first argument of par1!()
function is the value that I want to store.
Now, it’s still not very inspiring, so I am trying to make this syntax a bit more easy to write (and to read/maintain).
Specifically, I am trying to write a macro that let’s write the equation as:
@meq par1!(d1 in DIM1, d2 in DIM2, dfix3) = par2(d1,d2)+par3(d1,d2)
That’s sexy!
While I have used macros in julia, it’s the first time I am trying to write one.
So far, I managed to write:
macro meq(eq)
dump(eq)
lhs_par = eq.args[1].args[1]
rhs = eq.args[2]
lhs_dims = eq.args[1].args[2:end]
loop_counters = [d.args[2] for d in lhs_dims if typeof(d) == Expr]
loop_sets = [d.args[3] for d in lhs_dims if typeof(d) == Expr]
loop_wholeElements = [d for d in lhs_dims if typeof(d) == Expr]
lhs_dims_placeholders = []
for d in lhs_dims
if typeof(d) == Expr
push!(lhs_dims_placeholders,d.args[2])
else
push!(lhs_dims_placeholders,d)
end
end
outExp = quote
[$(lhs_par)($(rhs),$(lhs_dims_placeholders ...)) for $(loop_wholeElements ...) ]
end
return outExp
end
While I think “I am almost there”, unfortunately the above code return a syntax error (“invalid iteration specification”) due to the for $(loop_wholeElements)
part… indeed I don’t know how to expand the expressions in lhs_dims_placeholders and loop_wholeElements in order to “assemble” the expanded expression…