Given even n, I would like to make a 3^(n/2)-1
by n
2d-numpy array. The first half of every row should iterate over all possible values of -1,0,1 and the second half should be zero. However, the first half should never be all zeros.
This code almost works except it includes the all zero row which I don't want.
n = 4
M = [list(row) +[0]*(n/2) for row in itertools.product([-1,0,1], repeat = n/2)]
print np.array(M)
It gives
[[-1 -1 0 0]
[-1 0 0 0]
[-1 1 0 0]
[ 0 -1 0 0]
[ 0 0 0 0]
[ 0 1 0 0]
[ 1 -1 0 0]
[ 1 0 0 0]
[ 1 1 0 0]]
Is there a less horrible and more time and space-efficient way to do this? n
will ultimately be 30 and I won't be printing it out of course. 3^15 is only 14,348,907 and yet the code uses all the RAM on my 8GB machine when I set n=30
as well as taking a very long time.
How can I make the numpy
array directly without going via itertools
, lists
etc.?