MathGroup Archive 2011

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Problem with Compile

  • To: mathgroup at smc.vnet.net
  • Subject: [mg119919] Re: Problem with Compile
  • From: Leonid Shifrin <lshifr at gmail.com>
  • Date: Wed, 29 Jun 2011 05:30:23 -0400 (EDT)
  • References: <201106281155.HAA21492@smc.vnet.net>

Hi Arthur,

It looks like a flaw in the compiler type-inferencer w.r.t. NestWhileList
(which might be explained somewhat
by the arguments below). You can help Compile by explicitly declaring the
type:

In[9]:= g =
  Compile[{{z, _Integer}},   NestWhileList[# + z &, 0, # < 10 &, 1,
    9], {{NestWhileList[__], _Integer, 1}}];

g[1]

Out[10]= {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

Note however that NestWhile and NestWhileList don't get the full (or any?)
advantage of Compile, since they
are not compiled  down to byte-code. Assuming version 8, you can look at the
instructions with

In[5]:= g[[6]]

Out[5]= {{46,   Function[{z}, NestWhileList[#1 + z &, 0, #1 < 10 &, 1, 9]],
2, 0, 0,   2, 1, 0}, {1}}

(use g[[4]] for older versions).

For version 8, you can use CompilePrint to get a better picture:

Needs["CompiledFunctionTools`"]
CompilePrint[g]

You will see the call to MainEvaluate in the printed instructions, which
indicates that the main evaluator, rather
than lower-level code, is used to compute NestWhileList,  which therefore is
not helped much by Compile, at
least in this particular case. I don't know if there are cases when
NestWhileList gets Compiled down to byte-code,
but even if they exist, I don't think they are typical.

Regards,
Leonid



On Tue, Jun 28, 2011 at 3:55 PM, Arthur Evans <24evansa at gmail.com> wrote:

> Dear Mathgroup,
>
> I am having some problems with Compile.
>
> Here the below expression compiles and executes as expected
>
> f = Compile[{{z, _Integer}}, NestList[# + z &, 0, 9]];
> f[1]
>
> Out[89]={0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
>
>
> But this one gives an error
>
> g = Compile[{{z, _Integer}},NestWhileList[# + z &, 0, # < 10 &, 1, 9]]
> g[1]
>
> CompiledFunction::cfse: Compiled expression {0,1,2,3,4,5,6,7,8,9}
> should be a machine-size integer. >>
>
> CompiledFunction::cfex: Could not complete external evaluation at
> instruction 1; proceeding with uncompiled evaluation. >>
>
> Any help is appreciated.
>
> Regards,
>
> Arthur
>


  • Prev by Date: Re: How can I concatenate elements
  • Next by Date: Re: How to find the difference between two lists (opposite of union[]?)
  • Previous by thread: Re: Problem with Compile
  • Next by thread: Remote presentation clicker for mathematica slideshows.