# ReplaceAll (/.) not working as expected

I am trying to replace some variables in the form of n*wt with wt. However, I came across some cases where the /. gives unexpected results. For example, -Cos[\[Pi]/6 - 6*wt] + 7/5 Cos[\[Pi]/6 + 6* wt] /. {6*wt -> wt} would give me the result -Cos[\[Pi]/6 - 6 wt] + 7/5 Cos[\[Pi]/6 + wt]. It seems that the replacement worked for the second half of the expression, but not the first half. Could anyone explain why is this and what would be a reliable way to do this?

• Evaluate -Cos[\[Pi]/6 - 6*wt] // FullForm Mar 10, 2020 at 22:25
• adding to Edmunds comment: it is vital to understand that the Mathematica pattern matching is working with the internal representation (FullForm) of expressions, it is not based on mathematical identities. Mar 11, 2020 at 19:42

You can use

-Cos[π/6 - 6*wt] + 7/5 Cos[π/6 + 6*wt] /. {a_ wt :> Sign[a] wt}


or, if you want to replace only coefficients 6 or -6,:

-Cos[π/6 - 6*wt] + 7/5 Cos[π/6 + 6*wt] /. {(a : 6 | -6) wt :> Sign[a] wt}


to get

-Cos[[Pi]/6 - wt] + 7/5 Cos[[Pi]/6 + wt]

Mathematica is generally more reliable if your replacement is for a single variable rather than an expression, such as:

-Cos[π/6 - 6*wt] + 7/5 Cos[π/6 + 6*wt] /. {wt -> wt/6}
(*7/5 Cos[wt + π/6] - Cos[π/6 - wt]*)


you may try something like:

    -Cos[\[Pi]/6 - 6*wt] + 7/5 Cos[\[Pi]/6 + 6*wt] /. Cos[x_ + y_] :> Cos[x + y/6]