Consider a simple function
g[x_,y_,z_] := x + y + z;
(indeed, any multivariate function g will illustrate the same thing). And suppose we're given a list of list of the form,
list = { {1,2,3}, {4,5,6} }
Without using Compile, if I want a result of the form { g[1,2,3], g[4,5,6] }, I can use Apply to get,
resultlist = g @@@ list
Question: Suppose that I want to write a compiled, listable, version of g, such that,
g[list] = { g[1,2,3], g[4,5,6] }
How would we achieve the same result? I'd attempted to write a Listable of the form,
compileG = Compile[ {x,y,z},
x + y + z,
RuntimeAttributes -> {Listable}
]
then it'll give me the error CompiledFunction::cfct: Number of arguments 1 does not match the length 3 of the argument template. Of course, in this circumstance, I know that compileG @@@ list will still work.
However, in the actual application I have in mind, the g function will be involved and list is quite large, and hence, I want to take advantage of these options RuntimeAttributes -> {Listable}, CompilationTarget -> "C", Parallelization -> True (which, at least according to here http://www.walkingrandomly.com/?p=3635, gives a very significant speed boost.)
Compilefunction). Open up the docs and see the right way to do it or read the WalkingRandomly article thoroughly. – Sektor Aug 18 '15 at 08:31Compilefunction correctly? I'd read through the documentations (https://reference.wolfram.com/language/ref/RuntimeAttributes.html) and the WalkingRandomly article. The toy functioncompileGwill indeed compute – user32416 Aug 18 '15 at 08:42compileG = Compile[{{x, _Real, 1}}, Total[x], RuntimeAttributes -> {Listable}]thencompileG[{{1, 2, 3}, {4, 5, 6}}]prints{6., 15.}– Sektor Aug 18 '15 at 09:07g[x_List] := Total[x];SetAttributes[g,Listable]instead. We would get{{g[1],g[2],g[3]},{g[4],g[5],g[6]}}– LLlAMnYP Dec 07 '16 at 12:01RuntimeAttributes: "If a compiled function with Listable attribute receives any arguments with higher rank than specified, the function will thread over these arguments." See this and this for more discussion. – Michael E2 Mar 09 '17 at 15:08compileG = Compile[{x, y, z}, x + y + z, RuntimeAttributes -> {Listable}]; compileG[{1, 4}, {2, 5}, {3, 6}]– LLlAMnYP Mar 09 '17 at 15:30