Tuesday, June 22, 2021

Python for Beginners: Complex numbers in Python

While working on data science, machine learning or scientific calculations, we often need to perform calculations on numeric data types including  complex numbers. In this article, we will learn how to define and use complex numbers in python. 

What is a complex number ?

A complex number is a number that can be written in the form of (a+b j) where a and b are real numbers. Here j is an imaginary number and is defined to be the square root of -1. Complex numbers occur in pairs and are mostly used in calculations involving square roots of negative numbers.

How to define complex numbers in Python?

We can define complex numbers in python by simply declaring a variable and assigning an expression of the form (a+bj) .Here “a” and “b” should be a numeric literal and “j” can be any alphabet character. We can also check the data type of the defined variable using type() function as follows.

myNum= 3+2j
print("The Number is:")
print(myNum)
print("Data type of Number is:")
print(type(myNum))

Output:

The Number is:
(3+2j)
Data type of Number is:
<class 'complex'>

We can also define a complex number using complex() function. The complex function takes a compulsory input as first parameter which denotes the real part of the complex number and an optional input as second parameter which denotes the imaginary part of the complex number. We can define a complex number using the complex() function as follows.

myNum= complex(3,2)
print("The Number is:")
print(myNum)
print("Data type of Number is:")
print(type(myNum))

Output:

The Number is:
(3+2j)
Data type of Number is:
<class 'complex'>

Extracting real and imaginary part of a complex number

In a complex number (a+b j), “a” is called the real part and “b” is called the imaginary part. We can extract real part of the complex number using an attribute named “real” which contains value “a” and imaginary part using the attribute “imag” which contains the value “b” as follows.

myNum= complex(3,2)
print("The Complex Number is:")
print(myNum)
print("Real part of the complex Number is:")
print(myNum.real)
print("Imaginary part of the complex Number is:")
print(myNum.imag)

Output:


The Complex Number is:
(3+2j)
Real part of the complex Number is:
3.0
Imaginary part of the complex Number is:
2.0

Conjugate of a complex number in Python

For a complex number (a+ b j), its conjugate is defined as the complex number (a- b j).We can obtain the conjugate of any complex number in python using the conjugate() method. The conjugate() method when invoked on a complex number, returns the complex conjugate of the number. This can be done as follows.

myNum= complex(3,2)
print("The Complex Number is:")
print(myNum)
print("Conjugate of the complex Number is:")
print(myNum.conjugate())

Output:

The Complex Number is:
(3+2j)
Conjugate of the complex Number is:
(3-2j)

Magnitude of the complex number

The magnitude of a complex number (a+b j) is the distance of the point (a,b) from (0,0). It is the length of the vector which represents the complex number. The magnitude of a complex number can be calculated as follows in python.

import math
def magnitude(num):
    x=num.real
    y=num.imag
    mag=x*x+y*y
    return math.sqrt(mag)
myNum= complex(3,2)
print("The Complex Number is:")
print(myNum)
print("Magnitude of the complex Number is:")
print(magnitude(myNum))

Output:

The Complex Number is:
(3+2j)
Magnitude of the complex Number is:
3.605551275463989

Conclusion

In this article, we have studied the basics of complex numbers and its attributes in python. We have also derived the real part, imaginary part , conjugate and magnitude of a complex number. We can also write the programs used in this article with exception handling using python try except to make the programs more robust and handle errors in a systematic way. Stay tuned for more informative articles.

The post Complex numbers in Python appeared first on PythonForBeginners.com.



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...