#------------------------------------------------------------------------------ # complex.py # Definition of the Complex class. #------------------------------------------------------------------------------ import math class Complex(object): """Class representing a complex number.""" def __init__(self, re=0, im=0 ): """Initialize a Complex object.""" self._real = re # real part self._imag = im # imaginary part # end @property def real(self): """Return the real part of a complex number.""" return self._real # end @property def imag(self): """Return the imaginary part of a complex number.""" return self._imag # end def __str__(self): """Return the string representation of a complex number.""" a = self._real b = self._imag if a==b==0: return '0' elif b==0: return str(a) elif a==0: return '{} i'.format(b) else: if b>0: return '({}+{}i)'.format(a, b) else: return '({}-{}i)'.format(a, abs(b)) # end # end # end def __repr__(self): """Return the detailed string representation of a complex number.""" return 'complex.Complex({}, {})'.format(self._real, self._imag) # end def __eq__(self, other): """ Return True if self and other have the same real parts and the same imaginary parts. Return False otherwise. """ a = self._real b = self._imag c = other._real d = other._imag return (a==c) and (b==d) # end def __add__(self, other): """Return the sum of two complex numbers.""" x = self._real + other._real y = self._imag + other._imag return Complex(x, y) # end def __sub__(self, other): """Return the difference of two complex numbers.""" x = self._real - other._real y = self._imag - other._imag return Complex(x, y) # end def __mul__(self, other): """Return the product of two complex numbers.""" a = self._real b = self._imag c = other._real d = other._imag x = a*c - b*d y = a*d + b*c return Complex(x, y) # end @property def sq_modulus(self): """Return the square modulus of a complex number.""" a = self._real b = self._imag return a**2 + b**2 # end @property def modulus(self): """Return the modulus of a complex number.""" return math.sqrt(self.sq_modulus) # end def conjugate(self): """Return the conjugate of a complex number.""" a = self._real b = self._imag return Complex(a, -b) # end def inverse(self): """Return the multiplicative inverse of a complex number.""" m = self.sq_modulus # to cache it, notice no () a = self._real b = self._imag if m==0: raise ZeroDivisionError('cannot divide by complex zero') # end return Complex(a/m, -b/m) # end def __truediv__(self, other): """Return the quotient of two complex numbers.""" a = self._real b = self._imag c = other._real d = other._imag m = other.sq_modulus x = (a*c+b*d)/m y = (b*c-a*d)/m return Complex(x, y) # end @property def arg(self): """ Return the argument of a complex number, i.e., the angle theta made with the positive real axis, where -pi <= theta <= pi. """ a = self._real b = self._imag return math.atan2(b, a) # end def log(self): """Return the complex logarithm base e of self.""" x = math.log(self.modulus) y = self.arg return Complex(x, y) # end def exp(self): """Return the complex exponent base e of self.""" a = self._real b = self._imag x = math.exp(a)*math.cos(b) y = math.exp(a)*math.sin(b) return Complex(x, y) # end def __pow__(self, other): """Return complex pow(self, other).""" return (other*(self.log())).exp() # end # end # test the Complex class def main(): z = Complex(2, 3) # 2+3i w = Complex(4, -1) # 4-i u = Complex(-5, 2) # -5+2i print(z*z - u/w) print((z/w).real) print(u.inverse().imag) # print(1/0) # print(Complex().inverse()) print(z.arg) print(z.modulus) print(z.log()) print(z.exp().log()) print(z**w) # end #------------------------------------------------------------------------------ if __name__=='__main__': main() # end