4

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

Kattern
  • 2,949
  • 5
  • 20
  • 28

3 Answers3

3

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
Juan Osorio
  • 378
  • 3
  • 15
2

Here is a silly way to get this job done.

trigsimp((sin(x)**2).rewrite(tan))

returns: -cos(2*x)/2 + 1/2

also works for

trigsimp((sin(x)**3).rewrite(tan))

returns 3*sin(x)/4 - sin(3*x)/4

but not works for

trigsimp((sin(x)**2*cos(x)).rewrite(tan))

retruns 4*(-tan(x/2)**2 + 1)*cos(x/2)**6*tan(x/2)**2

Kattern
  • 2,949
  • 5
  • 20
  • 28
2

The full "fu" method tries many different combinations of transformations to find "the best" result.

The individual transforms used in the Fu-routines can be used to do targeted transformations. You will have to read the documentation to learn what the different functions do, but just running through the functions of the FU dictionary identifies TR8 as your workhorse here:

    >>> for f in FU.keys():
    ...   print("{}: {}".format(f, FU[f](sin(var('x'))**2)))
    ...
8<---
    TR8 -cos(2*x)/2 + 1/2
    TR1 sin(x)**2
8<---
ti7
  • 16,375
  • 6
  • 40
  • 68
smichr
  • 16,948
  • 2
  • 27
  • 34
  • Yeah, I noticed this function in previous answers to similar question. I am concerning this question, since the manualintegrate function fails to integrate many trig functions due to this kind of limitations. – Kattern Jun 02 '15 at 02:09