Created
December 14, 2016 17:23
-
-
Save dsouzadyn/c9feb2aee44b608476b3005686ec7f26 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Complex: | |
def __init__(self, a = 0, b = 0): | |
self.a = a | |
self.b = b | |
def __repr__(self): | |
if self.b >= 0: | |
return '{:0.2f}+{:0.2f}i'.format(self.a, self.b) | |
else: | |
return '{:0.2f}-{:0.2f}i'.format(self.a, abs(self.b)) | |
def __eq__(self, n): | |
return __repr__() | |
def __add__(self, n): | |
a = self.a + n.a | |
b = self.b + n.b | |
return Complex(a, b) | |
def __sub__(self, n): | |
a = self.a - n.a | |
b = self.b - n.b | |
return Complex(a, b) | |
def __mul__(self, n): | |
a = (self.a*n.a) - (self.b*n.b) | |
b = (self.a*n.b) + (self.b*n.a) | |
return Complex(a, b) | |
def __truediv__(self, n): | |
denominator = (n.a)**2 + (n.b)**2 | |
a = ((self.a*n.a) + (self.b*n.b)) / denominator | |
b = (-1*(self.a*n.b) + (self.b*n.a)) / denominator | |
return Complex(a, b) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment