k.k.k //. {k.k -> k}

works perfectly, so the output is just k.

But, when I use Esc.Esc (center dot, CenterDot[k,k]) instead of . (comma),

k Esc.Esc k Esc.Esc k //. {k Esc.Esc k -> k}

does not work while

k Esc.Esc k //. {k Esc.Esc k -> k}


How can I fix this problem?


That's because Dot has the atrribute Flat while CenterDot doesn't:

{Dot, CenterDot} // Attributes
{{Flat, OneIdentity, Protected}, {}}

As the document said,

In pattern matching, Flat allows sequences of elements to be replaced:

 SetAttributes[f, Flat]
 f[a, b, c, d, e] /. f[b, c, d] -> x
f[a, x, e]

So, to fix your problem, just set Flat attribute to CenterDot:

SetAttributes[CenterDot, Flat]
k·k·k //. k·k -> k
  • $\begingroup$ It works but how about this situation? (I set CenterDot to be flat) CenterDot[a, b] //. CenterDot[x_, b] -> x This code returns not a, but CenterDot[a]. I don't know why this happened. $\endgroup$ – Analysis Aug 6 '14 at 10:05
  • $\begingroup$ @Analysis You need another attribute OneIdentity added. See here for more details. $\endgroup$ – xzczd Aug 6 '14 at 10:19
k Esc.Esc k Esc.Esc k /. CenterDot[k, ___] -> k

gives the desired result

  • $\begingroup$ For a more general rule, you can use //.{CenterDot[a___,x_,x_,b___]:>CenterDot[a,x,b],CenterDot[x_]:>x}. For example, try evaluating k\[CenterDot]k\[CenterDot]k\[CenterDot]g\[CenterDot]g\[CenterDot]k\[CenterDot]h//.{CenterDot[a___,x_,x_,b___]:>CenterDot[a,x,b],CenterDot[x_]:>x} $\endgroup$ – seismatica Aug 6 '14 at 8:35

It's not answer but comment


k Esc.Esc k Esc.Esc k //. {k Esc.Esc k -> k}

does not work becouse

FullForm[k Esc.Esc k Esc.Esc k]

and you try replace CenterDot[k,k] (that is missing on the left side) by k

Finally write it in more programatic style

CenterDot[k,k,k] //. CenterDot[k,k] -> k

And you last working example

CenterDot[k,k] //. CenterDot[k,k] -> k
  • 1
    $\begingroup$ Well, not quite correct because if you check the FullForm of k.k.k what you'll see is Dot[k, k, k], the true cause lies in the Attributes. $\endgroup$ – xzczd Aug 6 '14 at 8:48

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.