May 9, 2024

Complex Numbers in Python – Define, Perform Basic Operations, and Convert Polar Form to Rectangular and Vice-Versa


In this Python and complex number tutorial, we explain how to

  1. Define complex numbers in Python by using several approaches
  2. Perform basic operations on complex numbers in Python
  3. Compute phase and magnitude of complex numbers in Python
  4. Convert complex numbers from polar to rectangular forms in Python

The YouTube tutorial on complex numbers in Python is given below.

Define Complex Numbers and Perform Operations on Complex Numbers in Python

The Python script given below presents two approaches for defining complex numbers.

import numpy as np
import cmath 

z=3+2j
print(z)

z2=complex(3,2)
print(z2)
print(z==z2)

First, we import NumPy and cmath libraries. The cmath library implements some of the functions necessary to manipulate with complex numbers. Also, to define the complex numbers in Python, we will be using the built-in Python functions.

The first approach for defining the complex number is to directly specify the complex number z=3+2j. It should be kept in mind that Python uses j to denote the imaginary unit. Here, when writing the imaginary part, there should not be space or an operator between the imaginary part and the imaginary unit. That is, just write 2j. Then, we can also use the built-in Python function “complex()” to define complex numbers in Python. The first argument of the complex() Python function is the real part and the second argument is the imaginary part of the complex number. In the last code line, we compare the two numbers in order to show that the two ways of defining the complex number will produce identical results.

We can print the real and imaginary parts of complex numbers in Python like this

z = -1+2j
# print the real and imaginary parts
print(z.real)
print(z.imag)

The conjugate of the complex number can be computed like this in Python

# conjugate 
z = -1+2j
z2=z.conjugate()
print(z2)

We can perform addition, multiplication, and division of complex numbers in Python in this way

#basic operations 
z2=complex(2,3)
print(z3=2+3j)
#addition
z4=z2+z3
print(z4)
#multiplication 
z5=z2*z3
print(z5)
# we can divide complex numbers like this 
z6=z4/z5
print(z6)
# we can compute expressions like this
s=((2+1j).conjugate())*1j
print(s)

The exponents of complex numbers can be computed like this in Python

# compute exponents of complex numbers
print((2+1j)**3)
print((2j)**3)

Compute the Phase, Magnitude, Polar, and Rectangular Forms of Complex Numbers in Python

Let z=a+bj be a complex number. The magnitude or the radius of the complex number is defined by |z|=\sqrt{a^{2}+b^{2}}. To calculate the magnitude or the radius of the complex number, we can use the Python function “abs()”. In the case of a complex number, this function will return the magnitude of the complex number

# calculate the magnitude or the modulus of the complex number
z=3+4j
print(abs(z))

For a complex number

(1)   \begin{align*}z=a+bj=|z|e^{\phi j}\end{align*}

The phase \phi is computed by using the “arctan2()” function. The first argument of arctan2 is the imaginary part b and the second argument is the real part a. We have \phi =arctan2(b,j)

To compute the phase or the argument of the complex number in Python, we use the cmath function phase():

# compute the phase of a complex number 
z=1+1j
# the results is returned in radians 
print(cmath.phase(z))
# double check 
print(np.arctan2(1,1))
# print the radian value 
print(np.pi/4)

In the script presented above, we can use the NumPy function “arctan2(b,a)” to verify that the phase is computed properly.

The following Python script will transform the rectangular form of the complex number z=a+bj into the polar form of the complex number z=|z|e^{\phi j}, where |z| is the magnitude and \phi is the argument. For that purpose, we use the cmath function polar():

# cmath.polar(z)
# is used to return the representation of the complex number 
# z in polar coordinates. That is, this function returns (r, phi) 
# where r is the modulus of x and phi is the phase of x. 
# That is, the function polar(x) is equivalent to (abs(x), phase(x)).
z=1+2j
print(z)
z2=cmath.polar(z)
print(z2)
# you will see that the z2 is actually a tuple
print(type(z2))
print(abs(z))
print(cmath.phase(z))

The function polar() returns a tuple. The first entry of the tuple is the magnitude |z| and the second entry of the tuple is the phase \phi.

Finally, the Python script given below explains how to construct a rectangular form of the complex number by using the magnitude and phase information.


# cmath.rect()
# Define the complex number x by using modulus and phase

# define the complex number by using modulus and phase 
z=1+2j
# extract the modulus and the phase
(r1,phi1)=cmath.polar(z)
# define a new complex number by using the modulus and phase
z3=cmath.rect(r1, phi1)