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?
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 .
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 .
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With