Python program to define class for complex number objects


Suppose we want to do complex number tasks by defining a complex number class with following operations −

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

The complex numbers will be shown in the form (a + bi). We have two complex numbers, will perform these operations on them. Inside the class we overload the add(), sub(), mul() and div() methods so that we can use the operators to perform the operations. We also overload __str__() method to print the complex number in proper form.

So, if the input is like c1 = 2+3i c2 = 5-2i, then the output will be (7.00 + 1.00i), (-3.00 + 5.00i), (16.00 + 11.00i), (0.14 + 0.66i), 3.61, 5.39.

To solve this, we will follow these steps −

  • Define complex class with real part re and imaginary part im
  • Define a function add(). This will take o
  • return a new Complex object with (re + o.re, im + o.im)
  • Define a function sub() . This will take o
  • return a new Complex object with (re - o.re, im - o.im)
  • Define a function mul() . This will take o
  • return a new Complex object with (re * o.re -im * o.im, re * o.im + im * o.re)
  • Define a function div(). This will take o
  • m := o.re * o.re + o.im * o.im
  • return a new Complex number object with ((re * o.re + im * o.im)/m, (im * o.re - re * o.im)/m)
  • Define a function mod() . This will take
  • return square root of (re * re + im * im)
  • Overload __str__().
  • if im is same as 0, then
    • return re up to two decimal places
  • if re is same as 0, then
    • return im up to two decimal places
  • if im < 0, then
    • return re - im i, both (re and im are up to two decimal places)
  • otherwise,
    • return re + im i, both (re and im are up to two decimal places)

Example

Let us see the following implementation to get better understanding

from math import sqrt
class Complex:
   def __init__(self, real, imag):
      self.re = real
      self.im = imag

   def __add__(self, o):
      return Complex(self.re+o.re, self.im+o.im)

   def __sub__(self, o):
      return Complex(self.re-o.re, self.im-o.im)

   def __mul__(self, o):
      return Complex(self.re*o.re-self.im*o.im, self.re * o.im + self.im * o.re)

   def __truediv__(self, o):
      m = o.re * o.re + o.im * o.im
      return Complex((self.re * o.re + self.im * o.im)/m, (self.im * o.re - self.re * o.im)/m)

   def __str__(self):
      if self.im == 0:
         return '%.2f' % self.re
      if self.re == 0:
         return '%.2fi' % self.im
      if self.im < 0:
         return '%.2f - %.2fi' % (self.re, -self.im)
      else:
         return '%.2f + %.2fi' % (self.re, self.im)
      def mod(self):
         return sqrt(self.re*self.re+self.im*self.im)

def solve(comp1, comp2):
   print(comp1 + comp2)
   print(comp1 - comp2)
   print(comp1 * comp2)
   print(comp1 / comp2)
   print('%.2f' % comp1.mod())
   print('%.2f' % comp2.mod())

comp1 = Complex(2, 3)
comp2 = Complex(5, -2)
solve(comp1, comp2)

Input

2, 3
5, -2

Output

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

Updated on: 12-Oct-2021

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements