Applying TrigToExp only for terms containing certain variables

Consider a Minimum Working Example,

expr = Sin[a] (-I Cos[b] + Sin[b]),

is there some way to only apply TrigToExp[] for terms containing b (for a minimum-working example, output should be: -I E^(I b) Sin[a]), leaving other terms untouched? In my actual problem, the expression may be much more complex. I could, for example, apply

expr /. {Cos[b] -> TrigToExp[Cos[b]],
Sin[b] -> TrigToExp[Sin[b]]} // Simplify

and for all other triangular functions (Tan, etc), but is there a more elegant solution?

• Ummm.... what is MWE? And why not write it out for those who want to help? – David G. Stork Dec 19 '17 at 22:43
• @DavidG.Stork the mwe is to apply TrigToExp only for terms containing b in expr; reformatted the question to make it easier to read, thanks! – egwene sedai Dec 19 '17 at 23:08
• I have a Mathematica reputation of 18,500 and don't know what the acronym MWE stands for, and suspect at least a few others here don't either. Why not spell out the three words and be clear? – David G. Stork Dec 19 '17 at 23:19
• @DavidG.Stork thanks for the advice, I saw many just use this phrase for "Minimum Working Example", thought it's a standard here. it's good to be clear :-) – egwene sedai Dec 20 '17 at 14:44
• On the example, expr /. e : _[b] :> TrigToExp[e] or expr /. e_?(FreeQ[a]) :> TrigToExp[e] seems easiest. Depends on desired output, and whether the variables are known in advance (usually they are, aren't they?). Use expr /. e_?(FreeQ[a | c]) :> TrigToExp[e]` for more variables. – Michael E2 Mar 9 at 13:08

Does this suit your needs?

expr /. {x_[y_] /;
And[MemberQ[{Sin, Cos, Tan, Csc, Sec, Cot}, x],
Length[Position[y, b]] > 0]
:> TrigToExp[x[y]]}

Searches for trigonometric functions which contain b in their parameters and expands them.

• nice approach using MemberQ and Length[Position[]]! thanks – egwene sedai Dec 19 '17 at 22:03
Replace[expr, h : Except[_[_?(FreeQ[b])]] :> TrigToExp[h], ∞]

-I E^(I b) Sin[a]

Here's another variation, where I inactivate trig functions that don't contain b:

$trigs = Sin | Cos | Tan | Sec | Csc | Cot; Activate @ TrigToExp @ ReplaceAll[ expr, (h :$trigs)[x_?(FreeQ[b])] :> Inactive[h][x]
]

-I E^(I b) Sin[a]

A variation on @kglr's method:

expr /. h : _[_?(Not@*FreeQ[b])] :> TrigToExp[h]

$$\left(-i \left(\frac{e^{-i b}}{2}+\frac{e^{i b}}{2}\right)+\frac{1}{2} i e^{-i b}-\frac{1}{2} i e^{i b}\right) \sin (a)$$