Sympy: how to simplify a trigger expression

I have a trigger 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 cannot find a way to do it with sympy. Is there a smart way to do this?

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)

      

+3


source to share


1 answer


However, this is probably not a practical approach, but it does show that sometimes it is 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)

+2


source







All Articles