MathGroup Archive 2007

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

Search the Archive

Re: Re: Compile forgets assigned values inside Do unless they are initialised

  • To: mathgroup at smc.vnet.net
  • Subject: [mg82242] Re: [mg82207] Re: Compile forgets assigned values inside Do unless they are initialised
  • From: "Jean-Marc Gulliet" <jeanmarc.gulliet at gmail.com>
  • Date: Tue, 16 Oct 2007 03:24:03 -0400 (EDT)
  • References: <fepss7$gi3$1@smc.vnet.net>

Giving more thoughts, I think you are right and my "thesis" would have
been clearer if I had written, "As a rule of thumb, if one wants to
compile an expression that uses local symbols, then these symbols
should be initialized preferably at the beginning of the Module or
Block construct, or as soon as possible."

Why that? Because, as we are warned by the online documentation,
"Compiled code does not handle numerical precision and local variables
in the same way as ordinary Mathematica code."

Moreover, "The number of times and the order in which objects are
evaluated by Compile may be different from ordinary Mathematica code."

In other words, ordinary evaluation rules do not necessarily and/or
usually apply to compiled code.

In[1]:= Block[{x}, Do[x = 2, {10}]; x]

Out[1]= 2

In[2]:= Compile[{}, Block[{x}, Do[x = 2, {10}]; x]][]

During evaluation of In[2]:= Compile::initvar: The variable x has not \
been initialized or has been initialized to Null. >>

In[3]:= Compile[{}, Block[{x}, x = 2; Do[x = 2, {10}]; x]][]

Out[3]= 2

Regards,
Jean-Marc

On 10/15/07, DrMajorBob <drmajorbob at bigfoot.com> wrote:
> > Yes. If one wants to compile an expression that uses local symbols, then
> > these local symbols must be initialized at the beginning of the Module
> > (or Block) construct. See ref/message/Compile/initvar.
>
> ref/message/Compile/initvar is precious little to go on, and the following
> seems to contradict your thesis:
>
> Compile[{},Block[{x},x=2]]
> %[]
>
> CompiledFunction[{},Block[{x},x=2],-CompiledCode-]
> 2
>
> Precisely how "late" is "too late"?
>
> Bobby
>
> On Mon, 15 Oct 2007 00:18:55 -0500, Jean-Marc Gulliet
> <jeanmarc.gulliet at gmail.com> wrote:
>
> > Andrew Moylan wrote:
> >
> >> In the following example, although var is assigned a value, these are
> >
> > The value is set to late in your example (see below).
> >
> >> "forgotten" and an error occurs when it is returned as the result of the
> >
> > The value has not been "forgotten" since it has never been assigned to
> > the symbol.
> >
> >> compiled function:
> >>
> >> Compile[{},
> >>   Module[
> >>    {var},
> >>    Do[
> >>     var = 0.;,
> >>     {10}
> >>     ];
> >>    var
> >>    ]
> >>   ][]
> >>
> >>>> Compile::initvar: The variable var has not been initialized or has
> >>>> been
> >> initialized to Null.
> >>
> >>
> >> But if var is initialised (to any Real value at all), then all
> >> subsequents
> >> assignments to it are "remembered":
> >>
> >> Compile[{},
> >>   Module[
> >>    {var = 1.},
> >>    Do[
> >>     var = 0.;,
> >>     {10}
> >>     ];
> >>    var
> >>    ]
> >>   ][]
> >>
> >>>> 0.
> >>
> >> Is this the expected behaviour?
> >
> > Yes. If one wants to compile an expression that uses local symbols, then
> > these local symbols must be initialized at the beginning of the Module
> > (or Block) construct. See ref/message/Compile/initvar.
> >
> > Regards,
>
>
>
> --
> DrMajorBob at bigfoot.com
>


-- 
Jean-Marc


  • Prev by Date: Re: ProgressIndicator Questions
  • Next by Date: Re: format mixed integers & floats with text styling (see )
  • Previous by thread: Re: Re: Compile forgets assigned values inside Do unless they are initialised
  • Next by thread: Re: Compile forgets assigned values inside Do unless they are initialised