Friday, December 27, 2024
Google search engine
HomeLanguagesPython Program to convert complex numbers to Polar coordinates

Python Program to convert complex numbers to Polar coordinates

Before starting with the program, let’s see the basics of Polar Coordinates and then use Python’s cmath and abs module to convert it. Polar coordinates are just a different way of representing Cartesian coordinates or Complex Numbers. A complex number  z is defined as :

z=x+yj

It is completely determined by its real part x and imaginary part y. Here, j is the imaginary unit.

The polar coordinates (r , φ) is completely determined by modulus r and phase angle φ.

Where,

 r: Distance from z to origin, i.e., 
 r = \sqrt{x^{2}+y^{2}}
 φ: Counterclockwise angle measured from the positive x-axis to the line segment that joins z to the origin.

The conversion of complex numbers to polar coordinates is explained below with examples.

Using cmath module

Python’s cmath module provides access to the mathematical functions for complex numbers. It contains several functions that are used for converting coordinates from one domain to another. 

Out of them, some are explained as:

1. cmath.polar(x):

Return the representation of x in polar coordinates. cmath.polar() function is used to convert a complex number to polar coordinates. 

Python3




# Python code to implement
# the polar()function
 
# importing "cmath"
# for mathematical operations
import cmath
 
# using cmath.polar() method
num = cmath.polar(1)
print(num)


Output

(1.0, 0.0)

Time Complexity: O(1)
Auxiliary space: O(1)

2. cmath.phase (z): This method returns the phase of the complex number z(also known as the argument of z).

Python3




import cmath
 
 
x = -1.0
y = 0.0
z = complex(x, y)
 
# printing phase of a complex number using phase()
print("The phase of complex number is : ", end="")
print(cmath.phase(z))


Output

The phase of complex number is : 3.141592653589793

Using abs()

abs(): This method returns the modulus (absolute value) of the complex number z.

Python3




num1 = 3 + 4j
print('Absolute value of 3 + 4j is:', abs(num1))


Output

Absolute value of 3 + 4j is: 5.0

You are given a complex number z and your task is to convert it to polar coordinates. Let us consider a complex number as 1+5j, and we need to convert it to Polar coordinates. 

Python3




import cmath
 
 
c = complex(1+5j)
print(abs(c))


Output

5.0990195135927845

Example in python:

Approach:

The abs function returns the modulus or magnitude of the complex number. The math.atan2 function returns the angle in radians of the complex number. The angle is returned in the range -pi to pi, and is measured counterclockwise from the positive real axis. The polar coordinates are returned as a tuple of the form (r, theta), where r is the magnitude and theta is the angle in radians.

Python3




import math
 
 
def complex_to_polar(z):
    r = abs(z)
    theta = math.atan2(z.imag, z.real)
    return (r, theta)
 
 
z = 3 + 4j
 
polar_z = complex_to_polar(z)
print(polar_z)  # Output: (5.0, 0.9272952180016122)


Output

(5.0, 0.9272952180016122)

Time complexity: O(1)
Auxiliary Space: O(1)

Approach: Using numpy:

Algorithm:

  1. Import the numpy library.
  2. Define a function named “complex_to_polar” that takes a complex number as input.
  3. Calculate the magnitude of the complex number using np.abs() function.
  4. Calculate the phase angle of the complex number using np.angle() function.
  5. Return a tuple of magnitude and phase angle.
  6. Define a complex number “z”.
  7. Call the “complex_to_polar” function with “z” as input and store the result in “polar_z”.
  8. Print the “polar_z” result.
     

Python3




import numpy as np
 
def complex_to_polar(z):
    r = np.abs(z)
    theta = np.angle(z)
    return (r, theta)
 
z = 3 + 4j
 
polar_z = complex_to_polar(z)
print(polar_z)
#This code is contributed by Jyothi pinjala


Output:
(5.0, 0.9272952180016122)

Time complexity:
The time complexity of this code is O(1), as it involves simple arithmetic operations and calling numpy functions that have constant time complexity.

Auxiliary Space:
The space complexity of this code is O(1), as it only involves a few variable assignments and does not require any additional memory allocation or data structures.
has context menu

RELATED ARTICLES

Most Popular

Recent Comments