5
$\begingroup$

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?

$\endgroup$
2
  • 6
    $\begingroup$ Evaluate -Cos[\[Pi]/6 - 6*wt] // FullForm $\endgroup$
    – Edmund
    Mar 10, 2020 at 22:25
  • 2
    $\begingroup$ 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. $\endgroup$ Mar 11, 2020 at 19:42

3 Answers 3

9
$\begingroup$

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]

$\endgroup$
8
$\begingroup$

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]*)
$\endgroup$
3
$\begingroup$

you may try something like:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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