Is there a built-in Numpy function to convert a complex number in polar form, a magnitude and an angle (degrees) to one in real and imaginary components?
Clearly I could write my own but it seems like the type of thing for which there is an optimised version included in some module?
More specifically, I have an array of magnitudes and an array of angles:
>>> a array([1, 1, 1, 1, 1]) >>> b array([120, 121, 120, 120, 121])
And what I would like is:
>>> c [(-0.5+0.8660254038j),(-0.515038074+0.8571673007j),(-0.5+0.8660254038j),(-0.5+0.8660254038j),(-0.515038074+0.8571673007j)]
Converting from Polar Form to Rectangular Form To convert from polar to rectangular, find the real component by multiplying the polar magnitude by the cosine of the angle, and the imaginary component by multiplying the polar magnitude by the sine of the angle.
Python offers the built-in math package for basic processing of complex numbers. As an alternative, we use here the external package numpy , which is used later for various purposes. A complex number c=a+ib can be plotted as a point (a,b) in the Cartesian coordinate system.
The cmath. polar() method converts a complex number to polar coordinates. It returns a tuple of modulus and phase. In polar coordinates, a complex number is defined by modulus r and phase angle phi.
Converting real numbers to complex number An complex number is represented by “ x + yi “. Python converts the real numbers x and y into complex using the function complex(x,y). The real part can be accessed using the function real() and imaginary part can be represented by imag().
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).
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