0
$\begingroup$

I want to count and generate the number of non distinct integer partitions into k. I know that IntegerPartitions[n,{k}] returns the partitions of integer n into k.

E.g. IntegerPartitions[4, {2}] returns {{3, 1}, {2, 2}}. I want to count the frequency at which these occur e.g. 4 times for {3,1} and twice for {2,2}.

Edit

By this, I mean I can split 4 into {3,1} in 4 ways i.e. :-:---,-:-:--,--:-:- and ---:-: where the colons denote partitioning of the hyphens that comprise the number (here, 4). I count 4 ways with 'periodic boundary conditions'.

For the partitioning of 4 into {2,2} with the above notation, I find :--:-- and -:--:-, so 2 ways.

For IntegerPartitions[5, {2}], {4,1} and {2,3} occur 5 times (with periodic boundary conditions).

For the partitioning of 5 into {2,3} with the above notation, I find :--:---, -:--:--, --:--:-, ---:--: and -:---:-, so 5 ways.

Thank you!

Edit 2

As Domen says, and my examples attest, I assume pbc such that e.g. -:---:- is an example of an integer partition of 5 into {2,3}.

The case n=5 is still relatively simple, in that looking for integer partitions into k=2 returns only 2 distinct partitions. This grows with n, e.g. n=6 partitions into the 3 distinct partitions {1,5}, {2,4} and {3,3}. I want to compute the frequency of these/how many non distinct partitions there are.

I am working on a function but it is not streamlined.

Ideally, this would be called something like 'countIntegerPartitions', and take the integer 'n', partitions into 'k' as input. This is for a physics application (with instantaneous Gaussian distributed Hamiltonians H scaled such that H^2 on average gives the identity) in which I am only concerned with integer partitions into k=2. This function would return the integer partitions and the frequency at which these occur subject to pbc e.g. countIntegerPartitions[4,2] would return something like {{{3,1},4},{{2,2},2}}.

So far, I have created a repeating list of integers up to 2n and used the Partition function to partition this into partitions of length z where z is the largest integer in the partition pair. The partition function allows one to cycle through the list starting positions, and one can then count the number of distinct elements in the output list. For z = n/2, I need to divide by 2 to avoid double counting the partitions i.e. for n=6, {1,2,3} is equal to {4,5,6}. This is quite lengthly with for/if statements...

$\endgroup$
11
  • $\begingroup$ 4 times for {3,1} and twice for {2,2} : Could you elaborate upon this? How do you count 4 and twice? $\endgroup$
    – Syed
    Nov 23, 2022 at 14:51
  • $\begingroup$ I have edited my question to elaborate on this! $\endgroup$
    – 00123456
    Nov 23, 2022 at 15:01
  • $\begingroup$ Your question doesn't seem very well defined. Can you instead provide test cases? Like explicitly, what should the answer be for partitioning 4 into 2 and 5 into 2 and maybe a couple other examples. $\endgroup$
    – lericr
    Nov 23, 2022 at 15:53
  • $\begingroup$ For example, one of your examples for 4 into {2,2} is -:--:-, but that sure looks like 3 partitions to me. $\endgroup$
    – lericr
    Nov 23, 2022 at 15:54
  • 1
    $\begingroup$ Alternatively, if you're doing some sort of cyclic thing, then :--:-- and -:--:- are the same, so only one way to do it. $\endgroup$
    – lericr
    Nov 23, 2022 at 15:56

1 Answer 1

0
$\begingroup$

Okay, I don't know how performant this will be, but it sounds simpler than what you describe as lengthy for/if statements:

PartitionPatterns[parts : {__Integer}] :=
  DeleteDuplicates[
    DeleteCases[
      NestList[
        RotateRight,
        Flatten[Riffle[parts, Null, {1, -2, 2}] /. num_Integer :> ConstantArray[1, num]],
        Total@parts],
      {__, Null}]]

This gives examples like these:

PartitionPatterns[{3, 2}]

{{Null, 1, 1, 1, Null, 1, 1}, {1, Null, 1, 1, 1, Null, 1}, {Null, 1, 1, Null, 1, 1, 1}, {1, Null, 1, 1, Null, 1, 1}, {1, 1, Null, 1, 1, Null, 1}}

PartitionPatterns[{2, 2}]

{{Null, 1, 1, Null, 1, 1}, {1, Null, 1, 1, Null, 1}}

PartitionPatterns[{3, 1}]

{{Null, 1, 1, 1, Null, 1}, {Null, 1, Null, 1, 1, 1}, {1, Null, 1, Null, 1, 1}, {1, 1, Null, 1, Null, 1}}

You said that in addition to counting you wanted to generate these partitions, so that's why I've created a representation (you can just count them with Length). I don't know if you want this type of representation, but I'm just following your examples with : and - in the comments.

$\endgroup$
2
  • $\begingroup$ This works with Length and avoids the ambiguity z = n/2! Thanks very much for your help! The method I came up with is this: Co[r_] := (partition = IntegerPartitions[r, {2}]; coef = Table[0, {i, Length[partition]}]; tab = Table[i, {i, 1, r}]; table = Flatten[Table[Flatten[tab], 2]]; For[j = 1, j < Length[partition] + 1, j++, If[Max[partition[[j]]] == r/2, coef[[j]] = CountDistinct[Partition[table, Max[partition[[j]]], {1}]]/2, coef[[j]] = CountDistinct[Partition[table, Max[partition[[j]]], {1}]]]]; coef) and is comparable when timed. $\endgroup$
    – 00123456
    Nov 24, 2022 at 9:58
  • $\begingroup$ I should note that I had previously misread your question to be only concerned with two partitions. I don't think my implementation will work for more partitions. It could be modified to handle more partitions by permuting the partition spec. $\endgroup$
    – lericr
    Nov 24, 2022 at 13:40

Your Answer

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

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