Sign up ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.
g = RandomGraph[{1000, 6000}];
NumberOfVertices = VertexCount @ g;
A = AdjacencyMatrix @ g;
ele = Flatten[A];
cnt = Length /@ A;
dsp = FoldList[Plus, 1, cnt];
fun = Compile[{{ele, _Integer, 1}, {cnt, _Integer, 1}, {dsp, _Integer, 
         1}, {n, _Integer}, {ccc, _Integer, 1}}, 
         Fold[Function[{c, v}, 
              com = Complement[Range[n], 
                c[[Take[ele, {dsp[[v]], dsp[[v]] + cnt[[v]] - 1}]]]]}, 
             RandomChoice[Append[com, c[[v]]]]], v]], cc, 
          RandomSample[Range[NumberOfVertices], NumberOfVertices]]], ccc, 
        Max[#] > n &]];
 fun[ele, cnt, dsp, 100, Range[NumberOfVertices]]

Its giving me the following warnings:

Compile::extscalar: Function[{cc},Fold[Function[{c,v},ReplacePart[c,Module[{i,com=Complement[<<2>>]},RandomChoice[Append[com,Part[<<2>>]]]],v]],cc,RandomSample[Range[NumberOfVertices],NumberOfVertices]]] cannot be compiled and will be evaluated externally. The result is assumed to be of type Real. >>
Compile::extscalar: Max[#1]>n& cannot be compiled and will be evaluated externally. The result is assumed to be of type Real. >>
share|improve this question
Why is NumberOfVertices not defined in your code? – J. M. Nov 11 '12 at 3:10
@J.M. sorry, just added it as a global but it could be any positive integer. – Tom Wellington Nov 11 '12 at 6:42
I'm not sure that this question is answerable as such, because there's really no specific reason; Compile is just not very effective for functional-style code, especially with nested scoping constructs. Essentially, the style that's most easily understood by the compiler is the polar opposite of what's needed for top-level code. You'll probably have more success if you rewrite in a more procedural style. – Oleksandr R. Nov 11 '12 at 10:03
One problem is that ele is a sparse array and is not among the supported types for compile. NestWhile isn't amongst the results for CompilerFunctions[] and I'm not certain but ccc might also be causing problems, perhaps to the function with cc as it's parameter. – image_doctor Nov 11 '12 at 11:26
@image_doctor yes, exactly. There are multiple issues here, each of them sufficient to prevent compilation. Fixing them all will require modifying the existing code to such an extent that I thought it best just to suggest a rewrite, since it's obvious just by looking that the compiler is going to choke on something like this in its present form. – Oleksandr R. Nov 11 '12 at 12:18

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.