0
$\begingroup$

I have expressions like

tc*Cos[2*w*(t0+tc)]

and I want to replace tc by something else, but only if it occurs inside Cos, i.e. the outer tc should remain as is. Is there any way of specifying the context in which Replace should act? Maybe some clever pattern tricks?

Note that the actual expressions I'm working on are more complicated and consist of more (similar) terms, so just skipping over the first occurrence isn't going to cut it.

Thanks!

$\endgroup$
2
  • 1
    $\begingroup$ ReplaceAll[c_Cos?(Not@*FreeQ[tc]):>ReplaceAll[c, tc->new]] should do it. It's not a nice pattern to work with. Will be slow. But if the tc can appear in an arbitrary expression within Cos then there's not too much else I know to do. $\endgroup$
    – b3m2a1
    Dec 6, 2017 at 16:43
  • $\begingroup$ The solution by QuantumDot below works like a charm! Also, in the mean time I've found a related, more general question (with answers) here $\endgroup$
    – Jesse Slim
    Dec 12, 2017 at 13:09

1 Answer 1

2
$\begingroup$

My suggestion is: if you have

expr = tc*Cos[2*w*(t0+tc)]

Then to make the replacement, use

expr /. (Cos[arg_] :> Cos[arg /. tc -> new])

tc Cos[2 (new + t0) w]

$\endgroup$

Your Answer

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

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