<div dir="ltr">On Wed, Sep 25, 2013 at 1:12 PM, Edmondo Porcu <<a href="mailto:edmondo.porcu@gmail.com">edmondo.porcu@gmail.com</a>> wrote:<br>><br>> That's what I was looking for, except that I want to be sure to generate all the possible combinations, and to have no repeated values.<br>
<br>Okay, then you need to find all of the integer partitions of 10 with `ncols` elements (padding with 0s for those partitions with fewer than `ncols` elements), finding the permutations of each padded partition, then eliminating duplicates.<br>
<br><br>import itertools<br>import numpy as np<br><br><br>def gen_partitions(n):<div> """ Generate integer partitions for `n`.</div><div><br></div><div> <a href="http://homepages.ed.ac.uk/jkellehe/partitions.php">http://homepages.ed.ac.uk/jkellehe/partitions.php</a></div>
<div> """<br> a = [0 for i in range(n + 1)]<br> k = 1<br> y = n - 1<br> while k != 0:<br> x = a[k - 1] + 1<br> k -= 1<br> while 2*x <= y:<br> a[k] = x<br> y -= x<br>
k += 1<br> l = k + 1<br> while x <= y:<br> a[k] = x<br> a[l] = y<br> yield a[:k + 2]<br> x += 1<br> y -= 1<br> a[k] = x + y<br> y = x + y - 1<br>
yield a[:k + 1]<br><br><br><div>def gen_permuted_partitions(ncols, n=10):</div><div> for partition in gen_partitions(n):</div><div><div> partition = list(partition)</div></div><div><span style="font-size:10pt"> if len(partition) > ncols:</span><br>
</div><div> continue</div><div><span style="font-size:10pt"> partition.extend([0] * (ncols - len(partition)))</span><br></div><div> for x in itertools.permutations(partition):</div><div> yield x</div>
<div><br></div><div><br></div><div>def compute_matrix(ncols):</div><div> return np.array(sorted(set(gen_permuted_partitions(ncols)))) / 10.0</div><br>--<br>Robert Kern</div></div>