Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.
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}

works.

How can I fix this problem?

share|improve this question

3 Answers 3

up vote 3 down vote accepted

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
k
share|improve this answer
    
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. –  Analysis Aug 6 at 10:05
    
@Analysis You need another attribute OneIdentity added. See here for more details. –  xzczd Aug 6 at 10:19
    
Thanks! It works! –  Analysis Aug 6 at 10:31
k Esc.Esc k Esc.Esc k /. CenterDot[k, ___] -> k

gives the desired result

share|improve this answer
    
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} –  seismatica Aug 6 at 8:35

It's not answer but comment

Code

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

does not work becouse

FullForm[k Esc.Esc k Esc.Esc k]
(*CenterDot[k,k,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
(*CenterDot[k,k,k]*)

And you last working example

CenterDot[k,k] //. CenterDot[k,k] -> k
(*k*)
share|improve this answer
1  
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. –  xzczd Aug 6 at 8:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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