3
$\begingroup$

I have two matrices, $A$ and $B$. The elements of these matrices are some abstract non-commuting objects, which I'm just representing as variables. For now, I don't care about having Mathematica know about the specifics of these commutation relations - all I care about is that it doesn't assume that they commute, so that, for example, when I calculate something like $\text{Tr}(A B)$, the expression that it returns keeps the elements of $A$ in front of the elements of $B$. Of course for something as simple as $\text{Tr}(AB)$ I can just rearrange the elements by hand, but I would like to have the ability to do this for more complex expressions, with potentially more than two matrices. It seems Mathematica has some built-in functionality for non-commutative multiplication, but this is only for scalars - is there something that generalizes this to matrices of non-commuting objects?

$\endgroup$
12
$\begingroup$

As Dot is a special case of Inner with Dot[##] == Inner[Times,##, Plus], we can just tell Mathematica to use NonCommutativeMultiply in the first slot to take the place of regular multiplication.

ncdot[ten1_, ten2_] := Inner[NonCommutativeMultiply, ten1, ten2, Plus]
ncdot[{x, y}, {u, v}]
ncdot[{{x, y}, {z, w}}, {{a, b}, {c, d}}]
Tr@%

x ** u + y ** v

{{x ** a + y ** c, x ** b + y ** d}, {w ** c + z ** a, w ** d + z ** b}}

w ** d + x ** a + y ** c + z ** b

This contrasts the automatic canonical sorting MMA does under the hood of the individual symbols as in

{x, y}.{u, v}
(*and*)
{{x, y}, {z, w}}.{{a, b}, {c, d}} // Tr

u x + v y

d w + a x + c y + b z

To use this for several items (e.g. matrices $A,B,C$), we incorporate/redefine Fold into the definition as

ncdot[tens__] := Fold[Inner[NonCommutativeMultiply, ##] &, {tens}]
(*Without the inclusion of the fourth argument of Inner, the default is Plus*)

Which we can use as

ncdot @@ Table[i[j, k], {i, {a, b, c}}, {j, 2}, {k, 2}];
% /. (i : (a | b | c))[j_, k_] :> Subscript[i, j\[InvisibleComma]k] // MatrixForm

$\left( \begin{array}{cc} \left(a_{1,1}\text{**}b_{1,1}+a_{1,2}\text{**}b_{2,1}\right)\text{**}c_{1,1}+\left(a_{1,1}\text{**}b_{ 1,2}+a_{1,2}\text{**}b_{2,2}\right)\text{**}c_{2,1} & \left(a_{1,1}\text{**}b_{1,1}+a_{1,2}\text{**}b_{2,1}\right)\text{**}c_{1,2}+\left(a_{1,1}\text{**}b_{ 1,2}+a_{1,2}\text{**}b_{2,2}\right)\text{**}c_{2,2} \\ \left(a_{2,1}\text{**}b_{1,1}+a_{2,2}\text{**}b_{2,1}\right)\text{**}c_{1,1}+\left(a_{2,1}\text{**}b_{ 1,2}+a_{2,2}\text{**}b_{2,2}\right)\text{**}c_{2,1} & \left(a_{2,1}\text{**}b_{1,1}+a_{2,2}\text{**}b_{2,1}\right)\text{**}c_{1,2}+\left(a_{2,1}\text{**}b_{ 1,2}+a_{2,2}\text{**}b_{2,2}\right)\text{**}c_{2,2} \\ \end{array} \right)$

$\endgroup$
3
$\begingroup$

Maybe this?

Tr[{{a, b}, {c, d}}.{{x, y}, {z, w}}] /. Times -> NonCommutativeMultiply
Tr[{{a, b}, {c, d}}.{{x, y}, {z, w}} /. Times -> NonCommutativeMultiply]

a ** x + b ** z + c ** y + d ** w

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.