diff --git a/Chapter_7/complexe.py b/Chapter_7/complexe.py index c1ccf4307a934c419ab6100812148b594cf54859..c945e824deb4a8cf2b81d8396aedaf46ef3016ae 100644 --- a/Chapter_7/complexe.py +++ b/Chapter_7/complexe.py @@ -16,6 +16,9 @@ class complexe: else: return "{}{}i".format(self.real,self.imag) + def __neg__(self): + return complexe(-self.real,-self.imag) + def __add__(self,other): if isinstance(other,complexe): return complexe(self.real + other.real, self.imag+other.imag) @@ -28,6 +31,18 @@ class complexe: def __radd__(self,other): return self + other + def __sub__(self,other): + if isinstance(other,complexe): + return complexe(self.real - other.real, self.imag-other.imag) + else: + try: + return complexe(self.real-other, self.imag) + except: + return NotImplemented + + def __rsub__(self,other): + return - self + other + def __mul__(self,other): if isinstance(other,complexe): return complexe(self.real*other.real - self.imag * other.imag, @@ -97,6 +112,5 @@ class complexe: else: return (abs(self),math.atan(self.imag/self.real)) - if __name__ == "__main__": print("For explanation on all the 'NotImplemented in the code, please check-out not_implemented_expl.py")