# Compilation of IntegerPartitions

**URL:** <https://community.wolfram.com/t/compilation-of-integerpartitions/23826>\
**Category:** Questions\
**Tags:** compilation\
**Created:** [April 3, 2024, 10:53am UTC](https://community.wolfram.com/t/compilation-of-integerpartitions/23826 "2024-04-03T10:53:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ulrichutiger](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/ulrichutiger/32/40153_2.png) [@ulrichutiger](https://community.wolfram.com/u/ulrichutiger)\
**Post date:** [April 3, 2024, 10:53am UTC](https://community.wolfram.com/t/compilation-of-integerpartitions/23826/1 "2024-04-03T10:53:57Z")

</div>

I need to compile a function to render it as fast as possible because it is used recurrently. Inside this function I use IntegerPartitions. However, it seems impossible to correctly compile code using IntegerPartitions. For instance, for the following test code

```
F = Compile[{{k, _Integer}, {d, _Integer}, {s, _Integer}}, Block[{ls},
   ls = IntegerPartitions[s, {k}, Range[d]];
   Return[ls]],
  CompilationTarget -> "C"]

```

the answer is “Compiled expression {{3,1,1},{2,2,1}} should be a rank 1 tensor of machine-size integers” when F[3,7,5] is executed, whether the CompilationTarget is C or WVM. However, with ls={{3,1,1},{2,2,1}} it is executed flawlessly. Has anyone an idea what is the reason for this oddity?

---

<div class="post-metadata">

**Author:** ![gianlucagorni](https://avatars.discourse-cdn.com/v4/letter/g/ec9cab/32.png) [@gianlucagorni](https://community.wolfram.com/u/gianlucagorni)\
**Post date:** [April 3, 2024, 5:03pm UTC](https://community.wolfram.com/t/compilation-of-integerpartitions/23826/2 "2024-04-03T17:03:46Z")

</div>

I know nothing about compiling, but the following

```
F = Compile[{{k, _Integer}, {d, _Integer}, {s, _Integer}}, 
  IntegerPartitions[s, {k}, Range[d]]]
With[{s = 8, k = 4, d = 5}, IntegerPartitions[s, {k}, Range[d]]]
With[{s = 8, k = 4, d = 5}, F[k, d, s]]

```

gives the message “Could not complete external evaluation at instruction 9; proceeding with uncompiled evaluation”. Maybe `IntegerPartitions` cannot be compiled.

What is the purpose of `Block` in your code?

---

<div class="post-metadata">

**Author:** ![ulrichutiger](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/ulrichutiger/32/40153_2.png) [@ulrichutiger](https://community.wolfram.com/u/ulrichutiger)\
**Post date:** [April 3, 2024, 6:38pm UTC](https://community.wolfram.com/t/compilation-of-integerpartitions/23826/3 "2024-04-03T18:38:01Z")

</div>

I get the same message. Well, Block has no purpose in this test code. I would use Block together with other instructions that would run faster if I could compile them. The function I want to compile is

```
SP[k_, d_, s_] := Block[{sum, ls, ln, zeros, part, t, numP},
   sum = 0.;
   Do[
    ls = IntegerPartitions[s, {i}, Range[d]]; ln = Length[ls];
    Do[
     part = Join[ConstantArray[0, k - Length[ls[[j]]]], ls[[j]]];
     t = Tally[part];
     numP = k!/Product[t[[l, 2]]!, {l, 1, Length[t]}];
     sum = sum + numP*Apply[Times, Map[y, part]],
     {j, 1, ln}],
    {i, 1, k}];
   Return[sum]];

```

Map[y, part] has still to be replaced with Table[y[[i + 1]], {i, part}], otherwise it cannot be compiled. This code calculates the sum of all y[s1] \* y[s2] \* … \* y[sk] where the si satisfy s1 +…+ sk = s and 0 \<= si \<= d. The y are a binomial distribution. Have you an idea how this can be compiled?
