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:
- It can represent a wider variety of tensors, e.g.,
TensorContract[TensorProduct[a, b], {{1, 4}, {2, 5}, {3, 6}}]whereaandbare rank 3 tensors doesn't have an equivalentDot+Trrepresentation (at least, I can't think of one). TensorReducecan in some cases reduce pureTensorContract+TensorProductexpressions better than the equivalentDot+Trexpressions.
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:
Tr[m.n]⇔TensorContract[TensorProduct[m, n], {{1, 4}, {2,3}}]m.n⇔TensorContract[TensorProduct[m, n], {{2, 3}}]a.m.n⇔TensorContract[TensorProduct[a, m, n], {{1, 2}, {3, 4}}]a.m.n.b⇔TensorContract[TensorProduct[a, m, n, b], {{1, 2}, {3, 4}, {5, 6}}]
Some other similar examples:
a.Transpose[n].Transpose[m]⇔TensorContract[TensorProduct[a, m, n], {{1, 5}, {4, 3}}]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
Post a Comment