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 real numbers to complex number
An complex number is represented by “ x + yi “. Python converts the real numbers x and y into complex using the function complex(x,y). The real part can be accessed using the function real() and imaginary part can be represented by imag().
python - Sample - python code :
python tutorial - Output :
The real part of complex number is : 5.0 The imaginary part of complex number is : 3.0
Phase of complex number
Geometrically, the phase of a complex number is the angle between the positive real axis and the vector representing complex number. This is also known as argument of complex number. Phase is returned using phase(), which takes complex number as argument. The range of phase lies from -pi to +pi. i.e from -3.14 to +3.14.
python - Sample - python code :
python tutorial - Output :
The phase of complex number is : 3.141592653589793
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 - Sample - python code :
python tutorial - Output :
The modulus and argument of polar complex number is : (1.4142135623730951, 0.7853981633974483) The rectangular form of complex number is : (1.0000000000000002+1j)