Search code examples
pythonsympysimplification

Sympy absolute value of complex exponential


When working with complex numbers in polar form, I've experienced a strange behavior. For example, doing

from sympy import *
simplify(Abs(exp(I)))

I would expect the result 1 because the absolute value of a complex exponential should always be one if the exponent is only imaginary. However, sympy gives as answer

Abs(exp(I))

Doing the alternative

phi=symbols('phi', real=True)
y=exp(I*phi)
sqrt(y*conj(y))

gives the expected result but is less clear than abs in my opinion. Did I miss some constraint that prevents sympy from performing this simplification when just using abs?


Solution

  • simplify could definitely be smarter about this.

    In general, to simplify things using complex numbers, use expand_complex, which tries to rewrite the expression as a + b*I, where a, and b are real. This works for me.

    In [17]: (abs(exp(I))).expand(complex=True)
    Out[17]:
       ___________________
      ╱    2         2
    ╲╱  cos (1) + sin (1)
    
    In [18]: simplify(abs(exp(I)).expand(complex=True))
    Out[18]: 1