Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #2789

Evaluation control in Compile[]

From Ben <benp84@gmail.com>
Newsgroups comp.soft-sys.math.mathematica
Subject Evaluation control in Compile[]
Date 2011-05-30 10:33 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <irvrqm$8hh$1@smc.vnet.net> (permalink)

Show all headers | View raw


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[latitude
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?

Thanks.

Back to comp.soft-sys.math.mathematica | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Evaluation control in Compile[] Ben <benp84@gmail.com> - 2011-05-30 10:33 +0000
  Re: Evaluation control in Compile[] Albert Retey <awnl@gmx-topmail.de> - 2011-05-30 11:48 +0000
    Re: Evaluation control in Compile[] Ben <benp84@gmail.com> - 2011-05-31 11:46 +0000
  Re: Evaluation control in Compile[] Szabolcs Horvát <szhorvat@gmail.com> - 2011-05-31 11:49 +0000
  Re: Evaluation control in Compile[] Szabolcs Horvát <szhorvat@gmail.com> - 2011-06-01 08:30 +0000

csiph-web