# How to remove parts of an expression [closed]

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:

A.B.Transpose[R].R.C


I want to transform it into

A.B.C

• /. 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. – ciao Jul 21 '15 at 22:19
• @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? – space_voyager Jul 21 '15 at 22:21
• 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. – ciao Jul 21 '15 at 22:24
• @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! – space_voyager Jul 21 '15 at 22:30
• 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. – Mr.Wizard Jul 21 '15 at 22:32

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:

A.B.T


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]

(*A.B.T*)

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