Python Program to convert complex numbers to Polar coordinates
Last Updated :
04 Apr, 2023
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 :

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
import cmath
num = cmath.polar( 1 )
print (num)
|
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)
print ( "The phase of complex number is : " , end = "")
print (cmath.phase(z))
|
OutputThe 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))
|
OutputAbsolute 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))
|
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)
Time complexity: O(1)
Auxiliary Space: O(1)
Approach: Using numpy:
Algorithm:
- Import the numpy library.
- Define a function named “complex_to_polar” that takes a complex number as input.
- Calculate the magnitude of the complex number using np.abs() function.
- Calculate the phase angle of the complex number using np.angle() function.
- Return a tuple of magnitude and phase angle.
- Define a complex number “z”.
- Call the “complex_to_polar” function with “z” as input and store the result in “polar_z”.
- 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)
|
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
Similar Reads
Convert Complex Number to String in Python
We are given complex numbers and we have to convert these complex numbers into strings and return the result in string form. In this article, we will see how we can convert complex numbers to strings in Python. Examples: Input : 3+4j <complex>Output : 3+4j <string> Explanation: We can se
3 min read
Python program to convert exponential to float
Given a number in exponential format, the task is to write a Python program to convert the number from exponential format to float. The exponential number is a way of representing a number. Examples: Input: 1.900000e+01 Output: 19.0 Input: 2.002000e+03 Output: 2002.0 Input: 1.101020e+05 Output: 1101
1 min read
Python program for multiplication and division of complex number
Given two complex numbers. The task is to multiply and divide them. Multiplication of complex number: In Python complex numbers can be multiplied using * operator Examples: Input: 2+3i, 4+5i Output: Multiplication is : (-7+22j) Input: 2+3i, 1+2i Output: Multiplication is : (-4+7j) C/C++ Code # Pytho
3 min read
Python program for addition and subtraction of complex numbers
Given two complex numbers z1 and z2. The task is to add and subtract the given complex numbers.Addition of complex number: In Python, complex numbers can be added using + operator.Examples: Input: 2+3i, 4+5i Output: Addition is : 6+8i Input: 2+3i, 1+2i Output: Addition is : 3+5i Example : C/C++ Code
2 min read
Finding Magnitude of a Complex Number in Python
Complex numbers are an essential part of mathematics and engineering, often encountered in various fields such as signal processing, control systems, and physics. The magnitude of a complex number is a measure of its distance from the origin in the complex plane. In Python, there are several ways to
3 min read
Python Program to Find Area and Circumference of Circle
Python provides a simple yet powerful way to calculate the area and circumference of a circle using various mathematical formulas. In this article, we will explore the main logic behind these calculations and demonstrate two commonly used methods to find the area and circumference of a circle in Pyt
3 min read
Python program to Return the real part of the complex argument
Given a complex number, the task is to write a Python Program to return the real part of the complex argument. What is a Complex number? Complex numbers are those numbers that are written in the format a+ib, where 'a' and 'b' are real numbers and 'i' is the imaginary part called "iota." (â-1) is the
2 min read
Solve Complex Equations in Python
Complex numbers seem scary, but Python can help you understand and solve equations with them. This article explores the concept of solving complex equations in Python and various approaches to solve complex equations. Solve Complex Equations in PythonBelow, are the approaches for solving complex equ
4 min read
Python Complex to Int
In Python, we can work by changing the data types of objects using type conversion or typecasting. Type conversion in Python is of two types - Implicit, where type conversion is automatically performed by Python, and explicit, where we make use of predefined functions to change data types. In this p
4 min read
Python program to represent floating number as hexadecimal by IEEE 754 standard
Prerequisite : IEEE Standard 754 Floating Point NumbersGiven a floating point number, the task is to find the hexadecimal representation for the number by IEEE 754 standard.The IEEE Standard for Floating-Point Arithmetic (IEEE 754) is a technical standard for floating-point computation which was est
3 min read