MathGroup Archive 2010

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

Search the Archive

Re: multiple outputs from compiled function

  • To: mathgroup at
  • Subject: [mg114315] Re: multiple outputs from compiled function
  • From: Oliver Ruebenkoenig <ruebenko at>
  • Date: Tue, 30 Nov 2010 06:23:49 -0500 (EST)

On Tue, 30 Nov 2010, Eric Michielssen wrote:

> I have a compiled function that internally generates 10 large lists (list1
> though list10), all of different ranks and sizes. Problem is that a compiled
> function can only return one packed array, so returning them to the main
> code is an issue. Of course, I could combine/pack all these lists into one
> list by Flatten[list1,...,list10], return the new structure, and then unpack
> it in the main code. Is there a less cumbersome way of doing this?
> Eric Michielssen

Here are some thoughts:

It is correct that compile can only work with rectangular tensors. See the 

f = Compile[{{t1, _Real, 2}, {t2, _Real, 2}}, Join[t1, t2]]

f[RandomReal[{0, 1}, {2, 2}], RandomReal[{0, 1}, {2, 1}]]

compared to:

f[RandomReal[{0, 1}, {2, 2}], RandomReal[{0, 1}, {2, 2}]]

But if the result of the base CompiledFunction is a list, and the list 
results come out different lengths, it returns a list of PackedArrays,

cf = Compile[{{x, _Real}, {n, _Integer}},
    Module[{y = 1.}, Table[y = (y + x/y)/2, {n}]],
    RuntimeAttributes -> Listable];

res = cf[{2., 3.}, {3, 2}]

{Developer`PackedArrayQ[res], Map[Developer`PackedArrayQ, res]}

resp = cf[{2., 3.}, 3]


What do exactly in your case depends on the code.

Hope this helps,

  • Prev by Date: Re: Compile can't handle indexed variables inside loops
  • Next by Date: Re: Links from slide shows/PDF documents to Mathematica Manipulate demos?
  • Previous by thread: multiple outputs from compiled function
  • Next by thread: How to restore the region deleted by accidental paste?