You are viewing a single comment's thread. Return to all comments →
import math class Complex(object): def __init__(self, real, imaginary): self.real = real self.imaginary = imaginary def __add__(self, no): return Complex(self.real + no.real, self.imaginary + no.imaginary) def __sub__(self, no): return Complex(self.real - no.real, self.imaginary - no.imaginary) def __mul__(self, no): real = self.real * no.real - self.imaginary * no.imaginary imaginary = self.real * no.imaginary + self.imaginary * no.real return Complex(real, imaginary) def __truediv__(self, no): denominator = math.pow(no.real,2) + math.pow(no.imaginary,2) real = ((self.real * no.real) + (self.imaginary * no.imaginary)) / denominator imaginary = ((self.imaginary * no.real) - (self.real * no.imaginary)) / denominator return str(self.__class__(real, imaginary)) def mod(self): real = math.sqrt(math.pow(self.real,2) + math.pow(self.imaginary,2)) imaginary = 0 return str(self.__class__(real, imaginary))
Seems like cookies are disabled on this browser, please enable them to open this website
Classes: Dealing with Complex Numbers
You are viewing a single comment's thread. Return to all comments →