Complex Number

Yaser Rahmati | یاسر رحمتی

Creating a Complex Number:

You can create a complex number in Python by using the j suffix to represent the imaginary part. For example:

z = 3 + 2j  # This represents the complex number 3 + 2i

Accessing Real and Imaginary Parts:

Once you have a complex number, you can access its real and imaginary parts using the real and imag attributes:

z = 3 + 2j
real_part = z.real  # This will be 3
imag_part = z.imag  # This will be 2

Using Complex Numbers in Calculations:

Python supports arithmetic operations on complex numbers, such as addition, subtraction, multiplication, and division. These operations follow the same rules as for real numbers, but they involve both real and imaginary parts.

z1 = 2 + 3j
z2 = 5 -2 j
zsum = z1 + z2  # (7+1j)
zsub = z1 - z2  # (-3+5j)
zmul = z1 * z2  # (16+11j)
zdiv = z1/z2  # (0.1379310344827586+0.6551724137931034j)

Conjugate of a Complex Number:

You can obtain the conjugate of a complex number using the conjugate method:

z = 3 + 2j
conjugate_z = z.conjugate()  # This will be 3 - 2j

Built-in Functions:

Python includes built-in functions to work with complex numbers, such as abs to calculate the magnitude (or modulus) of a complex number, and cmath module to handle complex math function. Here’s an example of using complex numbers in Python:

z1 = 3 + 2j
z2 = 1 - 5j
sum_z = z1 + z2
print(sum_z)  # Output will be (4-3j)

magnitude_z1 = abs(z1)
print(magnitude_z1)  # Output will be 3.605551275463989

The Polar Form of Complex Numbers:

Python’s cmath module provides a function cmath.polar to convert a complex number to its polar form, which consists of the magnitude and the phase angle:

import cmath
z = 1 + 1j

polar_z = cmath.polar(z)
print(polar_z) # Output will be (1.4142135623730951, 0.7853981633974483)

Converting Polar Form to Rectangular Form:

You can also convert a complex number from its polar form back to rectangular form using the cmath.rect function:

mag = 2
phase = cmath.pi / 4

rect_z = cmath.rect(mag, phase)
print(rect_z)  # Output will be (1.4142135623730951+1.4142135623730951j)

Plotting Complex Numbers:

You can visualize complex numbers in Python using libraries such as Matplotlib. By plotting the real and imaginary parts of complex numbers on a 2D plane, you can gain insights into their behavior and relationships.

import matplotlib.pyplot as plt

z1 = 3 + 2j
plt.plot(z1.real, z1.imag, 'ro')
plt.xlabel('Real')
plt.ylabel('Imaginary')
plt.title('Complex Number Visualization')
plt.show()

My Training Vido

Keywords

Web Development (Django, Flask) , Data Science (Pandas, NumPy, Matplotlib) , Machine Learning (Scikit-learn, TensorFlow, PyTorch) , Artificial Intelligence , Automation , GUI Development (Tkinter, PyQt) , Game Development (Pygame) , Scientific Computing , Financial Analysis (Pandas, NumPy) , Network Programming , Image Processing (OpenCV) , Web Scraping (Beautiful Soup, Scrapy) , Internet of Things (IoT) Development , Robotics , Cybersecurity , Mobile App Development (Kivy) , Cloud Computing (Boto3, AWS Lambda) , Big Data Analysis (PySpark) , Geographical Information Systems , Natural Language Processing (NLTK, spaCy) , Data Visualization (Seaborn, Plotly) , Bioinformatics (Biopython) , Multimedia Applications , Education and Teaching

Last updated