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

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-09 03:20:42

问题


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


回答1:


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



回答2:


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



回答3:


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



来源:https://stackoverflow.com/questions/30541734/how-to-rewrite-sinx2-to-cos2x-form-in-sympy

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!