Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python program to convert complex number to polar coordinate values
Converting a complex number to polar coordinates involves finding the magnitude (radius) and phase angle. For a complex number x + yj, the radius is sqrt(x² + y²) and the angle is measured counter-clockwise from the positive x-axis.
Python's cmath module provides built-in functions: abs() for magnitude and phase() for the angle in radians.
Syntax
import cmath magnitude = abs(complex_number) angle = cmath.phase(complex_number)
Example
Let's convert the complex number 2+5j to polar coordinates ?
import cmath
def convert_to_polar(c):
return (abs(c), cmath.phase(c))
c = 2+5j
polar_coords = convert_to_polar(c)
print(f"Complex number: {c}")
print(f"Polar coordinates: {polar_coords}")
print(f"Magnitude: {polar_coords[0]:.4f}")
print(f"Angle (radians): {polar_coords[1]:.4f}")
Complex number: (2+5j) Polar coordinates: (5.385164807134504, 1.1902899496825317) Magnitude: 5.3852 Angle (radians): 1.1903
Converting Multiple Complex Numbers
Here's how to convert multiple complex numbers at once ?
import cmath
def convert_to_polar(c):
return (abs(c), cmath.phase(c))
complex_numbers = [2+5j, 3+4j, 1-2j, -2+3j]
for c in complex_numbers:
magnitude, angle = convert_to_polar(c)
print(f"{c} ? (r={magnitude:.3f}, ?={angle:.3f})")
(2+5j) ? (r=5.385, ?=1.190) (3+4j) ? (r=5.000, ?=0.927) (1-2j) ? (r=2.236, ?=-1.107) (-2+3j) ? (r=3.606, ?=2.159)
Converting Angle to Degrees
To get the angle in degrees instead of radians ?
import cmath
import math
def convert_to_polar_degrees(c):
magnitude = abs(c)
angle_radians = cmath.phase(c)
angle_degrees = math.degrees(angle_radians)
return (magnitude, angle_degrees)
c = 2+5j
magnitude, angle = convert_to_polar_degrees(c)
print(f"Complex number: {c}")
print(f"Magnitude: {magnitude:.4f}")
print(f"Angle: {angle:.2f}°")
Complex number: (2+5j) Magnitude: 5.3852 Angle: 68.20°
Key Points
abs()returns the magnitude (distance from origin)cmath.phase()returns the angle in radians (-? to ?)Use
math.degrees()to convert radians to degreesNegative angles indicate clockwise rotation from positive x-axis
Conclusion
Python's cmath module makes converting complex numbers to polar coordinates straightforward using abs() and phase(). The magnitude represents distance from origin while the phase angle shows direction in the complex plane.
