Javatpoint Logo
Javatpoint Logo

Define a Python Class for Complex Numbers

If we define a Python class of complex numbers, the following methods are a must to perform basic complex number operations.

  • The add() method to add the given two complex numbers
  • The sub() method to subtract the given two complex numbers
  • The mul() method to multiply the given two complex numbers
  • The div() method to divide the given two complex numbers
  • The mod() method to get the modulus of the given complex numbers

The form (a + bi), i.e., the standard complex number form, will be used to display the results of the operations. We will apply these operations to the two complex numbers we will take as class arguments. The add(), sub(), multiply(), and divide() methods are defined within the class so that we can use the operators to carry out the operations. Additionally, we will use the __str__() function to output the complex number correctly.

So, for example, if the input is n1 = 1 + 4i n2 = 6 - 1i, then the result of the above operations will be (7.00 + 3.00i), (-5.00 + 5.00i), (10.00 + 23.00i), (0.05 + 0.68i), 4.12, 6.08.

To calculate the results, we will have to follow these steps −

  • Start by defining a Python class having the real part as real and the imaginary part as img
  • Then to add the complex numbers define a method add(). It will take a complex argument named com. This function will return an instance of the Complex class with (real + comp.re, img + comp.im)
  • In the same way, we will define the sub() class to subtract the two complex numbers. It will also take an argument names comp. This method will also return a Complex object of (real - comp.re, img - comp.im)
  • The next operation is to multiply the two given complex numbers. This mul() class will also take an argument named comp and return a Complex instance performing (real * comp.re -img * comp.im, real * comp.im + img * comp.re)
  • We will repeat the same steps for the div() method to divide the numbers. It will perform m = (comp.re * comp.re + comp.im * comp.im), and return the object with ((real * comp.re + img * comp.im)/m, (img * comp.re - real * comp.im)/m)
  • The last one is the mod() function. This method will not take any argument. It will return the square root of (real * real + img * img)
  • At last, as told earlier, we will use the __str__() method.
  • If the imaginary part is zero, then it will return the real part up to two decimal places
  • If the real part is zero, then it will return the imaginary part up to two decimal places
  • Ixf the imaginary part is less than 0. It will return in (real - img i) format, both numbers up to two decimal places.
  • For any other cases, it will return (real + img i) format, both numbers upto two decimal places.

Example

Let us now implement the above algorithm in Python. Here is the code snippet of the implementation.

Code

Output:

7.00 + 1.00i
-3.00 + 5.00i
16.00 + 11.00i
0.14 + 0.66i
3.61
5.39






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA