# How do I apply complicated algebraic/trigonometric manipulations to nested expressions?

Consider the expression $$a\sin(x)-a\sin\left(\frac{x}{a}\left(a+\frac{ay}{x}\right)\right) \label{a}\tag{1}$$ which can be rewritten as $$\sin(x)-\sin\left(x+y\right) \label{b} \tag{2}$$ and finally as $$-2 \sin(y/2) \cos(x+y/2) \label{c} \tag{3}$$ I have a more complicated expression, where an expression such as (\ref{a}) is embedded. I want Mathematica to apply the transformation "(\ref{b}) -> (\ref{c})" to this expression, without any further simplifications. Note that the variable names may differ and I want to inform Mathematica about "(\ref{b}) -> (\ref{c})" in the general most way. That is for example, without the prefactor $$a$$ in (\ref{a}) or without giving the explicit structure of the parentheses in the $$\sin$$ on the right hand side of (\ref{a}).

a Sin[x] - a Sin[x/a (a + a y/x)] // FullSimplify // TrigFactor

g[a Sin[x] - a Sin[x/a (a + a y/x)]] /. g[a_] :> g[TrigFactor[FullSimplify[a]]]

• Unfortunately, that does not work if the difference of the sines is wrapped inside Exp. Mar 16, 2020 at 17:40
• If g is Exp I have to explicitly use Exp on the right hand side of /., otherwise this still does not work. Furthermore, if there are other things wrapped around the Sines this still does not work, e.g.: Exp[t + a Sin[x] - a Sin[x/a (a + a y/x)]] /. Exp[x_] :> Exp[TrigFactor[ExpandAll[x]]]. I guess that's because the sines are wrapped in something because of the +. Mar 16, 2020 at 19:29