
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Square Root of Complex Numbers in Python
Complex numbers are numbers that have both real and imaginary components in the structure, a+bi. You can find the square root of complex numbers in Python using the cmath module. This module in Python is exclusively used to deal with complex numbers.
Square Root of Complex Numbers Using cmath.sqrt()
The cmath.sqrt() function is a part of Python's cmath module, that takes a number which is an integer or float (real or complex) and returns the complex square root of x.
Below are some examples of scenarios where the function can be used -
Example - Basic Complex Number
In the below example, we use the cmath.sqrt() function to find the square root of a basic complex number whose real number and imaginary number's are positive -
import cmath # Standard complex number with real and imaginary parts z = complex(3, 4) # 3+4j sqrt_z = cmath.sqrt(z) print(f"Square root of {z} = {sqrt_z}")
The output returned by the above example is as follows -
Square root of (3+4j) = (2+1j)
Example - Negative Real Number
In this case, we consider a complex number that has a negative real number only i.e., the complex number is zero. The code below uses cmath.sqrt() to compute the above description -
import cmath # Negative real number z = complex(-25, 0) # -16+0j sqrt_z = cmath.sqrt(z) print(f"Square root of {z} = {sqrt_z}")
The code above returns a pure imaginary number as shown below -
Square root of (-25+0j) = 0j+5j
Example - Pure Imaginary Numbers
In the below example, we use cmath.sqrt() function to calculate the square root of a complex number, where it is completely imaginary, i.e., the real number is zero -
import cmath # Pure imaginary number z = complex(0, 9) # 0+9j sqrt_z = cmath.sqrt(z) print(f"Square root of {z} = {sqrt_z}")
The output returned by the above example is as follows -
Square root of 0j+9j = (2.1213203435596424+2.1213203435596424j)
Complex Numbers in Different Quadrants
As you know, the coordinate plane is divided into four quadrants, where -
- Quadrant I: Both real and imaginary numbers are positive.
- Quadrant II: Real number is negative and imaginary number is positive.
- Quadrant III: Both real and imaginary numbers are negative.
- Quadrant IV: Real number is positive and imaginary number is negative.
Unlike the math.sqrt() function in the cmath.sqrt() function in the cmath module will allow you to find the square root of a negative number also. The example program below illustrates the use of the cmath.sqrt() function my passing complex numbers from the 2nd and 3rd quadrant -
import cmath # Second quadrant (negative real, positive imaginary) z = complex(-5, 12) sqrt_z = cmath.sqrt(z) print(f"Square root of {z} = {sqrt_z}") # Third quadrant (negative real, negative imaginary) z = complex(-3, -4) sqrt_z = cmath.sqrt(z) print(f"Square root of {z} = {sqrt_z}")
The output returned by the above example is as follows -
Square root of (-5+12j) = (2+3j) Square root of (-3-4j) = (1-2j)
Example - Zero and Small Values
The example below calculates the square of complex numbers with real and complex number being zero and nearer to zero's -
import cmath # Zero z = complex(0, 0) sqrt_z = cmath.sqrt(z) print(f"Square root of {z} = {sqrt_z}") # Very small values z = complex(1e-10, 1e-10) sqrt_z = cmath.sqrt(z) print(f"Square root of {z} = {sqrt_z}")
The output returned by the above example is as follows -
Square root of 0j = 0j Square root of (1e-10+1e-10j) = (1.09868411346781e-05+4.5508986056222734e-06j)