Services & Resources / Wolfram Forums
MathGroup Archive
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2005

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

Search the Archive

Re: Re: Compile nested loops with depending index variables...

  • To: mathgroup at
  • Subject: [mg61338] Re: Re: Compile nested loops with depending index variables...
  • From: albert <awnl at>
  • Date: Sun, 16 Oct 2005 00:17:54 -0400 (EDT)
  • References: <dikso7$4nr$>
  • Sender: owner-wri-mathgroup at

Hi Christoph,

this is from the HelpBrowser about Compile:

o Nested lists given as input to a compiled function must be full arrays of

the table you construct is not a "full array of numbers", since the length
of each element is different:

In[4]:= Length /@ Table[{i,j},{i,0,n},{j,0,i}]

Out[4]= {1, 2, 3, 4}

and obviously Compile not only doesn't like this sort of nested lists as
input but also can't handle these anyware within the compiled code. This is
not too surprising but not explicitly documented so. So I think if you
really need to compile your code you need to change it in such a way that
it constructs a "full array", e.g. the following compiles fine:

fc = Compile[{n}, Table[If[j <= i, {i, j}, {-1, -1}], {i, 0, n}, {j, 0, n}]]

you could then reconstruct the original nested list by:

DeleteCases[fc[2], {-1, -1}, Infinity]

It depends on your actual problem whether this is more efficient than not
compiling, and usually only trying out will give you the answer. Memory
usage is of course worse than when using the not compiled nested Table...

This is another approach that is probably more memory efficient:

fc = Compile[{{i, _Integer}}, Table[{i, j}, {j, 0, i}]]
ffc = Function[{n}, Table[fc[i], {i, 0, n}]]

note that using Function instead of ffc[n_] is often slightly more efficient
because you avoid complicated pattern matching. The drawback is that you
can't do checks about correct input that easily (like in ffc[n_Integer])...

just try what is the most efficient way for your problem, I wouldn't exclude
that not compiling gives you the fastest code in the end...



  • Prev by Date: Re: surface fitting question
  • Next by Date: Re: Interesting failure of Collect
  • Previous by thread: Re: Compile nested loops with depending index variables...
  • Next by thread: Re: Re: Re: Compile nested loops with depending index variables...