Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 351
jan Avatar asked Jun 05 '14 13:06

jan


People also ask

Does SymPy have e?

Note that by default in SymPy the base of the natural logarithm is E (capital E ). That is, exp(x) is the same as E**x .

How do you evaluate a SymPy expression?

To evaluate a numerical expression into a floating point number, use evalf . SymPy can evaluate floating point expressions to arbitrary precision. By default, 15 digits of precision are used, but you can pass any number as the argument to evalf .

How do you write Ln in SymPy?

Note: In SymPy, as in Python and most programming languages, log is the natural logarithm, also known as ln. SymPy automatically provides an alias ln = log in case you forget this.

How do you write the square root in SymPy?

With the help of sympy. sqrt() method, we can find the square root of any number by using sympy. sqrt() method. Return : Return square root of any number.


1 Answers

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
like image 59
asmeurer Avatar answered Sep 27 '22 20:09

asmeurer