Python program to convert complex number to polar coordinate values

PythonServer Side ProgrammingProgramming

Suppose we have a complex number c, we have to convert it into polar coordinate (radius, angle). Complex number will be in form x + yj. The radius is the magnitude of the complex number which is square root of (x^2 + y^2). And the angle is the counter clockwise angle measured from positive x-axis to the line segment that joins x + yj to the origin. From the cmath library we can use the phase() function to calculate the angle. And abs() function on complex number will return the magnitude value.

So, if the input is like c = 2+5j, then the output will be (5.385164807134504, 1.1902899496825317)

To solve this, we will follow these steps −

  • return a pair with (|c| , phase(c) from cmath library)

Example

Let us see the following implementation to get better understanding

import cmath

def solve(c):
   return (abs(c), cmath.phase(c))

c = 2+5j
print(solve(c))

Input

2+5j

Output

(5.385164807134504, 1.1902899496825317)
raja
Updated on 12-Oct-2021 07:07:03

Advertisements