Modulus of Complex Number in Python

One of Python’s built-in data type is Numbers which further can be divided into Reals and Complex. Complex Numbers being those which have form like a + bj where a, b are Real Numbers and j is a special imaginary value called iota(iota equals to √-1). For example 4 + 8j is a complex number as it have two real numbers 4, 8 and an imaginary part j.

How to calculate modulus of a Complex Number in simple maths?

Traditionally in simple mathematics, modulus of a complex number say a + bj is defined as square root of additions of squares of a,b.
Simpley Modulus = square root of a2 + b2 .
For example – Modulus of 4 + 5j Complex Number will be square root of 42 + 52.
Which will be square root of 41. So Modulus of 4 + 5j = √42.

Three Ways to Find Modulus of a number in Python

Simple Way using sqrt function

For this approach to find modulus of Complex Number. You just need to import sqrt function from Python’s math module.
Below is a code example showing How to find Modulus of Complex Number using sqrt function?

from math import sqrt
x = a + bj               # x is a Complex Number
y = sqrt(a**2 + b**2)    # y is Modulus of x

Note
This method is only useful when you know beforehand what’s real, imaginary parts of Complex Number.

Finding Modulus of Complex Number using abs() function in Python

Python’s built-in function abs() can also be used for finding modulus of Complex Number. Its one of simplest way to do this, I usually use this one.
Below is an example showing How to find Complex Number modulus using abs()?

x = a + bj     // A Complex Number in Python
y = abs(x)     // Returns Modulus of Complex Number x

My preferred approach to find Complex Number Modulus in Python

Finding Modulus of Complex Number using cmath.polar() function in Python

Python’s cmath module have a function called polar which when passed a Complex Number returns a tuple containing modulus, phase of Complex Number.
As polar returns modulus so with some additional code it can be taken out from tuple returned by cmath polar function.
Below is a code example showing How to find Complex Number Modulus suing cmath.polar() function?

from cmath import polar
x = a + bj          # Python Complex Number
y = polar(x)			  # Returns a tuple (modulus of x, phase of x)
modulus = y[0]      # Takes out modulus from tuple y and return it  	

Conclusion

Finding Modulus of a Complex Number in Python is super easy, you can use any one of three methods which I listed in this article. Personally I would usually use abs(complex number) as for this just one line of code is required.
Feel free to comment below if you want me to explain something or to answer questions of your about Complex Number Modulus finding in Python. (Or any question in general about Python)

If your not aware of What are Complex Numbers and related functions which Python supports? Then you can visit below links to learn more about these concepts.

Moreover if in case your interested and want to read more about different aspects of Python Programming Language then see – Computer Science Hub Python Programming Language.

Josh

Hi, I'm Josh a Computer Science graduate from California State University, Sacramento since coming out with my Master's from university. I've worked with multiple startups across US and in UK as well primarily as a Python Developer. Here on this website, I'm sharing my knowledge of Python. If you want to ask me anything about Python feel free to reach out, I would be happy to help you out.

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Posts