MathGroup Archive 2011

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

Search the Archive

Re: Evaluation control in Compile[]

  • To: mathgroup at smc.vnet.net
  • Subject: [mg119333] Re: Evaluation control in Compile[]
  • From: Ben <benp84 at gmail.com>
  • Date: Tue, 31 May 2011 07:45:45 -0400 (EDT)
  • References: <irvrqm$8hh$1@smc.vnet.net> <is005s$9or$1@smc.vnet.net>

On May 30, 5:48 am, Albert Retey <a... at gmx-topmail.de> wrote:
> Am 30.05.2011 12:33, schrieb Ben:
>
>
>
>
>
>
>
>
>
> > I'd like to compile a function that estimates (assuming a spherical
> > earth) the length of a degree of longitude at any particular
> > latitude.  Here's how I write it up and test its speed:
>
> > In[1]:=  radiusOfEarth=6378100.;
> > latitudeDegreeLength=2*Pi*radiusOfEarth/360
> > Out[2]= 111319.
> > In[3]:= X=Table[RandomReal[{0,90}],{100000}];
> > In[4]:=
> > GetLongitudeDegreeLength=Compile[{latitude},latitudeDegreeLength*Cos[latitu de
> > Degree]]
> > Out[4]= CompiledFunction[{latitude},latitudeDegreeLength Cos[latitude
> > =B0],-CompiledCode-]
> > In[5]:= Timing[Map[GetLongitudeDegreeLength,X];]
> > Out[5]:= {0.312,Null}
>
> > Since the values of "latitudeDegreeLength" and "Degree" are constant,
> > I'd prefer to use numerical approximations in the definition so the
> > compiled code doesn't have to request the values of these global
> > variables at runtime.  Like this:
>
> > In[6]:= GetLongitudeDegreeLength=Compile[{latitude},
> > 111318.*Cos[0.0174532925 latitude]]
> > Out[6]:= CompiledFunction[{latitude},111318. Cos[0.0174533 latitude], -
> > CompiledCode-]
> > In[7]:= Timing[Map[GetLongitudeDegreeLength,X];]
> > Out[7]:= {0.031,Null}
>
> > Is there any way to tell Mathematica that I'd like these symbols to be
> > evaluated before the compilation so that I don't have to copy and
> > paste the numerical values into the definition?
>
> there are several, but for this case I think With is probably the clearest:
>
> radiusOfEarth=6378100.;
> With[{latitudeDegreeLength=2*Pi*radiusOfEarth/360,deg=N[Degree]},
> GetLongitudeDegreeLength=Compile[{latitude},
>         latitudeDegreeLength*Cos[latitude*deg]]
> ]
>
> hth,
>
> albert

Thank you!


  • Prev by Date: Re: Strange result with FindShortestPath and Tuple
  • Next by Date: Re: Can't make function periodic for t<0
  • Previous by thread: Re: Evaluation control in Compile[]
  • Next by thread: Re: Evaluation control in Compile[]