Re: Re: Nested iterators in Compile

*To*: mathgroup at smc.vnet.net*Subject*: [mg55058] Re: [mg55053] Re: [mg55010] Nested iterators in Compile*From*: DrBob <drbob at bigfoot.com>*Date*: Fri, 11 Mar 2005 04:20:33 -0500 (EST)*References*: <200503101025.FAA19427@smc.vnet.net>*Reply-to*: drbob at bigfoot.com*Sender*: owner-wri-mathgroup at wolfram.com

Hartmut, Here's your own solution with a different result (due to 5.1.1, perhaps?): f2=Compile[{{n,_Integer}}, Module[{s=0},Do[Do[s+=j,{j,i}],{i,n}];s]]; f2[[-2]] Function[{n},Module[{s=0},Do[Do[s+=j,{j,i}],{i,n}];s]] FreeQ[f2[[-2]],Function] False If only I had some more of that "purposive pragmatism". Bobby On Thu, 10 Mar 2005 05:25:20 -0500 (EST), Wolf, Hartmut <Hartmut.Wolf at t-systems.com> wrote: > >> -----Original Message----- >> From: Maxim [mailto:ab_def at prontomail.com] To: mathgroup at smc.vnet.net >> Sent: Wednesday, March 09, 2005 12:35 PM >> Subject: [mg55058] [mg55053] [mg55010] Nested iterators in Compile >> >> 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 >> >> > > > Maxim, > > I had told you this before: > > using compiled functions needs some sort of purposive pragmatism. > Also mistrust any compiled function f with > > In[3]:= FreeQ[f[[-2]], Function] > Out[3]= False > > > This case can be solved easily: >In[4]:= f2 = Compile[{{n, _Integer}}, > Module[{s = 0}, Do[Do[s += j, {j, i}], {i, n}]; > s]]; > > In[5]:= f2[10] > Out[5]= 220 > > In[6]:= FreeQ[f2[[-2]], Function] > Out[6]= True > > > > -- > Hartmut Wolf > > > > -- DrBob at bigfoot.com

**References**:**Re: Nested iterators in Compile***From:*"Wolf, Hartmut" <Hartmut.Wolf@t-systems.com>