Re: Compile arguments
- To: mathgroup at smc.vnet.net
- Subject: [mg64230] Re: [mg64176] Compile arguments
- From: Darren Glosemeyer <darreng at wolfram.com>
- Date: Tue, 7 Feb 2006 03:35:59 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
One possibility would be to write a function that constructs the InputForm
for the Compile input from strings. For instance, buildcompiledfun
constructs the expression (using Join as fc) based on a list of input
variables.
In[1]:= fc = Join;
In[2]:= vars = {x, y};
In[3]:= Compile[{{x, _Real, 1}, {y, _Real, 1}},
Apply[fc, vars], {{Apply[fc, vars], _Real}}]
Compile::cpapot: Compilation of fc @@ vars can only proceed when fc
is Times, Plus or List; evaluation will use the uncompiled function.
Out[3]= CompiledFunction[{x, y}, fc @@ vars, -CompiledCode-]
In[4]:= buildcompiledfun[vars_] :=
ToExpression[
"Compile[" <> ToString[InputForm[Map[{#, _Real, 1} &, vars]]] <>
"," <>
ToString[InputForm[Apply[fc, vars]]] <> ",{{" <>
ToString[InputForm[Apply[fc, vars]]] <> ",_Real}}]"]
buildcompiledfun[vars] is now a compiled function that evaluates for
Length[vars] real vector arguments.
In[5]:= buildcompiledfun[{a, b}][{1.}, {2.}]
Out[5]= {1., 2.}
In[6]:= vals = Table[{i}, {i, 10}]
Out[6]= {{1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}}
For an unspecified length, the list of variables for buildcompiledfun can
be constructed based on the length of vals and the resulting compiled
function applied to vals.
In[7]:= Apply[buildcompiledfun[Table[Unique[x], {Length[vals]}]], vals]
Out[7]= {1., 2., 3., 4., 5., 6., 7., 8., 9., 10.}
Darren Glosemeyer
Wolfram Research
At 04:13 AM 2/4/2006 -0500, gcer wrote:
>Hi,
>
>Function fc is already compiled numerical function. When I use the
>following compilatinon line to build new function gc it compiles ok:
>
>gc=Compile[{{x,_Real,1},{y,_Real,1}},fc[x,y],{{fc[x,y],_Real}}],
>
>but since, the function fc has more than just two arguments (25 to 30,
>two arguments are used for clarity) and I would like to compile the
>whole polinom of fc functions :
>
>vars={x,y};
>blanks={_,_};
>Compile[{{x,_Real,1},{y,_Real,1}},Apply[fc,vars],{{Apply[fc,blanks],_Real}}],
>
>It does not compile and I get following error line:
>
>"Compile::cpapot: Compilation of fc@@vars can only proceed when fc is
>Times, \Plus or List; evaluation will use the uncompiled function."
>
>With best regards,
>
>Gregor Cernivec