This question already has an answer here:

I wrote a function with Compile that produces a large array of data and later does some computation on it. Now, I want to return from the compiled function not only the final processed data, but also the raw data from which it originates. I know that Compile cannot deal with ragged arrays, that is I can not simply state {x,y} as the final line of the compiled function. What are the best strategies to resolve this problem if I don't won't to break up the one compiled function into two functions?


marked as duplicate by Szabolcs, MarcoB, m_goldberg, Henrik Schumacher, J. M. will be back soon Feb 15 at 4:56

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • $\begingroup$ I edited this for clarity (as I found it a bit confusing). Please check that I did not break your intended meaning. $\endgroup$ – Szabolcs Feb 14 at 11:33
  • $\begingroup$ @Szabolcs: Sorry, yes I was trying to Export it first on-the-fly, but the function is not supported within Compiled. Now I would be glad to find a way of returning it as the output of Compile and then process it further from there within the MMA kernel. $\endgroup$ – Display Name Feb 14 at 11:34
  • $\begingroup$ Can you give a representative example? Something trivial that returns that same shape of data that you actually have. Maybe there's a simple way to encode all pieces into a single tensor. You can actually return them as {x,y}, and I believe that the system will break from the compiled evaluation only at the very last operation. It does issue an annoying message though. $\endgroup$ – Szabolcs Feb 14 at 11:40
  • 1
    $\begingroup$ Not necessarily. Basically, it is about two copy operations. The Flattening and ArrayReshape ing is really almost for free since packed arrays are stored internally as a flat vector anyways. $\endgroup$ – Henrik Schumacher Feb 14 at 11:48
  • 1
    $\begingroup$ @Szabolcs My understand is that "proceeding with uncompiled evaluation" means that the evaluation of the CompiledFunction cf is stopped and restarted completely from the beginning by executing cf[[7]] as fallback. So that would make things even more expensive. $\endgroup$ – Henrik Schumacher Feb 14 at 11:55

Not an answer but too long for a comment.

This shows that in case of CompiledFunction::cflist, a CompiledFunction cf will fall back to execution of cf[[7]]:

cf = Compile[{}, {{1, 2}, {3}}];
Quiet[cf[[7]] = 
  Function["args", Print["Meep!"]; "body"] /. 
  {"args" -> cf[[7, 1]], "body" -> cf[[7, 2]]};

enter image description here


  • $\begingroup$ @Szabolcs See here for an example. $\endgroup$ – Henrik Schumacher Feb 14 at 12:09

Not the answer you're looking for? Browse other questions tagged or ask your own question.