循环展开?在 Julia 中使用元编程
Loop unrolling? in Julia with metaprogramming
有没有办法"metaprogrammatically"获得具有以下结构的代码块:
if r1 < R1
s = 1
elseif r1 < R2
s = 2
... etc until N
end
end
谢谢!
查看Base.Cartesian.@nif
我认为这对你有用...
julia> macroexpand(:(@nif 10 d->(r1 < R_d) d->begin s=d; break end))
:(if r1 < R_1 # REPL[9], line 1:
s = 1 # REPL[9], line 1:
break
else
if r1 < R_2 # REPL[9], line 1:
s = 2 # REPL[9], line 1:
break
else
if r1 < R_3 # REPL[9], line 1:
s = 3 # REPL[9], line 1:
break
else
if r1 < R_4 # REPL[9], line 1:
s = 4 # REPL[9], line 1:
break
else
if r1 < R_5 # REPL[9], line 1:
s = 5 # REPL[9], line 1:
break
else
if r1 < R_6 # REPL[9], line 1:
s = 6 # REPL[9], line 1:
break
else
if r1 < R_7 # REPL[9], line 1:
s = 7 # REPL[9], line 1:
break
else
if r1 < R_8 # REPL[9], line 1:
s = 8 # REPL[9], line 1:
break
else
if r1 < R_9 # REPL[9], line 1:
s = 9 # REPL[9], line 1:
break
else # REPL[9], line 1:
s = 10 # REPL[9], line 1:
break
end
end
end
end
end
end
end
end
end)
有没有办法"metaprogrammatically"获得具有以下结构的代码块:
if r1 < R1
s = 1
elseif r1 < R2
s = 2
... etc until N
end
end
谢谢!
查看Base.Cartesian.@nif
我认为这对你有用...
julia> macroexpand(:(@nif 10 d->(r1 < R_d) d->begin s=d; break end))
:(if r1 < R_1 # REPL[9], line 1:
s = 1 # REPL[9], line 1:
break
else
if r1 < R_2 # REPL[9], line 1:
s = 2 # REPL[9], line 1:
break
else
if r1 < R_3 # REPL[9], line 1:
s = 3 # REPL[9], line 1:
break
else
if r1 < R_4 # REPL[9], line 1:
s = 4 # REPL[9], line 1:
break
else
if r1 < R_5 # REPL[9], line 1:
s = 5 # REPL[9], line 1:
break
else
if r1 < R_6 # REPL[9], line 1:
s = 6 # REPL[9], line 1:
break
else
if r1 < R_7 # REPL[9], line 1:
s = 7 # REPL[9], line 1:
break
else
if r1 < R_8 # REPL[9], line 1:
s = 8 # REPL[9], line 1:
break
else
if r1 < R_9 # REPL[9], line 1:
s = 9 # REPL[9], line 1:
break
else # REPL[9], line 1:
s = 10 # REPL[9], line 1:
break
end
end
end
end
end
end
end
end
end)