Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

This question already has an answer here:

First question,How to Compile this?

gather[data_]:=Gather[data, Mod[#1, 3] == Mod[#2, 3] &]

I dont know why this is wrong.

cf = Compile[{{data, _Integer, 1}}, Gather[data, Mod[#1, 3] == Mod[#2, 3] &]]
cf[Range[9]]
cf[Range[10]]

Second question,How to Compile this?

gather[data_] := Gather[data, Count[#1, 0] == Count[#2, 0] &]
gather[RandomInteger[{0, 5}, {10, 5}]]
share|improve this question

marked as duplicate by Kuba, m_goldberg, Michael E2, Yves Klett, belisarius Mar 20 at 18:48

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.

2  
I don't believe that Gather is compilable. You can see a list of compilable functions in the answers to this question. –  Mark McClure Mar 20 at 13:59
1  
Not every function can be compiled. Gather cannot be compiled. The reason why Gather is not compilable is that it returns a ragged array and compiled code doesn't support ragged arrays. –  Szabolcs Mar 20 at 13:59
    
sorry...My memory is wrong...but if i change gather to tally ,how to compile second situation? –  Chenminqi Mar 20 at 14:17
1  
@Chenminqi In the second situation is every sublist of data of the same length? This is necessary to make it compilable. Vectorization may be better though. Tally@Total[1-Unitize[data],{2}] should do it if I didn't make a mistake (untested). –  Szabolcs Mar 20 at 14:27

1 Answer 1

Not every function can be compiled by Compile, and those functions which are supported are often supported only partially, for numerical arguments only.

Gather cannot be compiled. The reason for this is that Gather will usually return a ragged array and compiled functions can only work with full (i.e. rectangular), homogeneous numerical arrays.

share|improve this answer

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