MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Question: Compile in Mathematica 8.0

  • To: mathgroup at smc.vnet.net
  • Subject: [mg114714] Re: Question: Compile in Mathematica 8.0
  • From: Patrick Scheibe <pscheibe at trm.uni-leipzig.de>
  • Date: Tue, 14 Dec 2010 06:53:49 -0500 (EST)

Hi again,

I have to make this clear since I just read my own message on the list:

Of course, n*(n - 1)/2 *is* always an integer when n is one. But that's
not what I wanted to express. What I wanted to say is that it depends on
how this expression is evaluated whether there appear non-integer values
during the evaluation. Look e.g. at

In[5]:= FullForm[n*(n-1)/2]
Out[5]//FullForm= Times[Rational[1,2],Plus[-1,n],n]

But even when Rational[1,2] would not appear... who knows in which order
the expression is evaluated in the compiled version? Maybe like that

n*((n-1)/2)

The rest of my original message stays hopefully right.

Cheers
Patrick


On Mon, 2010-12-13 at 03:54 -0500, Patrick Scheibe wrote:
> Hi,
> 
> the situation is simple. What should the compiler assume, when it sees
> something like this:
> 
> len = n*(n - 1)/2
> 
> Is it in any case an Integer? No. So lets give Compile a hint, that we
> want len to be an integer:
> 
> Compile[{{n, _Integer}},
>  Module[
>   {len = 0},
>   len = n*(n - 1)/2;
>   ]
>  ]
> 
> and now Compile tell us instantly:
> 
> Compile::cset: "Variable len of type \!\(\"_Integer\"\) encountered in
> assignment of type \!\(\"_Real\"\)."
> 
> So what you want is maybe:
> 
> v1 = Compile[{{n, _Integer}}, Module[
>    {g, len = 0},
>    len = Quotient[n*(n - 1), 2];
>    g = RandomVariate[NormalDistribution[0., 1.], len]]]
> 
> and everthing is fine.
> 
> In[9]:= v1[3]
> 
> Out[9]= {0.884713, 0.228091, 0.874789}
> 
> Hope this helps.
> 
> Cheers
> Patrick
> 
> On Sat, 2010-12-11 at 01:52 -0500, 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}
> > 
> > 
> > 
> > 
> 
> 



  • Prev by Date: Re: Foo /: Plus[b_Foo] := b
  • Next by Date: Re: Puzzled by IntegerPart
  • Previous by thread: Re: Question: Compile in Mathematica 8.0
  • Next by thread: Re: Replacement Rule with Sqrt in denominator. Also Bug in Series; see Semantica / Harris solution