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+ if not isinstance (other , Complex ):
17+ other = Complex (other )
18+ return Complex (self .re + other .re , self .im + other .im )
19+ # Operator overloading 2 : -
20+ def __sub__ (self , other ):
21+ if not isinstance (other , Complex ):
22+ other = Complex (other )
23+ return Complex (self .re - other .re , self .im - other .im )
24+ # Operator overloading 3 : *
25+ def __mul__ (self , other ):
26+ if not isinstance (other , Complex ):
27+ other = Complex (other )
28+ return Complex (self .re * other .re - self .im * other .im , self .re * other .im + self .im * other .re )
29+ # Operator overloading 4 : /
30+ def __truediv__ (self , other ):
31+ try :
32+ if not isinstance (other , Complex ):
33+ other = Complex (other )
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+ # Operator overloading 5 : //
40+ def __floordiv__ (self , other ):
41+ try :
42+ if not isinstance (other , Complex ):
43+ other = Complex (other )
44+ den = other * other .con ()
45+ num = self * other .con ()
46+ return Complex (num .re // den .re , num .im // den .re )
47+ except ZeroDivisionError as err :
48+ print ('Error: {}' .format (err ))
49+ def __gt__ (self , other ):
50+ if not isinstance (other , Complex ):
51+ other = Complex (other )
52+ return self .mod () > other .mod ()
53+ # Operator overloading 7 : >=
54+ def __ge__ (self , other ):
55+ if not isinstance (other , Complex ):
56+ other = Complex (other )
57+ return self .mod () >= other .mod ()
58+ # Operator overloading 8: <
59+ def __lt__ (self , other ):
60+ if not isinstance (other , Complex ):
61+ other = Complex (other )
62+ return not self >= other
63+ # Operator overloading 9: <=
64+ def __le__ (self , other ):
65+ if not isinstance (other , Complex ):
66+ other = Complex (other )
67+ return not self > other
68+ # Operator overloading 10: ==
69+ def __eq__ (self , other ):
70+ if not isinstance (other , Complex ):
71+ other = Complex (other )
72+ return (self .re == other .re ) and (self .im == other .im )
73+ # Operator overloading 11: !=
74+ def __ne__ (self , other ):
75+ if not isinstance (other , Complex ):
76+ other = Complex (other )
77+ return not self == other
78+ # Operator overloading 12: +=
79+ def __iadd__ (self , other ):
80+ if not isinstance (other , Complex ):
81+ other = Complex (other )
82+ self .re += other .re
83+ self .im += other .im
84+ return Complex (self .re , self .im )
85+ # Operator overloading 13: -=
86+ def __isub__ (self , other ):
87+ if not isinstance (other , Complex ):
88+ other = Complex (other )
89+ self .re -= other .re
90+ self .im -= other .im
91+ return Complex (self .re , self .im )
92+ # Operator overloading 14: *=
93+ def __imul__ (self , other ):
94+ if not isinstance (other , Complex ):
95+ other = Complex (other )
96+ self .re = self .re * other .re - self .im * other .im
97+ self .im = self .re * other .im + self .im * other .re
98+ return Complex (self .re , self .im )
99+ # Operator overloading 15: /=
100+ def __idiv__ (self , other ):
101+ try :
102+ if not isinstance (other , Complex ):
103+ other = Complex (other )
104+ den = other * other .con ()
105+ num = self * other .con ()
106+ self .re = num .re / den .re
107+ self .im = num .im / den .re
108+ return Complex (self .re , self .im )
109+ except ZeroDivisionError as err :
110+ print ('Error: {}' .format (err ))
111+ # Operator overloading 16: //=
112+ def __ifloordiv__ (self , other ):
113+ try :
114+ if not isinstance (other , Complex ):
115+ other = Complex (other )
116+ den = other * other .con ()
117+ num = self * other .con ()
118+ self .re = num .re // den .re
119+ self .im = num .im // den .re
120+ return Complex (self .re , self .im )
121+ except ZeroDivisionError as err :
122+ print ('Error: {}' .format (err ))
123+ ## Helper functions
124+ # Module function to calculate the modulus of a complex number
125+ def mod (self ):
126+ return sqrt (self .re ** 2 + self .im ** 2 )
127+ # Conjugated of a complex number
128+ def con (self ):
129+ return Complex (self .re , - self .im )
130+ # String function
131+ def __str__ (self ):
132+ output = str (self .re ) + ' + ' + str (self .im ) + 'j' if (self .im != 0 ) else str (self .re )
133+ output = str (self .re ) + ' - ' + str (- self .im ) + 'j' if (self .im < 0 ) else output
134+ return output
0 commit comments