Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Convert an expression involving ArrayDot, Dot, Transpose, Tr or MatrixPower into an equivalent expression using TensorContract
ResourceFunction["ToTensor"][expr] converts ArrayDot,Dot,Transpose,Tr and MatrixPower subexpressions into equivalent forms using TensorContract. |
Specify the properties of symbolic arrays:
| In[1]:= |
A tensor built using Tr and Dot:
| In[2]:= |
| Out[2]= |
Convert to an equivalent form using TensorContract:
| In[3]:= |
| Out[3]= |
Verify that the tensor calculation gives the same results as the equivalent matrix calculation:
| In[4]:= | ![]() |
| Out[5]= |
A tensor built using ArrayDot, Transpose and ArraySymbol:
| In[6]:= |
| Out[6]= |
Convert to an equivalent form using TensorContract:
| In[7]:= |
| Out[7]= |
Check:
| In[8]:= | ![]() |
| Out[9]= |
| Out[10]= |
Check equivalence of forms, assuming the two matrices A and B are square:
| In[11]:= | ![]() |
| Out[11]= |
A tensor consisting of Dot, Tr and MatrixPower:
| In[12]:= |
| Out[12]= |
Evaluate, assuming the two matrices are square:
| In[13]:= |
| Out[13]= |
TensorReduce is unable to reduce some mixed expressions consisting of both TensorContract and Dot:
| In[14]:= | ![]() |
| Out[16]= |
Using ToTensor trivially simplifies the expression:
| In[17]:= |
| Out[17]= |
Rank information for symbolic tensors must be specified using the Assumptions option or using $Assumptions:
| In[18]:= |
| Out[19]= |
Here the assumptions are added correctly:
| In[20]:= |
| Out[20]= |
Only Tr expression representing traces of the first two levels of an array have equivalent TensorContract representations:
| In[21]:= |
| Out[21]= |
This work is licensed under a Creative Commons Attribution 4.0 International License