# Define trace-like function (e.g. cyclically invariant) involving operators without built-in meaning

I am interested in defining a function, say $$\mathrm{Tra}$$, that imply operators without build-in meaning, say it involves products with the $$\cdot$$ (Esc . Esc), not assumed commuatative.

I want to mimic a trace for it, the main property being cyclic invariance, which I expect to use to simplify expressions. Now I face a syntax problem: the last two operators being the same, as is in general, when one wants to impose some invariance.

esq[A_CenterDot] := RotateLeft@A (*rotates to the CenterDot-product*)
Tra[A_Plus] :=Tra[#] & /@ A (*make Tra additive*)
Tra[A_CenterDot] := Tra[esq[A]]


Second line I've learnt from this answer by Jules Lamers, mutatis mutandis.

With last command I was expecting $$\mathrm{Tra}(x\cdot y\cdot y\cdot x\cdot y\cdot x)$$ to yield $$\mathrm{Tra}(x\cdot x \cdot y\cdot y\cdot x\cdot y)$$ (with only one left shift, but see below the aimed lexicographic order).

But this of course gives a loop instead. In general,

• how does one correct this?

• if possible: how to deal with situations when "the two last arrows of the commutative diagram are the same operation"?

• is there an way out to defining relations at the level of the algebra just before evaluating this "trace"? What is meant is: assuming that $$\mathrm{Tra}: A \to \mathbb k$$, for any $$n$$, here there is a commuting pentagon (joining the first and last $$A^n$$ at the same node)

$$A^n \stackrel{left-shift}{\to} A^n \stackrel{\cdot}{\to} A \stackrel{Tra}{\to} \mathbb k\stackrel{Tra}{\leftarrow } A \stackrel{\cdot}{\leftarrow} A^n$$

Edit: After the comment(s): I wish the routine to stop, say, after the lexicographic-like order is reached. All x before y and the highest power of x showing up first, independently of what happens thereafter. So, inside the Tra-operator:

• $$x \cdot y\cdot x \cdot x \to x\cdot x \cdot x \cdot y$$ (cyclicity allows here to group all the x together)
• $$y \cdot y\cdot y \cdot x \to x\cdot y \cdot y\cdot y$$
• $$x\cdot y \cdot x\cdot y\cdot y \cdot x \cdot x \to x \cdot x \cdot x\cdot y \cdot x\cdot y\cdot y$$ (this cannot be tested with OrderedQ, will almost always false)

Output example is then:

$$\mathrm{Tra}[ y \cdot y\cdot y \cdot x + x\cdot y \cdot y\cdot y ] \to 2\mathrm{Tra}[x\cdot y \cdot y\cdot y]$$

since the second line above makes Tra additive, but need not be. It suffices that $$\mathrm{Tra}[ y \cdot y\cdot y \cdot x]$$ is replaced by $$\mathrm{Tra}[x\cdot y \cdot y\cdot y]$$ in order to simplify.

The issue with the lexicographic for the non-commutative product order is that the test in the given answer will be always false, unless there is no interlaced y's and x's, but that's the exception.

• Obviously the definitions you gave lead to infinite cycling. What exactly should be the stopping condition? That is, “it should apply RotateLeft IF ...” or “it should stop applying RotateLeft IF ...”. You say something about “arrows in the commutative diagram” but I don’t know how to relate that to your expressions. In other words, you show the desired output for the Tra sample expression. Can you explain why that should be the desired output and not, say, one that was rotated twice, or not rotated at all? Jun 4, 2020 at 14:07
• I hope is slightly clearer now. I didn't want to sound like a wish list, given that my programming skills are poor. But if is relevant, I should have added it.
– João
Jun 4, 2020 at 14:23
• Still unclear... Why not giving a few examples and the desired output. Jun 4, 2020 at 14:32
• @yarchik Ok, I added it. I also made Tra additive, in order for the example to make sense.
– João
Jun 4, 2020 at 14:41
• your stopping condition does not rule out infinite loops: check OrderedQ[RotateLeft[ x\[CenterDot]y\[CenterDot]y\[CenterDot]x\[CenterDot]y\[CenterDot]\ x, #]] & /@ Range[6]
– kglr
Jun 4, 2020 at 15:08

Perhaps something like:

ClearAll[esq, Tra]

esq[A_CenterDot] :=  NestWhile[RotateLeft, A, Not @* OrderedQ, 1, Length[A] - 1]

Tra[Y_Plus] := Tra[#] & /@ Y

Tra[A_CenterDot] := esq @ A


Examples:

Tra[x·y·y·x·y·x]

 x·x·y·y·x·y

Tra[x·y·y·y]

 x·y·y·y

 Tra[y·x·y·y·y]

  x·y·y·y·y

Tra[y·y·y·x +  x·y·y·y]

 2 x·y·y·y

• Great. The only problem is that Tra[A_CenterDot] := esq@A only makes the lexicographic order. I want that under Tra the lexicographic order holds. So I naively added Tra as before, to the end of the code Tra[A_CenterDot] := Tra[esq@A], and that gives loops. Still your solution manages the cyclic permutations and is great.
– João
Jun 4, 2020 at 15:59
• Which is my fault, since I poorly stated also a condition: it is the order on a circle what matters, so the x's are grouped acoording to the highest power on the circle. My bad.
– João
Jun 4, 2020 at 16:29