I defined a compiled function

cf = Compile[{{x, _Integer}, {n, _Integer}},
   z = (n^x); 
   Binomial[n, #]* StirlingS2[x, #] *(#!)/z & /@ Range[x]];

and I want to run this for

n = 300000.0;
x = 186200;
Timing[cf[x, n]]

But I have below warnings/errors:

Join::heads: Heads CurrentValue and List at positions 1 and 2 are expected to be the same.
Join::heads: Heads CurrentValue and List at positions 1 and 2 are expected to be the same.
Join::heads: Heads CurrentValue and List at positions 1 and 2 are expected to be the same.
general:stop : Futher output of Join::heads will be suppressed during this calculation.
CompileFunction::cfse : Compiled expression
0000000000000000000000000000000000000000000000000 should be a \
machine-size integer.
CompiledFunction::cfex: Could not complete external evaluation at instruction 2; proceeding with uncompiled evaluation

Can you help me understand what I am doing wrong? Because for this values of n and x this compile function should be evaluated fast, but it is working very slow.

  • $\begingroup$ No idea, but a) since z isn't localised with Module you'll end up calling the main evaluator anyway, and even if you do localize it, b) neither Binomial nor StirlingS2 are compilable. You can see a list of compilable functions with Compile`CompilerFunctions $\endgroup$
    – acl
    Commented Jul 14, 2014 at 21:57
  • $\begingroup$ As x and n are integers, one might get this working. $\endgroup$
    – Karsten7
    Commented Jul 14, 2014 at 22:48
  • 1
    $\begingroup$ Can you give more details on these parameter and explain a litte bit, what you are trying do? You can use ´FunctionExpand[Binomial[n, m]]´ to get the function definition of Binomial in terms of the Gamma function, which is compilable. For StirlingS2 you could use the definitions from here or here to get something compilable. $\endgroup$
    – Karsten7
    Commented Jul 14, 2014 at 22:57
  • $\begingroup$ Could you also add an expacted output, because the function, as it is defined in you question, would have an output that is a list of 186200 numbers. A lot of these smaller than $MinMachineNumber and therefore not suitable within Compile $\endgroup$
    – Karsten7
    Commented Jul 14, 2014 at 23:18

1 Answer 1


You have several issues here. My oldest Mathematica here is version 8, but when I look at your compiled code:

cf = Compile[{{x, _Integer}, {n, _Integer}}, z = (n^x);
   Binomial[n, #]*StirlingS2[x, #]*(#!)/z & /@ Range[x]];

<< CompiledFunctionTools`

I see that there are several callbacks from the compiled code to the main-kernel, because e.g. Binomial, StirlingS2 or Factorial cannot be compiled. I would make no sense to compile your function.

You biggest issue seems to be that you think compiled functions can work with arbitrary large integers like Mathematica can. They cannot! The numbers in a compiled call are the usual machine numbers like C style int or float. This is what the message

Compiled expression .... should be a "machine-size integer"

tries to tell you. Here, I'm not sure whether this integer is really too large to fit, because your code doesn't localize the variable z. With a proper definition

cf = Compile[{{x, _Integer}, {n, _Integer}}, Module[{z = (n^x)},
   Binomial[n, #]*StirlingS2[x, #]*(#!)/z & /@ Range[x]]

n = 300000.0;
x = 186200;
Timing[cf[x, n];]

I only get the message

Numerical error encountered at instruction 1; proceeding with uncompiled evaluation. >>

which is comes from the calculation of n^x, because this number is too large with the values you have supplied. You should use normal Mathematica code to solve your problem.

  • $\begingroup$ Ok, thanks for your help! $\endgroup$
    – SugerBoy
    Commented Jul 14, 2014 at 22:03
  • $\begingroup$ The reason for that particular error could also be that n is of type Real, but was defined as Integer in the cf. $\endgroup$
    – Karsten7
    Commented Jul 14, 2014 at 23:01
  • $\begingroup$ @Karsten7. No, usually numbers like 1.0 are just converted to integer by casting: Compile[{{x, _Integer}}, x][300.0] On the other hand, when the conversion is not possible a message is thrown: Compile[{{x, _Integer}}, x][300.1] The message in the example is thrown because z is not localized by Module. After that the call cf[3, 300.] runs without message. $\endgroup$
    – halirutan
    Commented Jul 14, 2014 at 23:08
  • $\begingroup$ @Karsten7. I have added this information to the answer, because my first version did not describe the issue properly. $\endgroup$
    – halirutan
    Commented Jul 14, 2014 at 23:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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