Fwd: Compile forgets assigned values inside Do unless they are initialised
- To: mathgroup at smc.vnet.net
- Subject: [mg82224] Fwd: Compile forgets assigned values inside Do unless they are initialised
- From: "Jean-Marc Gulliet" <jeanmarc.gulliet at gmail.com>
- Date: Mon, 15 Oct 2007 01:27:42 -0400 (EDT)
- References: <fepss7$gi3$1@smc.vnet.net> <4711077A.2050607@gmail.com>
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, -- Jean-Marc