Compare two ways of computing a table of values of a multivariable Gaussian:
First@AbsoluteTiming[
ta = Table[Exp[-Total[Array[r, 5]^2]],
{r[1], 1., 2.},
{r[2], 1., 3.},
{r[3], 1., 3.},
{r[4], 1., 23.},
{r[5], 1., 421.}
];
]
(* 2.1069 *)
comp = Compile[{},
Table[Exp[-(r[1]^2 + r[2]^2 + r[3]^2 + r[4]^2 + r[5]^2)],
{r[1], 1., 2.},
{r[2], 1., 3.},
{r[3], 1., 3.},
{r[4], 1., 23.},
{r[5], 1., 421.}
], CompilationTarget->"C"
];
First@AbsoluteTiming[
tb = comp[];
]
(* 0.002853 *)
I would like to get such a speedup in a case where I specify the number of iterators/variables programmatically, and where the iterator values are stored in a list itvals
. Non-compiled is easy, e.g.:
n = 5; (* number of iterators *)
itvals = RandomReal[{0,1}, {n, 8}];
tc = Fold[Table[#1,{r[#2],itvals[[#2]]}]&, Exp[-Total[Array[r,n]^2]], Range[n]];
Or:
td = Table[Exp[-Total[Array[r,n]^2]],##] & @@ Table[{r[i], itvals[[i]]}, {i,n,1,-1}];
tc == td
(* True *)
But I can't get this to work inside Compile
! If I try
c1 = Compile[{{n, _Integer}},
Block[{itvals},
itvals = RandomReal[{0, 1}, {n, 5}];
Fold[Table[#1, {x[#2], itvals[[#2]]}] &, 1, Range[n]]
]
]
where I use the "function" 1 instead of the Gaussian for simplicity. CompilePrint[c1]
shows that the whole Fold
is wrapped in a MainEvaluate
. I can't do it similarly to td
either because Compile
only supports @@
with the functions Plus
, Times
or List
...
Anybody know a way to do it?