Re: Compile can't handle indexed variables inside loops

• To: mathgroup at smc.vnet.net
• Subject: [mg114278] Re: Compile can't handle indexed variables inside loops
• From: Oliver Ruebenkoenig <ruebenko at wolfram.com>
• Date: Tue, 30 Nov 2010 04:01:11 -0500 (EST)

```On Mon, 29 Nov 2010, Frank K wrote:

> Indexed variables are renamed.  This is ok if they aren't inside a
> loop
>
> In[1]:= fc1 = Compile[{x[1],x[2]},x[1]+x[2]]
>
> Out[1]= CompiledFunction[{x\$651,x\$652},x\$651+x\$652,-CompiledCode-]
>
> In[2]:= fc1[1,2]
>
> Out[2]= 3.
>
> Indexed variables are not renamed inside a loop, so the function
> fails.
>
> In[3]:= fc2 = Compile[{x[1],x[2]},Sum[x[i],{i,2}]]
>
> Out[3]= CompiledFunction[{x\$655,x\$656},\!\(
> \*UnderoverscriptBox[\(\[Sum]\), \(i\), \(2\)]\(x[i]\)\),-
> CompiledCode-]
>
> In[4]:= fc2[1,2]
> During evaluation of In[4]:= CompiledFunction::cfex: Could not
> complete external evaluation at instruction 5;
> proceeding with uncompiled evaluation. >>
>
> Out[4]= x[1]+x[2]
>
>
>

Hi Frank,

here is another approach that might help:

res = Quiet[ Sum[x[[i]], {i, 2}]];
fc3 = With[{code = res}, Compile[{{x, _Real, 1}}, code]]

create the expression first (with part) and then inject it into compile.

fc3[{1., 2.}]

Hth,
Oliver

```

• Prev by Date: Re: Problems with Mathematica 8.0 Solve
• Next by Date: Re: Workbench and Mathematica 8
• Previous by thread: Compile can't handle indexed variables inside loops
• Next by thread: Re: Compile can't handle indexed variables inside loops