Search code examples
pythonsympy

Why does sympy avoid simplifying a simple looking expression?


When performing equality testing, I realized that sympy appears to have trouble simplifying the following expression:

e^{2i\lambda t -2ie^{i\lambda t}sin(\lambda t) - 1.

I believe this should equal zero, which becomes clear when you rewrite sin into complex exponentials. Is there a situation where this expression does not simplify to zero, or is sympy not able to perform the simplification?

Here is a minimum working example:

import sympy as sp
t,lambdaa = sp.symbols('t lambda')
(sp.exp(2*sp.I*t*lambdaa)-2*sp.I*sp.exp(sp.I*t*lambdaa)*sp.sin(t*lambdaa)-1).simplify()

Solution

  • You can tell sympy to rewrite an expression in terms of other functions. Here, I ask for the expression to be rewritten in terms of exponential, then I simplify it:

    import sympy as sp
    t,lambdaa = sp.symbols('t lambda')
    e = sp.exp(2*sp.I*t*lambdaa)-2*sp.I*sp.exp(sp.I*t*lambdaa)*sp.sin(t*lambdaa)-1
    e.rewrite(sp.exp).simplify()
    # out: 0