Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #3396
| From | Leonid Shifrin <lshifr@gmail.com> |
|---|---|
| Newsgroups | comp.soft-sys.math.mathematica |
| Subject | Re: Problem with Compile |
| Date | 2011-06-29 21:51 +0000 |
| Organization | Steven M. Christensen and Associates, Inc and MathTensor, Inc. |
| Message-ID | <iug6ob$9me$1@smc.vnet.net> (permalink) |
| 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@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
>
Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread
Re: Problem with Compile Leonid Shifrin <lshifr@gmail.com> - 2011-06-29 21:51 +0000
csiph-web