T[Index] in Mathematica: Get Element with Variable Index

  • Context: Mathematica 
  • Thread starter Thread starter unih
  • Start date Start date
  • Tags Tags
    Mathematica
Click For Summary

Discussion Overview

The discussion revolves around how to access elements of a tensor in Mathematica using a variable index. Participants explore different methods for extracting elements from a multidimensional array, specifically focusing on performance considerations when performing this operation repeatedly.

Discussion Character

  • Technical explanation
  • Mathematical reasoning

Main Points Raised

  • One participant presents a tensor T with dimensions {3,3,4} and seeks a method to access an element using a variable index in list form.
  • Another participant suggests using the syntax T[[Sequence @@ index]] or T[[Apply[Sequence,index]]], indicating these methods can be used to extract the desired element.
  • A third participant expresses gratitude, noting that the suggested methods were not previously considered.
  • A different participant recommends using the Extract function for repeated operations, providing timing comparisons between using Sequence and Extract for accessing elements in a larger tensor.
  • The timing results indicate that Extract may be more efficient than using Sequence for this purpose, with specific performance metrics shared.
  • A tutorial link is provided for further reference on manipulating lists by their indices in Mathematica.

Areas of Agreement / Disagreement

Participants generally agree on the methods to access tensor elements but do not reach a consensus on the best approach for repeated operations, as performance considerations are discussed without a definitive conclusion.

Contextual Notes

The discussion includes performance metrics that depend on the specific tensor size and structure, which may vary in different contexts. The effectiveness of the proposed methods may also depend on the frequency of access and the size of the tensor.

Who May Find This Useful

Users of Mathematica interested in tensor manipulation, performance optimization in code, and those looking for efficient methods to access elements in multidimensional arrays may find this discussion relevant.

unih
Messages
27
Reaction score
0
Hi.
In Mathematica I have tensor T for example
In[]:=Dimensions[T]
Out[]:={3,3,4}

now i have index of some element in the List form
index={1,2,3}

Now I want to use the element whose index is in variable index. How can I do it?
 
Physics news on Phys.org
T[[Sequence @@ index]]
or
T[[Apply[Sequence,index]]]
 
Thank you very much! Its the only combination I didnt try!
 
If you need to do this operation repeatedly, you're better off using Extract:

Code:
In[4]:= T = Array[t, {5, 5, 5}];

In[8]:= SetOptions[TimeAv, Method -> {"MinNum", 1000000}];

In[9]:= T[[Sequence @@ {3, 3, 4}]] // TimeAv

During evaluation of In[9]:= Total wall time is 2.646485, total cpu time is 2.65217 and total time spent evaluating the expression is 2.65217
During evaluation of In[9]:= The expression was evaluated 1100010 times, in blocks of 110001 runs. This yields a mean timing of 2.41104*10^-6 with a blocked standard deviation of 2.28871*10^-7.

Out[9]= {2.41104*10^-6, t[3, 3, 4]}

In[10]:= Extract[T, {3, 3, 4}] // TimeAv

During evaluation of In[10]:= Total wall time is 0.614893, total cpu time is 0.616039 and total time spent evaluating the expression is 0.616039
During evaluation of In[10]:= The expression was evaluated 1100010 times, in blocks of 110001 runs. This yields a mean timing of 5.6003*10^-7 with a blocked standard deviation of 4.65709*10^-8.

Out[10]= {5.6003*10^-7, t[3, 3, 4]}

The tutorial http://reference.wolfram.com/mathematica/tutorial/ManipulatingListsByTheirIndices.html is worth looking at.
 
Thank you
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 2 ·
Replies
2
Views
3K