I have an expression where terms Transpose[R].R appear. R is a matrix with the property Transpose[R].R=IdentityMatrix[3].

How do I remove all the sub-expressions of type Transpose[R].R from an expression? Example: suppose I have the expression:


I want to transform it into

  • 3
    $\begingroup$ /. Transpose[x_].x_ -> Sequence[]. Have you read the documentation re: patterns, replacement, etc? This is not an interactive documentation answer site :-) Also, avoid using uppercase initials for your symbols - or you're asking for trouble. $\endgroup$
    – ciao
    Commented Jul 21, 2015 at 22:19
  • 1
    $\begingroup$ @ciao Fantastic, thanks a lot. Yes, I always ask only after searching the documentation, which did not show Sequence[] where I looked. How did you learn about this command? $\endgroup$ Commented Jul 21, 2015 at 22:21
  • $\begingroup$ Sequence is in the documentation... and the documentation certainly is not a cookbook for pattern machinations, though it does have copious examples. Best way to learn those kind of things is peruse the docs, find something interesting, and play with it in "what if" kind of ways. I've found that to be most effective teaching others term-rewriting languages like MMA. $\endgroup$
    – ciao
    Commented Jul 21, 2015 at 22:24
  • $\begingroup$ @ciao. Thanks. Can you tell me one last thing. What would I write for xyz if I want in Select[expr,!FreeQ[#,xyz]&] to select any term of expr which contains a pattern R.(something, don't care what).R? Thanks a lot! $\endgroup$ Commented Jul 21, 2015 at 22:30
  • 2
    $\begingroup$ Incidentally ciao's replacement relies on special evaluation rules for Dot due to it having the Flat attribute. This is something in which you should invest the time to understand. $\endgroup$
    – Mr.Wizard
    Commented Jul 21, 2015 at 22:32

1 Answer 1


I suppose you would like something like this?

Select[A.B.Transpose[R].R.T, FreeQ[#, R] &]

This mainly used the expression's tree like property. Due to Dot's Flat Property, the whole expression will actually looks like Dot[A,B,Transpose[R],R,T], thus this code can generate the result you want:


Note that I've change your C to T cause C is a built in symbol.....

Or you may use this if you only want to remove it when Transpose[R] and R are ordered in this particular way:

A.B.Transpose[R].R.T //. Dot[x___, Transpose[R], R, y___] -> Dot[x, y]

  • $\begingroup$ Your first method doesn't work on A.B.R.Transpose[R].R.T, just on the OP's toy example. $\endgroup$
    – Michael E2
    Commented Jun 28, 2016 at 16:39
  • $\begingroup$ It worked~ Though a incorrect result as it will delete everthing related to R...... $\endgroup$
    – Wjx
    Commented Jul 1, 2016 at 11:16

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