Ask Your Question

Revision history [back]

Hi,

I might have misunderstood, but the number of such arrays is $3^{776}$ which is of the size of $10^{140}$. There is no chance you can iterate through such a big set with a computer.

With more reasonable size this is doable as follows. There is a very nice standard Python library called itertools that allows to do that without nested loop. Let me first define a function to create a tensor from a list of values

sage: def one_line_array_to_tensor(l,m1,m2,m3):
....:    return [[[l[i + m3*(j + m2 * k) for i in range(m3)] for j in range(m2)] for k in range(m1)]

That can be used as

sage: sage: one_line_array_to_tensor(range(16),3,2,2)
[[[0, 1], [2, 3]], [[4, 5], [6, 7]], [[8, 9], [10, 11]]]
sage: one_line_array_to_tensor(range(16),2,3,2)
[[[0, 1], [2, 3], [4, 5]], [[6, 7], [8, 9], [10, 11]]]
sage: one_line_array_to_tensor(range(16),2,2,3)
[[[0, 1, 2], [3, 4, 5]], [[6, 7, 8], [9, 10, 11]]]

Then, you just need to iterate through the possible values:

sage: from itertools import product
sage: iterator = (one_line_array_to_tensor(p,2,2,2) for p in product((0,1,-1),repeat=2*2*2))
sage: for p in iterator:
....:    print p
[[[0, 0], [0, 0]], [[0, 0], [0, 0]]]
[[[0, 0], [0, 0]], [[0, 0], [0, 1]]]
[[[0, 0], [0, 0]], [[0, 0], [0, -1]]]
...
[[[-1, -1], [-1, -1]], [[-1, -1], [-1, -1]]]

Vincent