Ruud H.G. van Tol on Thu, 11 Dec 2025 00:53:51 +0100


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: set partitions, or mutually disjoint subsets



On 2025-12-09 14:18, Ruud H.G. van Tol wrote:
Mainly for fun, and to learn more about pari,
I'm looking for an efficient way to transform a number N (width)
into a set of K (binary) vectors (each of length K and at least one 1).

One way to do it:

? {
  my( n= 6, k= 3, p= partitions(n,,[k,k]) );
  foreach( p,q
  , forperm( q,r
    , print("=", Vec(r), "=");
      my(b= 0);
      for( i=1,#r
      , my(d= vector(n));
        for( j=1,r[i]
        , d[b++]= 1
        );
        print(d)
      )
    );
    print
  );
}

=[1, 1, 4]=
[1, 0, 0, 0, 0, 0]
[0, 1, 0, 0, 0, 0]
[0, 0, 1, 1, 1, 1]
=[1, 4, 1]=
[1, 0, 0, 0, 0, 0]
[0, 1, 1, 1, 1, 0]
[0, 0, 0, 0, 0, 1]
=[4, 1, 1]=
[1, 1, 1, 1, 0, 0]
[0, 0, 0, 0, 1, 0]
[0, 0, 0, 0, 0, 1]

=[1, 2, 3]=
[1, 0, 0, 0, 0, 0]
[0, 1, 1, 0, 0, 0]
[0, 0, 0, 1, 1, 1]
=[1, 3, 2]=
[1, 0, 0, 0, 0, 0]
[0, 1, 1, 1, 0, 0]
[0, 0, 0, 0, 1, 1]
=[2, 1, 3]=
[1, 1, 0, 0, 0, 0]
[0, 0, 1, 0, 0, 0]
[0, 0, 0, 1, 1, 1]
=[2, 3, 1]=
[1, 1, 0, 0, 0, 0]
[0, 0, 1, 1, 1, 0]
[0, 0, 0, 0, 0, 1]
=[3, 1, 2]=
[1, 1, 1, 0, 0, 0]
[0, 0, 0, 1, 0, 0]
[0, 0, 0, 0, 1, 1]
=[3, 2, 1]=
[1, 1, 1, 0, 0, 0]
[0, 0, 0, 1, 1, 0]
[0, 0, 0, 0, 0, 1]

=[2, 2, 2]=
[1, 1, 0, 0, 0, 0]
[0, 0, 1, 1, 0, 0]
[0, 0, 0, 0, 1, 1]

-- Ruud