# Operator products summing

Is there a way to perform the below summation in Mathematica?

$$\frac{\left(\hat{X}_{ij}+\mathbb i\delta_{ij}\right)}{\mathbb{i}}\frac{\left(\hat{X}_{kl}+\mathbb i\delta_{kl}\right)}{\mathbb{i}}$$

I am finding the issue because order matters here. X is operator. So it's order is important. g and X are antisymmetric tensors. The answer to the above equation I am expecting is:

$$-\text{test}=\sum_{i,j,k,l}g_{ijkl}\frac{\left(\hat{X}_{ij}+\mathbb i\delta_{ij}\right)}{\mathbb i}\frac{\left(\hat{X}_{kl}+\mathbb i\delta_{kl}\right)}{\mathbb i} =-\sum_{i,j,k,l}g_{ijkl}\left(\hat{X}_{ij}+\mathbb i\delta_{ij}\right)\left(\hat{X}_{kl}+\mathbb i\delta_{kl}\right) =-\sum_{i,j,k,l}g_{ijkl}\left(\hat{X}_{ij}\hat{X}_{kl}+\mathbb i\hat{X}_{ij}\delta_{kl}+\mathbb i\delta_{ij}\hat{X}_{kl}+\mathbb i\mathbb i\delta_{ij}\delta_{kl}\right) =-\left(\sum_{i,j,k,l}g_{ijkl}\hat{X}_{ij}\hat{X}_{kl}+\sum_{i,j,k,l}g_{ijkl}\mathbb i\hat{X}_{ij}\delta_{kl}+\sum_{i,j,k,l}g_{ijkl}\mathbb i\delta_{ij}\hat{X}_{kl}+\sum_{i,j,k,l}g_{ijkl}\mathbb i\mathbb i\delta_{ij}\delta_{kl}\right) =-\left(\sum_{i,j,k,l}g_{ijkl}\hat{X}_{ij}\hat{X}_{kl}+\sum_{i,j,k,l}g_{ijll}\mathbb i\hat{X}_{ij}+\sum_{i,j,k,l}g_{ijll}\mathbb i\hat{X}_{kl}+\sum_{i,j,k,l}g_{ijll}\mathbb i\mathbb i\delta_{ij}\right)$$

Since $$g$$ is an antisymmetric tensor, if any of the two indices are same then it will be zero. Thus, $$g_{ijll}=0$$

Thus we get the answer: $$test=-\sum_{i,j,k,l}g_{ijkl}\hat{X}_{ij}\hat{X}_{kl}$$

Can I implement this using the following code:

SumHeld /: MakeBoxes[SumHeld[expr_, ranges__], form_] :=
MakeBoxes[Sum[expr, ranges], form]

SumHeld /:
SyntaxInformation[
SumHeld] = {"LocalVariables" -> {"Table", {2, Infinity}}};
SumHeld /: c_?NumericQ SumHeld[rest_, range__] :=
SumHeld[c rest, range]
IndexUnify[HoldPattern@Plus[sums : SumHeld[_, __] ..]] :=
Plus @@ With[{targetIndices = List @@ #[[-1, 2 ;;, 1]],
sourceIndicesList = List @@@ #[[;; , 2 ;;, 1]]},
Function[{sum, sourceIndices},
sum /. Thread[
sourceIndices ->
Take[targetIndices, Length@sourceIndices]]] @@@
Transpose@{#, sourceIndicesList}] &@
SortBy[Flatten /@ {sums}, Length]

SumTogether[HoldPattern@Plus[sums : SumHeld[_, sameRanges__] ..]] :=
SumHeld[Plus @@ {sums}[[;; , 1]], sameRanges]
SumTogether[HoldPattern@Plus[sums : SumHeld[_, __] ..]] /;
UnsameQ @@ {sums}[[;; , 2 ;;]] :=
Plus @@ SumTogether@*Plus @@@ GatherBy[{sums}, Rest]

SumHeld[expr_, {lst_List,
dim_}] := (term |->
SumHeld[term,
Sequence @@
Table[If[Count[term, patt, ∞] == 2, {patt, dim},
Nothing], {patt, lst}]]) /@ expr


How to modify the above code to incorporate to handle operators?

• Is $i$ imaginary unit? Are you sure the result given in $\LaTeX$ is correct? Commented Apr 29, 2021 at 5:15
• @xzczd I have edited the question. There is a negative sign missing. Also i is imaginary unit. Commented Apr 29, 2021 at 5:30
• Then I suggest making the imaginary $\mathrm{i}$ upright, not to be confused with the index $i$. Commented Apr 29, 2021 at 5:51
• @ΑλέξανδροςΖεγγ Alternatively, one may consider \mathbf{i}. Commented Apr 29, 2021 at 6:01
• “I am finding the issue because order matters here. X is operator. So it's order is important. ” The statement is a bit confusing to me. Can you elaborate a bit? Commented Apr 29, 2021 at 6:44

## 1 Answer

SumHeld, IndexUnify and SumTogether are almost irrelevant to this task, please avoid utilizing others' code blindly. Then, though not a duplicate, this question is strongly related to your previous question. We just need to modify the rule there a little to make it handle non-commutative multiply.

We first define functions for expansion of non-commutative multiply. The first 3 definitions are taken from document of NonCommutativeMultiply:

ExpandNCM[(h : NonCommutativeMultiply)[a___, b_Times, c___]] :=
Most[b] ExpandNCM[h[a, Last[b], c]]
ExpandNCM[a_] := ExpandAll[a]
ExpandNCM[(h : NonCommutativeMultiply)[a___, b_Plus, c___]] :=
Distribute[h[a, b, c], Plus, h, Plus, ExpandNCM[h[##]] &]

ExpandNCM[c_ (h : NonCommutativeMultiply)@a__] := c ExpandNCM[h@a] // Expand


Then, modify a bit the definitions of Kronecker delta in my previous answer to make it handle non-commutative multiply:

Clear[δ];
SetAttributes[δ, Orderless]
δ /: δ[a_, b_] h_[former___, b_, latter___] := h[former, a, latter]

δ /: (expr : Except[__δ]) ** δ[a_, b_] := δ[a, b] expr
δ /: δ[a_, b_] ** (expr : Except[__δ]) := δ[a, b] expr


Finally, take the definition of antisymmetric tensor from my previous answer and add one more definition so that $$g_{ijll}=0$$:

g[arg__] /; ! OrderedQ@{arg} := Signature@{arg} g @@ Sort@{arg}

g[___, a_, a_, ___] := 0


Now the desired output is obtained:

g[i, j, k, l] ((X[i, j] + I δ[i, j])/I) ** ((X[k, l] + I δ[k, l])/I) // ExpandNCM
(* -g[i, j, k, l] X[i, j] ** X[k, l] *)