Short Question:
I have a Integer variable N and would like to write a macro to produce a single dummy variable i_($N).
An attempt:
@generated function testfunc(N)
:(i_($N))
end
testfunc(5) # Desired behavior i_5
ERROR: UndefVarError: i_ not defined
in testfunc at none:2
Longer explanation:
I recently discovered Base.Cartesian in Julia. It has some handy tricks for generating dummy variables for indexing multidimensional arrays.
The @ntuple macro in Cartesian can produce sequences starting at 1. For example, @ntuple 5 k->i_k produces (i_1,i_2,i_3,i_4,i_5). Inside an @generated function, if W=5 then @ntuple ($W) k->i_k will produce the same sequence. This doesn't work: @ntuple 1 k->i_(k+$W)
I can't figure out a way to just produce, e.g. i_3 if N=3 (this could be inside an @generated function).
My end goal is to use @nloops in Cartesian to loop over a sequence of dummy variables and store the results in some storage vector indexed by one of the dummy variables, e.g. something like:
@nloops ($W) i A begin
# Example generated code for N=2, W=3:
# storage[i_2] *= A[i_1, i_2, i_3]
storage[i_($N)] *= A[(@ntuple ($W) k->i_k)...]
end