Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rewrite `sin(x)^2` to cos(2*x) form in Sympy

Tags:

sympy

It is easy to obtain such rewrite in other CAS like Mathematica.

TrigReduce[Sin[x]^2]

(*1/2 (1 - Cos[2 x])*)

However, in Sympy, trigsimp with all methods tested returns sin(x)**2

trigsimp(sin(x)*sin(x),method='fu')

like image 413
Kattern Avatar asked Oct 22 '25 13:10

Kattern


1 Answers

While dealing with a similar issue, reducing the order of sin(x)**6, I notice that sympy can reduce the order of sin(x)**n with n=2,3,4,5,... by using, rewrite, expand, and then rewrite, followed by simplify, as shown here:

expr = sin(x)**6
expr.rewrite(sin, exp).expand().rewrite(exp, sin).simplify()

this returns:

-15*cos(2*x)/32 + 3*cos(4*x)/16 - cos(6*x)/32 + 5/16

That works for every power similarly to what Mathematica will do.

On the other hand if you want to reduce sin(x)**2*cos(x) a similar strategy works. In that case you have to rewrite the cos and sin to exp and as before expand rewrite and simplify again as:

(sin(x)**2*cos(x)).rewrite(sin, exp).rewrite(cos, exp).expand().rewrite(exp, sin).simplify()

that returns:

cos(x)/4 - cos(3*x)/4
like image 119
Juan Osorio Avatar answered Oct 27 '25 07:10

Juan Osorio