Re: Nested iterators in Compile
- To: mathgroup at smc.vnet.net
- Subject: [mg55019] Re: Nested iterators in Compile
- From: "Carl K. Woll" <carlw at u.washington.edu>
- Date: Thu, 10 Mar 2005 05:24:18 -0500 (EST)
- Organization: University of Washington
- References: <d0mo9g$75h$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Maxim,
A workaround that shouldn't affect performance is to write out the nested Do
loops explicitly:
Compile[{n},
Module[{s = 0},
Do[
Do[s += j, {j, i}],
{i, n}
];
s
]][10]
The above returned 220 without problems.
Carl Woll
"Maxim" <ab_def at prontomail.com> wrote in message
news:d0mo9g$75h$1 at smc.vnet.net...
> The fact that iterators work differently when used in Compile seems to
> lead to a serious limitation:
>
> In[1]:=
> Compile[{n},
> Module[{s = 0},
> Do[s += j, {i, n}, {j, i}];
> s
> ]][10]
>
> CompiledFunction::cfse: Compiled expression i should be a machine-size
> real number.
>
> CompiledFunction::cfex: External evaluation error at instruction 4;
> proceeding with uncompiled evaluation.
>
> Out[1]=
> 220
>
> The iterator {j, i} is evaluated before any value is assigned to i, and
> the evaluation of the compiled code fails (and if we add the
> initialization i=0 before Do, we'll only get an empty loop as the result,
> unlike the uncompiled version). So we have to resort to some workarounds
> such as changing the inner iterator to {j, n} and adding If[j>i,
> Continue[]] to the loop body, which of course decreases the performance.
>
> Maxim Rytin
> m.r at inbox.ru
>