# why doesn't the replacement rule work?

I have the following code:

S0 = {{a, 0}, {0, b}};
R[θ_] := {{Cos[θ], Sin[θ]}, {-Sin[θ],
Cos[θ]}};
S[θ_] := Dot[R[θ], S0, R[-θ]] // FullSimplify
S[θ] = S[θ] /. {(Cos[θ])^2 -> (1 + Cos[2 θ])/2} // FullSimplify
(*
{{a Cos[θ]^2 +
b Sin[θ]^2, (-a + b) Cos[θ] Sin[θ]}, {(-a +
b) Cos[θ] Sin[θ],
b Cos[θ]^2 + a Sin[θ]^2}}
*)
S[θ] = S[θ] /. {(Sin[θ])^2 -> (1 - Cos[2 θ])/2} // FullSimplify
(*
{{1/2 (a + b + (a - b) Cos[2 θ]), (-a +
b) Cos[θ] Sin[θ]}, {(-a +
b) Cos[θ] Sin[θ],
1/2 (a + b + (-a + b) Cos[2 θ])}}
*)
S[θ] = S[θ] /. {Cos[θ] Sin[θ] -> 1/2 Sin[2 θ]} //FullSimplify
(*
{{1/2 (a + b + (a - b) Cos[2 θ]), (-a +
b) Cos[θ] Sin[θ]}, {(-a +
b) Cos[θ] Sin[θ],
1/2 (a + b + (-a + b) Cos[2 θ])}}
*)


I wonder why the replacement rule in the last line of code doesn't work?

• Check 1/2 Sin[2 θ] // FullSimplify Oct 19, 2015 at 18:04

The reason is that FullSimplify
Mathematica regards $\cos\theta\sin\theta$ simpler than $\frac{1}{2}\sin 2\theta$, so when you run the code, FullSimplify right after the replacement rule, $\frac{1}{2}\sin 2\theta$ is converted to $\cos\theta\sin\theta$ again.
The solution is to write the replacement rule after FullSimplify
So the right order for the code can be as follows:

S0 = {{a, 0}, {0, b}};
R[θ_] := {{Cos[θ], Sin[θ]}, {-Sin[θ],
Cos[θ]}};
S[θ_] := Dot[R[θ], S0, R[-θ]] // FullSimplify;
S[θ] =
S[θ] /. {(Cos[θ])^2 -> (1 + Cos[2 θ])/2};
S[θ] =
S[θ] /. {(Sin[θ])^2 -> (1 - Cos[2 θ])/2};
S[θ] = S[θ] // FullSimplify;
S[θ] =
S[θ] /. {Cos[θ] Sin[θ] -> Sin[2 θ]/2};
S[θ] = S[θ] /. {a + b -> C};
S[θ] = S[θ] /. {a - b -> D };
S[θ] = S[θ] /. {b - a -> -D };

$\left( \begin{array}{cc} \frac{1}{2} (C+D (\cos (2 \theta ))) & -\frac{1}{2} D (\sin (2 \theta )) \\ -\frac{1}{2} D (\sin (2 \theta )) & \frac{1}{2} (C-D (\cos (2 \theta ))) \\ \end{array} \right)$