6
$\begingroup$

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?

$\endgroup$
  • $\begingroup$ Ummm.... what is MWE? And why not write it out for those who want to help? $\endgroup$ – David G. Stork Dec 19 '17 at 22:43
  • $\begingroup$ @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! $\endgroup$ – egwene sedai Dec 19 '17 at 23:08
  • 1
    $\begingroup$ 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? $\endgroup$ – David G. Stork Dec 19 '17 at 23:19
  • $\begingroup$ @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 :-) $\endgroup$ – egwene sedai Dec 20 '17 at 14:44
  • $\begingroup$ 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. $\endgroup$ – Michael E2 Mar 9 at 13:08
6
$\begingroup$

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.

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

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

$\endgroup$
2
$\begingroup$

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]

$\endgroup$
0
$\begingroup$

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)$

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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