I don’t think so; your way of determining the loop ranges is a little bit
too specific.
The only two ways of generating loop bounds is either from a variable
referencing an array (or array-like object), in which case the bounds will
be determined by the size of the array, or with an anonymous function. The
anonymous function allows for a limited set of rewrite-rules based on the
dimension (which is its input), but it doesn’t allow referencing another
dimension AFAICS. The closest I could get was this, which references the
wrong dimension:
julia> macroexpand(quote
@nloops 3 i d->0:n-i_d begin
sum(@ntuple 3 i)
end
end)
quote # In[15], line 2:
begin # cartesian.jl, line 31:
for i_3 = 0:n - i_3 # cartesian.jl, line 32:
nothing # cartesian.jl, line 33:
begin # cartesian.jl, line 31:
for i_2 = 0:n - i_2 # cartesian.jl, line 32:
nothing # cartesian.jl, line 33:
begin # cartesian.jl, line 31:
for i_1 = 0:n - i_1 # cartesian.jl, line 32:
nothing # cartesian.jl, line 33:
begin # In[15], line 3:
sum((i_1,i_2,i_3))
end # cartesian.jl, line 34:
nothing
end
end # cartesian.jl, line 34:
nothing
end
end # cartesian.jl, line 34:
nothing
end
end
end
I guess you’ve already found it, but the full docs of Base.Cartesian are
enlightening, especially the reference at the bottom:
http://docs.julialang.org/en/release-0.4/devdocs/cartesian/#devdoc-cartesian-reference
// T
On Thursday, January 7, 2016 at 11:10:18 AM UTC+1, Asbjørn Nilsen Riseth
wrote:
I would like to loop over all the \beta_i values of the following tree.
>
>
> <https://lh3.googleusercontent.com/-EBjanFs-BC4/Vo409IFAiTI/AAAAAAAAMEw/U7iqEv9nPmU/s1600/tree.png>
>
> What is the best Julia way of doing this? I'm also interested in
> generalising this to N levels.
>
>
> For the given tree, the following works. *Is there a way to use @nloops
> to shorten this?*
> n = Int(1/0.2)
> for i_3 = 0:n
> for i_2 = 0:n-i_3
> for i_1 = 0:n-i_2-i_3
> i_0 = n-sum(@ntuple 3 i)
> β = [i_3, i_2, i_1, i_0]/n
> @show β
> end
> end
> end
>
>
>