I have a simple function that I've written as both a pure function and a compiled pure function, but the compiled version produces different results, suggesting either I don't know how to get it to work with Compile
or that Compile
has some limitations I didn't know about. I intend to give the function two arguments, the first is a single real number, and the second is a list of integers.
fun = Apply[Times, #1 - Range[#2], {1}] &;
fc = Compile[
{{value, _Real},
{counts, _Integer, 1}},
Apply[Times, #1 - Range[#2], {1}] &[value, counts]
];
fun[5, {1, 2, 3}]
fc[5, {1, 2, 3}]
The results I obtain are
{4, 12, 24}
{4., 3., 2.}
The results I want to obtain are those produced by the non-compiled function.
I know I could rewrite the function as follows to produce the results that I want (thanks, @glS).
fcV2 = Compile[
{{value, _Real},
{counts, _Integer, 1}},
Apply[Times, value - Range@# ] & /@ counts
];
Why doesn't my first attempt at a compiled function work as expected? Is there a way to fix it without explicit mapping?
Range[{1,2,3}]
(uncompiled) does not produce a rectangular array. (2) InsideCompile[]
,Range[{1,2,3}]
produces{1,2,3}
. $\endgroup$