Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
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 – acl Jul 14 '14 at 21:57
As x and n are integers, one might get this working. – Karsten 7. Jul 14 '14 at 22:48
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. – Karsten 7. Jul 14 '14 at 22:57
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 – Karsten 7. Jul 14 '14 at 23:18

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.

share|improve this answer
Ok, thanks for your help! – SugerBoy Jul 14 '14 at 22:03
The reason for that particular error could also be that n is of type Real, but was defined as Integer in the cf. – Karsten 7. Jul 14 '14 at 23:01
@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. – halirutan Jul 14 '14 at 23:08
@Karsten7. I have added this information to the answer, because my first version did not describe the issue properly. – halirutan Jul 14 '14 at 23:33

Your Answer


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

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