Ruud H.G. van Tol on Tue, 09 Dec 2025 14:18:31 +0100


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

set partitions, or mutually disjount subsets



Mainly for fun, and to learn more about pari,
I'm looking for an efficient way to transform a number W (width)
into a set of K (binary) vectors (each of length K and at least one 1).

Example:

If K=4, then {0,1,2,3} are the possible bit-indexes.
If N=3, then partition K into 3 non-empty subsets:

? {
my(k=4, n=3);
forpart(p=k,
  forperm(k,q,
    (q[1]<q[2] && q[3]<q[4]) || next;
    print([Vec(p),Vec(q)])
  ),,[n,n]
);
}

[[1, 1, 2], [1, 2, 3, 4]]
[[1, 1, 2], [1, 3, 2, 4]]
[[1, 1, 2], [1, 4, 2, 3]]
[[1, 1, 2], [2, 3, 1, 4]]
[[1, 1, 2], [2, 4, 1, 3]]
[[1, 1, 2], [3, 4, 1, 2]]

The condition that skips permutations, depends on p.


The next step for the last element:

[[1, 1, 2], [3, 4, 1, 2]] ->

[[3], [4], [1, 2]] ->

[ [0, 1, 0, 0]  \\ 3
, [1, 0, 0, 0]  \\ 4
, [0, 0, 1, 1]  \\ 1, 2
]


The binary vectors can (for example) be used in multiplications
like primes(4) * [0, 0, 1, 1]~ = 12.
See also https://oeis.org/A390531.

All hints are welcome!

-- Ruud