sympy: how to simplify trig expression
python, sympy
Solution
The following is probably not a practical approach, however, it shows that sometimes it may be worth experimenting with manipulating sin/cos expressions in their complex exponential version.
a.rewrite(sp.exp).simplify().expand().rewrite(sp.cos).simplify()
`7*sin(x) + 3*sqrt(3)*cos(x)`
Problem
I have a trig expression ``` (-14*sin(x)**3 + 35*sin(x) + 6*sqrt(3)*cos(x)**3 + 9*sqrt(3)*cos(x))/((cos(2*x) + 4)) ``` which I know simplifies to ``` sqrt(3)*3*cos(x) + 7*sin(x) ``` but I can't seem to find a way to do it using sympy. Is there a clever way of doing it? ``` In [1]: from sympy import * In [2]: from sympy.abc import x In [3]: a = (-14*sin(x)**3 + 35*sin(x) + 6*sqrt(3)*cos(x)**3 + 9*sqrt(3)*cos(x))/((cos(2*x) + 4)) In [4]: b = sqrt(3)*3*cos(x) + 7*sin(x) In [5]: trigsimp(a-b) Out[5]: 0 In [6]: trigsimp(a) Out[6]: (-14*sin(x)**3 + 35*sin(x) + 6*sqrt(3)*cos(x)**3 + 9*sqrt(3)*cos(x))/(cos(2*x) + 4) In [7]: a.simplify() Out[7]: (-14*sin(x)**3 + 35*sin(x) + 6*sqrt(3)*cos(x)**3 + 9*sqrt(3)*cos(x))/(cos(2*x) + 4) In [8]: trigsimp(expand_trig(a)) Out[8]: (-14*sin(x)**3 + 35*sin(x) + 6*sqrt(3)*cos(x)**3 + 9*sqrt(3)*cos(x))/(cos(2*x) + 4) In [9]: expand_trig(trigsimp(a)) Out[9]: (-14*sin(x)**3 + 35*sin(x) + 6*sqrt(3)*cos(x)**3 + 9*sqrt(3)*cos(x))/(2*cos(x)**2 + 3) In [10]: fu(a) Out[10]: (-14*sin(x)**3 + 35*sin(x) + 6*sqrt(3)*cos(x)**3 + 9*sqrt(3)*cos(x))/(cos(2*x) + 4) ```