Processing math: 100%

First time here? Check out the FAQ!

Ask Your Question
1

Generation of all 3-index tensors with {-1,0,1} as entries.

asked 10 years ago

Ira gravatar image

updated 10 years ago

I need to generate all tensors Aijk (i.e. array with 3 indices), where i=0,,6, j=0,6, k=0,5, such that only integer numbers 1, 0 and 1 could be used as entries. There are $665*3$ such possibilities. What would be the shortest system of embedded loop for this in SAGE? I just started using SAGE and don't have any previous experience with Python. The only solution I was able to think of include hundreds of embedded loops.

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
1

answered 10 years ago

vdelecroix gravatar image

Hi,

I might have misunderstood, but the number of such arrays is $3^{776}whichisofthesizeof10^{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

Preview: (hide)
link

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 10 years ago

Seen: 307 times

Last updated: Oct 13 '14