pythonsympysymbolic-mathsimplify

sympy simplify expression with power-reduction formula


I have the following sympy expression

>>> a

         ⎛2⋅π⋅(x - y)⎞
2 - 2⋅cos ───────────
         ⎝     P     ⎠

which I would like to simplify to


     2 ⎛π⋅(x - y)⎞
4⋅sin   ─────────
       ⎝    P    ⎠

by using the (inverse) of the first power-reduction formula listed in Wikipedia.

The second form involves only 6 operations, versus the 7 operations of the first form, but when I use the fu function from sympy.simplify trying to minimize the number of operations I get:

>>> fu(a, measure=lambda x: x.count_ops())


         ⎛2⋅π⋅(x - y)⎞
2 - 2⋅cos ───────────
         ⎝     P     ⎠

or at best

>>> fu(sympy.expand_trig(a), measure=lambda x: x.count_ops())


         2⎛π⋅(x - y)⎞
4 - 4⋅cos  ─────────
          ⎝    P    ⎠

which still involves 7 operations.

Is there anyway to convince sympy to output the sin**2 form?


Solution

  • You can have finer control by selecting the desired transformation from fu

    >>> from sympy import S
    >>> from sympy.simplify.fu import TR11,TR6
    >>> TR6(TR11(S('2 - 2*cos(2*pi*(x - y)/p)')))
    4*sin(pi*(x - y)/p)**2