Mathematica 9 is now available
Student Support Forum
-----
Student Support Forum: 'passing tensor indexes to Module' topicStudent Support Forum > General > Archives > "passing tensor indexes to Module"

< Previous Comment | Next Comment >Help | Reply To Comment | Reply To Topic
Author Comment/Response
Peter Pein
10/30/11 04:57am

Hi Igor,

since you need the sequence of indices, not the list, try these:

In[1]:= T = Array[a, {3, 3, 3}];

one possibility: convert the list to the sequence of elements:

In[2]:= test1[ix_List, t_List] := t[[Sequence @@ ix]]
In[3]:= test1[{1, 2, 3}, T]
Out[3]= a[1, 2, 3]

another one with less typing :)
let the pattern matcher do the above for you:

In[4]:= test2[{ix : (_Integer | All | _Span) ..}, t_List] :=
t[[ix]]

In[5]:= test2[{2, 1, 3}, T]

Out[5]= a[2, 1, 3]

In[6]:= test2[{3, 1}, T]

Out[6]= {a[3, 1, 1], a[3, 1, 2], a[3, 1, 3]}

In[7]:= test2[{1, 2 ;; 3, 1}, T]

Out[7]= {a[1, 2, 1], a[1, 3, 1]}

In[8]:= test2[{All, 2, 3}, T]

Out[8]= {a[1, 2, 3], a[2, 2, 3], a[3, 2, 3]}

Peter




URL: ,

Subject (listing for 'passing tensor indexes to Module')
Author Date Posted
passing tensor indexes to Module Igor 10/27/11 5:58pm
Re: passing tensor indexes to Module Peter Pein 10/30/11 04:57am
Re: Re: passing tensor indexes to Module Igor 10/30/11 6:18pm
< Previous Comment | Next Comment >Help | Reply To Comment | Reply To Topic