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?
3 Answers
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]
-Cos[\[Pi]/6 - 6*wt] // FullForm
$\endgroup$FullForm
) of expressions, it is not based on mathematical identities. $\endgroup$