# Is there a Collect for TensorProduct?

I would like Mathematica to use the property that the TensorProduct is distributive to simplify expressions like

$A\otimes B+A\otimes C = A\otimes(B+C)$

Unfortunately neither Collect nor Simplify do the job

Collect[A \[TensorProduct] B + A \[TensorProduct] C, A]


and

Simplify[A \[TensorProduct] B + A \[TensorProduct] C]


both just return their argument. Is there a way to make Mathematica simplify such expressions?

• Block[{TensorProduct = Times}, Collect[A\[TensorProduct]B + A\[TensorProduct]C, A]] /. Times -> TensorProduct?
– kglr
Sep 7 '18 at 20:08

This is an experimental implementation of such a function:

tensorCollect[input_] := ReplaceRepeated[
input,
{
Plus[
summands___,
Times[x___, TensorProduct[a___, R_, b___, S_, c___]],
Times[y___, TensorProduct[a___, R_, b___, T_, c___]]
] :> Plus[summands, TensorProduct[a, R, b, Times[x, S] + Times[y, T], c]],
Plus[
summands___,
Times[x___, TensorProduct[a___, R_, b___, T_, c___]],
Times[y___, TensorProduct[a___, S_, b___, T_, c___]]
] :>
Plus[summands, TensorProduct[a, Times[x, R] + Times[y, S], b, T, c]]
}
]


A simple test:

input = 2 A\[TensorProduct]B\[TensorProduct]R\[TensorProduct]S + 2 T\[TensorProduct]S + 3 A\[TensorProduct]B\[TensorProduct]T\[TensorProduct]S;
output = tensorCollect[input]
TensorExpand[output] == input


2 T \[TensorProduct] S + A \[TensorProduct] B \[TensorProduct] (2 R + 3 T) \[TensorProduct] S

True

But it probably contains some bugs. This does also not attempt to collect as many terms as possible.

• Thanks a lot, so far this seems to work. I will do some more testing before accepting your answer Sep 10 '18 at 7:31
• I find that your function only works if there are coefficients in front of the Tensorproducts. For example tensorCollect[A \[TensorProduct] B + A \[TensorProduct] D] does not work Sep 12 '18 at 13:38
• @lennart Yeah, I know that it is not perfect. Yet, it might give you an idea how to proceed: You can add more and more transformation rules to improve upon tensorCollect. Sep 12 '18 at 13:42
• What is the meaning of the a___, b___, c___ in the tensorproduct? Everything seems to work just fine without them. Sep 12 '18 at 14:50
• The triple underscore stand for a sequence of zero or more elements. TensorProduct does not have the attribute Flat, so we have to account for the possibility that there are other factors around and between there relevant factors R, S, and T. And most importantly, these factors have to correspond to each other. That will only take effect with long TensorProducts. Sep 12 '18 at 15:09

This command will make it to move the left argument out of brackets if to execute it twice:

Unprotect[TensorProduct];
TensorProduct /: TensorProduct [a_ , b_] + TensorProduct[ a_ , c_] :=
TensorProduct[a, b + c]
Protect[TensorProduct];


Idk why it produces an error message

TagSetDelayed::write: Tag TensorProduct in a_[TensorProduct]b_+a_[TensorProduct]c_ is Protected.

after the first execution.