Skip to main content

simplifying expressions - Function to convert TensorContract[_TensorProduct, indices] into equivalent Dot + Tr version


Mathematica can use either Dot + Tr to represent some tensors, or TensorContract + TensorProduct. I believe that the TensorContract + TensorProduct representation, while verbose, is more powerful for a couple reasons:



  1. It can represent a wider variety of tensors, e.g., TensorContract[TensorProduct[a, b], {{1, 4}, {2, 5}, {3, 6}}] where a and b are rank 3 tensors doesn't have an equivalent Dot + Tr representation (at least, I can't think of one).

  2. TensorReduce can in some cases reduce pure TensorContract + TensorProduct expressions better than the equivalent Dot + Tr expressions.



Because of the above, it would be convenient to have a function that converted a Dot + Tr representation into a TensorContract + TensorProduct representation. Another reason why it would be nice to have such a function is that TensorReduce of a pure TensorContract + TensorProduct often works much better than TensorReduce of a mixture of a Dot + Tr and TensorContract + TensorProduct representation.


Pure vs mixed


Here is an example where TensorReduce works better with pure TensorContract representations instead of mixed representations:


TensorReduce[
r.R - TensorContract[TensorProduct[R, r], {{1, 2}}],
Assumptions -> (r|R) \[Element] Vectors[3]
]

TensorReduce[

TensorContract[TensorProduct[r, R], {{1, 2}}] - TensorContract[TensorProduct[R, r], {{1, 2}}],
Assumptions -> (r|R) \[Element] Vectors[3]
]


r.R - TensorContract[r[TensorProduct]R, {{1, 2}}]


0



ToTensor


The following function can be used to convert at Dot + Tr representation into a TensorContract + TensorProduct representation:



ToTensor[expr_] := expr /. {Dot->dot, Tr->tr}

dot[a__] := With[{indices = Accumulate@Map[TensorRank]@{a}},
TensorContract[TensorProduct[a], {#, # + 1} & /@ Most[indices]]
]

tr[a_] /; TensorRank[a] == 2 := TensorContract[a, {{1, 2}}]
tr[a_, Plus, 2] := TensorContract[a, {{1, 2}}]
tr[a___] := Tr[a]


FromTensor


It would be nice to have a function that converts a TensorContract + TensorProduct representation into a Dot + Tr representation, if possible. Let's call such a function FromTensor. Then, a TensorSimplify function that does something like FromTensor @ TensorReduce @ ToTensor @ expr could be defined that is as powerful as a simple TensorReduce, but allows one to work with Dot + Tr or mixed representations.


Examples


The kinds of TensorContract + TensorProduct representations that should be converted into a Dot + Tr representation include at least the following, where a and b are vectors, and m and n are matrices:



  1. Tr[m.n] ⇔ TensorContract[TensorProduct[m, n], {{1, 4}, {2,3}}]

  2. m.n ⇔ TensorContract[TensorProduct[m, n], {{2, 3}}]

  3. a.m.n ⇔ TensorContract[TensorProduct[a, m, n], {{1, 2}, {3, 4}}]

  4. a.m.n.b ⇔ TensorContract[TensorProduct[a, m, n, b], {{1, 2}, {3, 4}, {5, 6}}]



Some other similar examples:



  1. a.Transpose[n].Transpose[m] ⇔ TensorContract[TensorProduct[a, m, n], {{1, 5}, {4, 3}}]

  2. Tr[Transpose[m].n] ⇔ TensorContract[TensorProduct[m, n], {{1, 3}, {2, 4}}]


There may be other equivalent representations.


So, my question is, can somebody write such a FromTensor function?


(I have written such a function, but I am unhappy with it. I'm hopeful that someone can write a better one. I will post my version as an answer at some point, but for now I'm curious what other independent answers are possible)




Comments

Popular posts from this blog

functions - Get leading series expansion term?

Given a function f[x] , I would like to have a function leadingSeries that returns just the leading term in the series around x=0 . For example: leadingSeries[(1/x + 2)/(4 + 1/x^2 + x)] x and leadingSeries[(1/x + 2 + (1 - 1/x^3)/4)/(4 + x)] -(1/(16 x^3)) Is there such a function in Mathematica? Or maybe one can implement it efficiently? EDIT I finally went with the following implementation, based on Carl Woll 's answer: lds[ex_,x_]:=( (ex/.x->(x+O[x]^2))/.SeriesData[U_,Z_,L_List,Mi_,Ma_,De_]:>SeriesData[U,Z,{L[[1]]},Mi,Mi+1,De]//Quiet//Normal) The advantage is, that this one also properly works with functions whose leading term is a constant: lds[Exp[x],x] 1 Answer Update 1 Updated to eliminate SeriesData and to not return additional terms Perhaps you could use: leadingSeries[expr_, x_] := Normal[expr /. x->(x+O[x]^2) /. a_List :> Take[a, 1]] Then for your examples: leadingSeries[(1/x + 2)/(4 + 1/x^2 + x), x] leadingSeries[Exp[x], x] leadingSeries[(1/x + 2 + (1 - 1/x...

mathematical optimization - Minimizing using indices, error: Part::pkspec1: The expression cannot be used as a part specification

I want to use Minimize where the variables to minimize are indices pointing into an array. Here a MWE that hopefully shows what my problem is. vars = u@# & /@ Range[3]; cons = Flatten@ { Table[(u[j] != #) & /@ vars[[j + 1 ;; -1]], {j, 1, 3 - 1}], 1 vec1 = {1, 2, 3}; vec2 = {1, 2, 3}; Minimize[{Total@((vec1[[#]] - vec2[[u[#]]])^2 & /@ Range[1, 3]), cons}, vars, Integers] The error I get: Part::pkspec1: The expression u[1] cannot be used as a part specification. >> Answer Ok, it seems that one can get around Mathematica trying to evaluate vec2[[u[1]]] too early by using the function Indexed[vec2,u[1]] . The working MWE would then look like the following: vars = u@# & /@ Range[3]; cons = Flatten@{ Table[(u[j] != #) & /@ vars[[j + 1 ;; -1]], {j, 1, 3 - 1}], 1 vec1 = {1, 2, 3}; vec2 = {1, 2, 3}; NMinimize[ {Total@((vec1[[#]] - Indexed[vec2, u[#]])^2 & /@ R...

plotting - Plot 4D data with color as 4th dimension

I have a list of 4D data (x position, y position, amplitude, wavelength). I want to plot x, y, and amplitude on a 3D plot and have the color of the points correspond to the wavelength. I have seen many examples using functions to define color but my wavelength cannot be expressed by an analytic function. Is there a simple way to do this? Answer Here a another possible way to visualize 4D data: data = Flatten[Table[{x, y, x^2 + y^2, Sin[x - y]}, {x, -Pi, Pi,Pi/10}, {y,-Pi,Pi, Pi/10}], 1]; You can use the function Point along with VertexColors . Now the points are places using the first three elements and the color is determined by the fourth. In this case I used Hue, but you can use whatever you prefer. Graphics3D[ Point[data[[All, 1 ;; 3]], VertexColors -> Hue /@ data[[All, 4]]], Axes -> True, BoxRatios -> {1, 1, 1/GoldenRatio}]