Complex numbers in Python

Complex numbers are the numbers that contain a real part and a complex part, for example ‘2+3j’. We can use complex numbers in python using the ‘cmath’ module that contains mathematical functions for complex numbers. We can create a complex number by direct assignment or by using the complex() function. The complex numbers take as arguments two numbers, real and imaginary and assign it to the real and complex part of the complex number. Example:-

#Direct Assignment using real numbers
a=2+3j
print(a)

#Using the complex function
b=complex(2,3)
print(b)

Output:

2+3j
2+3j

Both methods give us the same complex number.

Attributes and functions for Python complex numbers

a = 2+3j
print('Real Part of the complex number = ', a.real)
print('Imaginary Part of the complex number = ', a.imag)
print('Complex conjugate of the complex number = ', a.conjugate())

Output:

Real Part of the complex number = 2
Imaginary Part of the complex number = 3
Complex conjugate of the complex number = 2-3j

We can also perform different mathematical operations on complex numbers like addition, subtraction, etc.

Note: We can not perform comparison operations like ‘<‘ or ‘>’ on complex numbers.

Phase of a complex number

Phase of a complex number is equal to the angle between the imaginary part ( represented by a vector) and the real part of the complex number. It can be calculated by using the ‘phase’ attribute of the ‘cmath’ module or using the ‘math.atan2()’ function from the ‘math’ module.
cmath.phase takes the complex number as the argument whereas the math.atan2 function takes the imaginary and the real part of the complex number as the arguments respectively. Both functions return the phase of the complex number in radians. Hence we can use the NumPy module to convert this into degrees.

import cmath, math, numpy

a=2+3j

phase = cmath.phase(a)
print('Phase of the complex number 2+3j is = ', phase)

#phase in degrees
print('Phase in Degrees = ', numpy.degrees(phase))

#using math.atan2() function
print('Phase using math.atan2() function = ', math.atan2(3, 2))

Output:

Phase of the complex number 2+3j is = 0.982793723247329
Phase in Degrees = 56.309932474020215
Phase using math.atan2() function = 0.982793723247329

Polar and rectangular form of a complex number in Python

We can convert a complex number into its polar coordinate using the polar() function. It takes the complex number as the argument and returns a pair of values (r, ph) where r is the modulus(abs(a)) and ph(phase(a)) is the phase angle for that complex number ‘a’.

import cmath, math, numpy 
a=2+3j 
phase = cmath.phase(a) 
print('Phase of the complex number 2+3j is = ', phase)

r = abs(a)
print('Modulus of the complex number 2+3j is = ', r)
  
# converting complex number into polar using polar() 
p = cmath.polar(a) 
print ("The polar coordinates of the complex number is : ", p)

Output:

Phase of the complex number 2+3j is = 0.982793723247329
Modulus of the complex number 2+3j is = 3.605551275463989
The polar coordinates of the complex number is : (3.605551275463989, 0.982793723247329)

We can convert a complex number into its rectangular coordinate using the rect() function. It takes the complex a pair of values (r, ph) where r is the modulus(abs(a)) and ph(phase(a)) is the phase angle for that complex number ‘a’ and returns the coordinates obtained by the formula:
r * (math.cos(ph) + math.sin(ph)*1j)

import cmath, math, numpy 

# converting complex number into rectangular using rect() 
r = cmath.rect(3.605551275463989,0.982793723247329) 
print ("The rectangular coordinates of the complex number is : ", r)

Output:

The rectangular form of complex number is : (2+3j)

Leave a Reply

Your email address will not be published.