Re: Re: Compile forgets assigned values inside Do unless they are initialised
- To: mathgroup at smc.vnet.net
- Subject: [mg82238] Re: [mg82207] Re: Compile forgets assigned values inside Do unless they are initialised
- From: DrMajorBob <drmajorbob at bigfoot.com>
- Date: Tue, 16 Oct 2007 03:21:59 -0400 (EDT)
- References: <fepss7$gi3$1@smc.vnet.net> <33239569.1192447162247.JavaMail.root@m35>
- Reply-to: drmajorbob at bigfoot.com
> 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