I got it!
n = sympyexpression_whichevaluatestoacomplexnumber
expressionangle = sympy.arg( n )
Also a slight modification of Tom's suggestion works:
expressionangle2 = sympy.log( n ).as_real_imag()[1]
Thanks for help!
Answer from D A on Stack OverflowVideos
Just to explain it a little bit more:
import numpy as np
complex_num = -64.97135784885555 + 30.016069249920577j
np.angle(complex_num)
#2.7088133115162574 #radian
np.angle(complex_num,deg=True)
#155.20357023873785 #degrees
np.arctan(30.016069249920577/-64.97135784885555)
#-0.43277934207353586 #radian
Convert radian to degree by: np.rad2deg()
np.rad2deg(-0.43277934207353586)
#-24.796429761262143 #degrees
Now, if you want to represent this is first coordinate add 180
-24.796429761262143 +180
#155.20357023873785
This is exactly similar to np.angle(complex_num,deg=True)
Hope this clarifies.
Links:
https://numpy.org/doc/stable/reference/generated/numpy.angle.html
https://numpy.org/doc/stable/reference/generated/numpy.arctan.html
You are correct in you assumption. Both values represent the same angle, just from a different "perspective".
numpy.angle returns values in the range [-pi, pi], while numpy.arctan returns values in the range [-pi/2, pi/2], which is why the returned values differ.
There isn't a function to do exactly what you want, but there is angle, which does the hardest part. So, for example, one could define two functions:
def P2R(radii, angles):
return radii * exp(1j*angles)
def R2P(x):
return abs(x), angle(x)
These functions are using radians for input and output, and for degrees, one would need to do the conversion to radians in both functions.
In the numpy reference there's a section on handling complex numbers, and this is where the function you're looking for would be listed (so since they're not there, I don't think they exist within numpy).
There's an error in the previous answer that uses numpy.vectorize - cmath.rect is not a module that can be imported. Numpy also provides the deg2rad function that provides a cleaner piece of code for the angle conversion. Another version of that code could be:
import numpy as np
from cmath import rect
nprect = np.vectorize(rect)
c = nprect(a, np.deg2rad(b))
The code uses numpy's vectorize function to return a numpy style version of the standard library's cmath.rect function that can be applied element wise across numpy arrays.
Degrees make little sense in complex numbers. But if you must use them, just use the same math formula as for real numbers:
cmath.asin(1.47) * 180 / math.pi
You get the result
(90+53.576889894078214j)
Note the 90 degrees in the real part.
The usefulness of this depends on the context. For example, when taking a complex logarithm, only the imaginary part of the result is an angle and thus can be expressed in degrees. The real part is the logarithm of the modulus of the parameter and has nothing to do with an angle. In that case, use the above conversion only on the imaginary part. In your arcsine example, usually only the real part is considered an angle, which is why you got the simple 90 for the real part but a mess for the imaginary part.
Let us know just what you are doing with this and we can help you determine the best way to use degrees.
To use math.degrees(), you need to get the real part of the complex number first.
import cmath
import math
rad = cmath.asin( 1.47 )
math.degrees( rad.real )