Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Numpy - Complex Numbers - Is there a function for Polar to Rectangular conversion?

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)] 
like image 329
atomh33ls Avatar asked May 08 '13 15:05

atomh33ls


People also ask

How do you convert from complex to rectangular polar?

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.

Does Numpy work with complex numbers?

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.

How do you convert complex to polar in Python?

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.

How do you deal with complex numbers in Python?

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().


1 Answers

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).

like image 181
tom10 Avatar answered Sep 24 '22 08:09

tom10