1+ # -*- coding: utf-8 -*-
2+ """
3+ Created on Tue Sep 10 17:36:25 2019
4+
5+ @author: Omar Belghaouti
6+ """
7+ from math import sqrt
8+ # Complex class for complex number manipulations
9+ class Complex ():
10+ # Constructor
11+ def __init__ (self , re = 0 , im = 0 ):
12+ self .re = re
13+ self .im = im
14+ # Operator overloading 1 : +
15+ def __add__ (self , other ):
16+ return Complex (self .re + other .re , self .im + other .im )
17+ # Operator overloading 2 : -
18+ def __sub__ (self , other ):
19+ return Complex (self .re - other .re , self .im - other .im )
20+ # Operator overloading 3 : *
21+ def __mul__ (self , other ):
22+ return Complex (self .re * other .re - self .im * other .im , self .re * other .im + self .im * other .re )
23+ # Operator overloading 4 : /
24+ def __truediv__ (self , other ):
25+ try :
26+ den = other * other .con ()
27+ num = self * other .con ()
28+ return Complex (num .re / den .re , num .im / den .re )
29+ except ZeroDivisionError as err :
30+ print ('Error: {}' .format (err ))
31+ # Operator overloading 5 : //
32+ def __floordiv__ (self , other ):
33+ try :
34+ den = other * other .con ()
35+ num = self * other .con ()
36+ return Complex (num .re // den .re , num .im // den .re )
37+ except ZeroDivisionError as err :
38+ print ('Error: {}' .format (err ))
39+ def __gt__ (self , other ):
40+ return self .mod () > other .mod ()
41+ # Operator overloading 7 : >=
42+ def __ge__ (self , other ):
43+ return self .mod () >= other .mod ()
44+ # Operator overloading 8: <
45+ def __lt__ (self , other ):
46+ return not self >= other
47+ # Operator overloading 9: <=
48+ def __le__ (self , other ):
49+ return not self > other
50+ # Operator overloading 10: ==
51+ def __eq__ (self , other ):
52+ return (self .re == other .re ) and (self .im == other .im )
53+ # Operator overloading 11: !=
54+ def __ne__ (self , other ):
55+ return not self == other
56+ # Operator overloading 12: +=
57+ def __iadd__ (self , other ):
58+ self .re += other .re
59+ self .im += other .im
60+ # Operator overloading 13: -=
61+ def __isub__ (self , other ):
62+ self .re -= other .re
63+ self .im -= other .im
64+ # Operator overloading 14: *=
65+ def __imul__ (self , other ):
66+ self .re = self .re * other .re - self .im * other .im
67+ self .im = self .re * other .im + self .im * other .re
68+ # Operator overloading 15: /=
69+ def __idiv__ (self , other ):
70+ try :
71+ den = other * Complex (other .re , - other .im )
72+ num = self * Complex (other .re , - other .im )
73+ self .re = num .re / den .re
74+ self .im = num .im / den .re
75+ except ZeroDivisionError as err :
76+ print ('Error: {}' .format (err ))
77+ # Operator overloading 16: //=
78+ def __ifloordiv__ (self , other ):
79+ try :
80+ den = other * Complex (other .re , - other .im )
81+ num = self * Complex (other .re , - other .im )
82+ self .re = num .re // den .re
83+ self .im = num .im // den .re
84+ except ZeroDivisionError as err :
85+ print ('Error: {}' .format (err ))
86+ ## Helper functions
87+ # Module function to calculate the modulus of a complex number
88+ def mod (self ):
89+ return sqrt (self .re ** 2 + self .im ** 2 )
90+ # Conjugated of a complex number
91+ def con (self ):
92+ return Complex (self .re , - self .im )
93+ # String function
94+ def __str__ (self ):
95+ output = str (self .re ) + ' + ' + str (self .im ) + 'j' if (self .im != 0 ) else str (self .re )
96+ output = str (self .re ) + ' - ' + str (- self .im ) + 'j' if (self .im < 0 ) else output
97+ return output
98+ # Callable module
99+ def __call__ (self ):
100+ return self
0 commit comments