pythonpython-3.xvectorpowersetbasis

Converting an array containing the power set of a positive integer d to an array with standard elementary vectors in d-dimensional space


Example of what I am trying to do: convert the power set of d = 2 using range(2):

[(), (0,), (1,), (0, 1)]

or using range(1, 3, 1):

[(), (1,), (2,), (1, 2)]

into the following array of arrays:

[[0, 0], [1, 0], [0, 1], [1, 1]]

More generally, I am trying to convert an indexSet (the power set of a positive number d) to be an array containing standard elementary vectors, where the element (i,j,k) corresponds to a vector (an array) in a d-dimensional space with a one in entries i, j, and k, and the empty set corresponds to the zero vector in d-space (the d-tuple array with only zeros in its entries).

I am having a hard time doing this. Any suggestions will be greatly appreciated.

from itertools import chain, combinations
d=5

indexSet = [[] for _ in range(1)]
NumbersUpToD = range(1,int(d)+1,1)
vect = [[0 for i in range(d)] for x in range(d)]
for i in range(d):
    vect[i][i] = vect[i][i] + 1

set_of_d = set(NumbersUpToD)
numbers = [i for i in range(1)]

for z in chain.from_iterable(combinations(set_of_d,r) for r in range(len(set_of_d)+1)):
    indexSet[numbers[0]].append(z)
indexSet = sum(indexSet, [])
for j in range(2**d):
    str(list(indexSet[j]))
print(indexSet)

powerSetVectors = [[0 for x in range(d)] for i in range(2**d)]
print(powerSetVectors)

for i in range(2**d):
    for j in range(d):
        if j in indexSet[j]:
            indexSet[j]=vect[j]
print(indexSet)

Note that the two sets of output you should see are the following:

[(), (0,), (1,), (2,), (3,), (4,), (0, 1), (0, 2), (0, 3), (0, 4), (1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4), (0, 1, 2), (0, 1, 3), (0, 1, 4), (0, 2, 3), (0, 2, 4), (0, 3, 4), (1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4), (0, 1, 2, 3), (0, 1, 2, 4), (0, 1, 3, 4), (0, 2, 3, 4), (1, 2, 3, 4), (0, 1, 2, 3, 4)]

[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]

Solution

  • Good friends of mine helped me in answering this question.

    Here is one alternative solution:

    def pwr(d,r):
        out = []
        for n in range(2**d):
            p = [r*int(i) for i in list(bin(n)[2:])]
            while len(p) < d:
                p = [0] + p
            out.append(p)
        return out
        print(out)
    
    print(pwr(2,1)) 
    

    with the output being:

    [[0, 0], [0, 1], [1, 0], [1, 1]]