This is a follow-up question to Interpolating literals into each expression in an array using a for loop . In that question, I wanted to interpolate a 1D array (vector) of expressions in a loop. Using the accepted answer:
julia> array_of_expr = Any[:(A * 5), :(B * 6 * T)]
julia> arglist = [:A; :B; :T]
julia> test_input = [1e5, 1e1, 100]
julia> @eval begin
function interpolate_1by1($(arglist...))
result_array = similar($array_of_expr)
$((quote
result_array[$i] = $(array_of_expr[i])
end for i in 1:length(array_of_expr))...)
return result_array
end
end
julia> interpolate_all(test_input...)
2-element Array{Float64,1}:
500000.0
6000.0
Working off of the accepted answer, suppose I now have an array of vectors of expressions:
args = [:A, :B, :C]
args_typed = [:($s::Float64) for s in args]
big_expr_array = Array{Vector{Expr}}(undef, 3, 4)
big_expr_array[1, :] = [[:(A+B), :(C/A)], [:(B+1), :(A+C)], [:(B%5)], [:(C+7)]]
big_expr_array[2, :] = [[:(A*B+C), :(B*C+A)], [:(C/6), :(C+C)], [:(3*C)], [:(A%2)]]
big_expr_array[3, :] = [[:(A*C), :(A*B)], [:(A-6), :(B+C/A)], [:(B+2)], [:(C%3)]]
and want to evaluate all the expressions given some arguments, without losing the overall structure of the array. It seems to me I have to loop over the i
rows, j
columns, and k
elements (expression vectors) in the array.
I have tried to modify the accepted answer to the previous question in two ways, both of which produce errors:
@eval begin
function looped_eval1($(args_typed...))
result = similar($big_expr_array)
$( (quote
$( (quote
$( (quote
# i = row, j = vector, k = specific expression
result[$i, $j][$k] = $(big_expr_array[i, j][k])
end for k in 1:length(big_expr_array[i, j]))...
)
end for j in 1:size(big_expr_array)[2])...
)
end for i in 1:size(big_expr_array)[1])...
)
end
end
this throws UndefRefError: access to undefined reference
.
Second:
@eval begin
function looped_eval2($(args_typed...))
result = similar($big_expr_array)
$( (quote (quote (quote
# i = row, j = vector, k = specific expression
result[$i, $j][$k] = $(big_expr_array[i, j][k])
end for k in 1:length(big_expr_array[i, j]))...
end for j in 1:size(big_expr_array)[2])...
end for i in 1:size(big_expr_array)[1])...
)
end
end
this throws syntax: "..." expression outside call around In[130]:5
.
An example desired result for the arguments A = 10, B = 20, C = 30:
big_expr_array[1, :] = [[30, 3], [21, 40], [0], [37]]
big_expr_array[2, :] = [[230, 610], [5, 60], [90], [0]]
big_expr_array[3, :] = [[300, 200], [4, 23], [22], [0]]
How should I write the manual loop unroll to accommodate these nested arrays? Or, is there a more elegant way to achieve evaluation of all of these expressions and maintain the same array structure?
(It's also possible that the code may assign to result
correctly--since I'm stuck on the loop unroll, I'm not sure.)