simplify works on expression but not on its negative

38 views
Skip to first unread message

Greg Grunberg

unread,
Mar 13, 2016, 1:14:16 PM3/13/16
to sympy
I'm new to SymPy and this group.  My apologies if I'm submitting this to the wrong group.  Please feel free to indicate a more appropriate alternate group, if there is such.

I ran into a behavior of the simplify function which strikes me as strange.  Notice that in[4] and in[5] below are identical, except that the expression to be simplified in the latter is the negative of the former.  out[4] is what I expected, but out[5] is not.  Shouldn't out[5] be the negative of out[4]?  Can anyone explain?  Note that out[5] is not mathematically incorrect but just not what one would expect.


Python 3.5.1 |Anaconda 2.5.0 (64-bit)| (default, Jan 29 2016, 15:01:46) [MSC v.1900 64 bit (AMD64)]
Type "copyright", "credits" or "license" for more information.

IPython 4.0.3 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: from sympy import *

In [2]: init_session()
IPython console for SymPy 1.0 (Python 3.5.1-64-bit) (ground types: python)

These commands were executed:
>>> from __future__ import division
>>> from sympy import *
>>> x, y, z, t = symbols('x y z t')
>>> k, m, n = symbols('k m n', integer=True)
>>> f, g, h = symbols('f g h', cls=Function)
>>> init_printing()

Documentation can be found at http://docs.sympy.org/1.0/

In [3]: theta = symbols('theta', real=True)

In [4]: simplify(+I/2*exp(I*theta) - I/2*exp(-I*theta))
Out[4]: -sin(theta)

In [5]: simplify(-I/2*exp(I*theta) + I/2*exp(-I*theta))
Out[5]:
  /   2*I*theta    \  -I*theta
I*\- e          + 1/*e
------------------------------
              2


Francesco Bonazzi

unread,
Mar 14, 2016, 4:10:41 AM3/14/16
to sympy
A related issue:

In [6]: m = Matrix([[0, theta], [-theta, 0]])

In [7]: exp(m)
Out[7]:
[   I*theta    -I*theta         I*theta      -I*theta]
[  e          e              I*e          I*e        ]
[  -------- + ---------    - ---------- + -----------]
[     2           2              2             2     ]
[                                                    ]
[   I*theta      -I*theta      I*theta    -I*theta   ]
[I*e          I*e             e          e           ]
[---------- - -----------     -------- + ---------   ]
[    2             2             2           2       ]

In [8]: simplify(exp(m))
Out[8]:
[               /   2*I*theta    \  -I*theta]
[             I*\- e          + 1/*e        ]
[cos(theta)   ------------------------------]
[                           2               ]
[                                           ]
[-sin(theta)            cos(theta)          ]


Aaron Meurer

unread,
Mar 14, 2016, 1:49:18 PM3/14/16
to sy...@googlegroups.com
Definitely a bug. A workaround is to use (-I/2*exp(I*theta) +
I/2*exp(-I*theta)).rewrite(sin), which will force the exponentials to
be rewritten in terms of sin and cos.

Aaron Meurer
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sympy+un...@googlegroups.com.
> To post to this group, send email to sy...@googlegroups.com.
> Visit this group at https://groups.google.com/group/sympy.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/7b314244-e00e-4f51-992c-e253edb07127%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages