Re: Partition Function
- To: mathgroup at smc.vnet.net
- Subject: [mg57634] Re: Partition Function
- From: "Ray Koopman" <koopman at sfu.ca>
- Date: Thu, 2 Jun 2005 05:17:44 -0400 (EDT)
- References: <d7k3qf$ooq$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Shug Boabby wrote: > is there any way to get mathematica to return the actual partitions > of an integer? the only functions related to this, (e.g. > http://mathworld.wolfram.com/PartitionFunctionP.html) only return > the *number* of partitions, not the actual partititons (including > permutations) themselves. > > e.g. for 4, i'd like a function to return > {{1,1,1,1}, {2,1,1}, {1,2,1}, {1,1,2}, {3,1}, {2,2}, {1,3}, {4}} > > with the ordering of the partitions not being important Here is the code I use to get ordered (i.e., unpermuted) partitions: In[1]:= P[0,m_] := {Table[0,{m}]}; P[n_,m_]/;n<m := With[{z=Table[0,{m-n}]},Join[#,z]&/@P[n,n]]; P[n_,1] := {{n}}; P[n_,2] := Table[{n-i,i},{i,0,n/2}]; P[n_,m_] := ToExpression[ "Block[" <> ToString[Table[SequenceForm["n",k],{k,m-2}]] <> ",Flatten[Table[" <> ToString[Table[If[k==m,SequenceForm["n",m-2,"-i",m-1], SequenceForm["i",k]],{k,m,1,-1}]] <> "," <> StringTake[ToString[Table[Which[ k==1,StringForm["{i1,0,``/``}",n,m], k==2,StringForm["{i2,i1,(n1=``-i1)/``}",n,m-1], True,StringForm["{i`3`,i`2`,(n`2`=n`1`-i`2`)/`4`}", k-2,k-1,k,m+1-k]], {k,m-1}]], {2,-2}] <> "]," <> ToString[m-2] <> "]]" ] In[6]:= P[4,4] Out[6]= {{4,0,0,0},{3,1,0,0},{2,2,0,0},{2,1,1,0},{1,1,1,1}} This will get what you want: In[7]:= P[n_] := Flatten[Map[Permutations,Select[#,Positive]&/@P[n,n]],1]; In[8]:= P[4] Out[8]= {{4},{3,1},{1,3},{2,2},{2,1,1},{1,2,1},{1,1,2},{1,1,1,1}}