9

I have an expression: sin(x)+sin(y)

There is a well-known trig identity to express this as the product of sin and cos.

Is there a way to get sympy to apply this identity?

simplify and trigsimp do nothing.

Nick Meyer
  • 1,771
  • 1
  • 17
  • 29
GeorgeSalt
  • 197
  • 1
  • 11
  • Interestingly, `trigsimp` seems to work in the other direction, that is, `trigsimp(2*sin( (x + y)/2 ) * cos ( (x - y)/2 ))` gives you `sin(x) + sin(y)`. Perhaps sympy is interpreting `sin(x) + sin(y)` as the "more simplified" form. Do you know of other means of performing trig manipulations in sympy? – Nick Meyer Aug 18 '14 at 02:54
  • `expand_trig(sin(x) + sin(y))` also seems to be doing nothing – Nick Meyer Aug 18 '14 at 02:57

2 Answers2

12

trigsimp, as Aristocrates points out, does the reverse, because sin(x) + sin(y) is simpler than 2*sin((x + y)/2)*cos((x - y)/2).

trigsimp internally uses an algorithm based on a paper by Fu, et. al., which does pattern matching on various trigonometric identities. If you look at the source code, all the identities are written out in individual functions (the functions are named after the sections in Fu's paper).

Looking at the list of simplifications at the top of the file, the one you want is probably

TR9 - contract sums of sin-cos to products

Testing it out, it looks like it works

In [1]: from sympy.simplify.fu import TR9

In [2]: TR9(sin(x) + sin(y))
Out[2]:
     ⎛x   y⎞    ⎛x   y⎞
2⋅sin⎜─ + ─⎟⋅cos⎜─ - ─⎟
     ⎝2   2⎠    ⎝2   2⎠

We would eventually like to factor these out into more user-friendly functions, but for now, the fu.py file is pretty well documented, even if all the function names are not particularly memorable.

asmeurer
  • 86,894
  • 26
  • 169
  • 240
  • 3
    Yes, this is what I was looking for. I didn't know about fu.py and I suspect that's true of lots of sympy users. – GeorgeSalt Aug 22 '14 at 02:41
  • There is also a higher-level interface from the `fu` function. You can provide a "measure" function, which lets you redefine what kinds of expressions are "simpler". – asmeurer Aug 23 '14 at 18:57
1

There does not seem to be a single function defined. You need to use the individual Fu functions as @asmeurer said. What I can contribute is a pointer to the official documentation explaining this: https://docs.sympy.org/dev/modules/simplify/fu.html

s5s
  • 11,159
  • 21
  • 74
  • 121