Complex Numbers in Python
Complex Numbers in Python
Not only real numbers, Python can also handle complex numbers and its associated functions using the file
“cmath”. Complex numbers have their uses in many applications related to mathematics and python provides
useful tools to handle and manipulate them.
Converting from polar to rectangular form and vice versa
Conversion to polar is done using polar(), which returns a pair(r,ph) denoting
the modulus r and phase angle ph. modulus can be displayed using abs() and
phase using phase().
A complex number converts into rectangular coordinates by using rect(r, ph),
where r is modulus and ph is phase angle. It returns a value numerically equal
to r * (math.cos(ph) + math.sin(ph)*1j)
# Python code to demonstrate the working of
# polar() and rect()
# importing "cmath" for complex number operations
import cmath
import math
# Initializing real numbers
x = 1.0
y = 1.0
# converting x and y into complex number
z = complex(x,y);
# converting complex number into polar using polar()
w = cmath.polar(z)
# printing modulus and argument of polar complex number
print ("The modulus and argument of polar complex number is : ",end="")
print (w)
# converting complex number into rectangular using rect()
w = cmath.rect(1.4142135623730951, 0.7853981633974483)
# printing rectangular form of complex number
print ("The rectangular form of complex number is : ",end="")
print (w)
Output: