I write a Macro to define the symbols with one index as following
macro varjs(x, j::Int64)
vars=Expr(:block)
for i = 1:j
push!(vars.args, Expr(:(=), esc(Symbol("$x$i")), Expr(:quote,Symbol("$x$i"))))
end
push!(vars.args, Expr(:tuple,map(esc, "$x".*map(string,1:j).|>Symbol)...))
vars
end
The macro is called as following
@varjs α 16
The output is
(:α1, :α2, :α3, :α4, :α5, :α6, :α7, :α8, :α9, :α10, :α11, :α12, :α13, :α14, :α15, :α16)
Each αi is defined as a symbol variable.
Then I plan to define the macro for multi-index variables. I have no ideal about this except for writing the “for loop” one by one. Is there any better way to realize the feature?
For example, 3-index varibales with three value for each index, the macro should looks like @varjs α 3 3 3 and automatically define the symbol varialbes α111, α112, α113,α121,α122,α123,α131,α132,α133,α211, α212, α213,α221,α222,α223,α231,α232,α233,α311, α312, α313,α321,α322,α323,α331,α332,α333.