Re: Question: Compile in Mathematica 8.0
- To: mathgroup at smc.vnet.net
- Subject: [mg114708] Re: Question: Compile in Mathematica 8.0
- From: Oliver Ruebenkoenig <ruebenko at wolfram.com>
- Date: Mon, 13 Dec 2010 06:34:23 -0500 (EST)
- References: <201012110652.BAA29709@smc.vnet.net>
On Sat, 11 Dec 2010, Asim wrote:
> Hi
>
> I am not sure why the first function named v works and why the second
> function named v1 does not compile. The only difference between the
> two functions is in the variable called len which controls the number
> of random numbers that are generated.
>
> I am using Mathematica 8.0 on Windows Vista machine.
>
> Thanks
>
> Asim
>
>
>
> First function:
>
>
> v = Compile[{{n, _Integer}},
> Module[
> {g, len},
> len = n*(n - 1);
> g = RandomVariate[NormalDistribution[0., 1.], len]
> ]
> ]
>
> Output is
>
> CompiledFunction[{n},Module[{g,len},len=n
> (n-1);g=RandomVariate[NormalDistribution[0.,1.],len]],-CompiledCode-]
>
> v[3]
>
> {0.0866864,-0.271121,-0.431317,-0.6787,-1.29246,0.550486}
>
> Second Function:
>
> v1 = Compile[{{n, _Integer}},
> Module[
> {g, len},
> len = n*(n - 1)/2;
> g = RandomVariate[NormalDistribution[0., 1.], len]
> ]
> ]
>
> CompiledFunction[{n},Module[{g,len},len=1/2 n
> (n-1);g=RandomVariate[NormalDistribution[0.,1.],len]],-CompiledCode-]
>
> v1[3]
>
> During evaluation of In[42]:= CompiledFunction::cfse: Compiled
> expression {-0.325949,1.72381,0.368231} should be a machine-size
> integer. >>
>
> During evaluation of In[42]:= CompiledFunction::cfex: Could not
> complete external evaluation at instruction 8; proceeding with
> uncompiled evaluation. >>
>
> Out[42]= {0.295626, 0.664446, 0.654626}
>
>
>
>
>
Asim,
consider this function
f = Function[{n}, Module[{g, len}, len = (n*(n - 1) + 1)/2;
Print[len];
g = RandomVariate[NormalDistribution[0., 1.], len]]]
f[11]
the compiler has no way of knowing that n*(n-1) is always _Integer. You
can help it with Floor or Round.
Oliver
- References:
- Question: Compile in Mathematica 8.0
- From: Asim <maa48@columbia.edu>
- Question: Compile in Mathematica 8.0