python complex number in polar form

Python – Convert Complex Number to Polar Form

In this tutorial, we will look at how to convert a complex number in Python to its polar form with the help of some examples. In the polar form, a complex number is represented using its magnitude (or absolute value) and its phase angle.

How to get the polar coordinates of a complex number in Python?

python complex number in polar form

To get the polar form of a complex number in Python, use the numpy.abs() function to get its magnitude and the numpy.angle() function to get its phase angle.

Let’s write a custom function in Python, to return the polar form for a complex number as the (magnitude, phase angle) tuple.

import numpy as np

# function to get polar form of complex number
def get_polar_form(z):
    # magnitude
    mag = np.abs(z)
    # phase angle in radians
    phase = np.angle(z)
    return (mag, phase)

Here, we use the numpy.abs() function to calculate the magnitude (or the absolute value) of the complex number and the numpy.angle() function to get its phase angle.

Examples

Let’s now look at examples of using the above function on complex numbers.

import numpy as np

# function to get polar form of complex number
def get_polar_form(z):
    # magnitude
    mag = np.abs(z)
    # phase angle in radians
    phase = np.angle(z)
    return (mag, phase)

# get the polar form of complex number
get_polar_form(3+4j)

Output:

(5.0, 0.9272952180016122)

Here, we used the get_polar_form() custom function to get the polar form for the complex number 3 + 4j. You can see that its magnitude is 5 units and its phase angle is approx. 0.927 radians.

Note that the numpy.angle() function returns the phase angle in radians by default.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

To get the phase angle in degrees, pass deg=True as an argument to the numpy.angle() function.

# function to get polar form of complex number
def get_polar_form(z):
    # magnitude
    mag = np.abs(z)
    # phase angle in degrees
    phase = np.angle(z, deg=True)
    return (mag, phase)

# get the polar form of complex number
get_polar_form(3+4j)

Output:

(5.0, 53.13010235415598)

We get the complex number in polar form with the angle in degrees.

Let’s apply the above function to some other values – a real number, a complex number with non-zero real and imaginary parts, and an imaginary number.

# get the polar form of complex number
print(get_polar_form(14))
print(get_polar_form(1+1j))
print(get_polar_form(-5j))

Output:

(14, 0.0)
(1.4142135623730951, 45.0)
(5.0, -90.0)

We get the polar form for each of the above inputs. You can see that for the real number 14, the phase angle is 0 whereas for the imaginary input -5j, the phase angle is -90 degrees.

Summary

In this tutorial, we looked at how we can use a combination of the numpy.abs() and numpy.angle() functions to construct the polar form of a complex number in Python.

You might also be interested in –

Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top