May-10-2019, 09:52 AM
this code is supposed to contain methods for complex numbers
This is what I came up with:
from scipy import * import numpy as np from numpy import array from scipy import integrate import matplotlib.pyplot as plt import scipy.integrate as si from scipy.optimize import fsolve from math import log import sys class Complex(object): def __init__(self, real, imag): self.real = real self.imag = imag def __add__(self, other): return Complex(self.real + other.real, self.imag + other.imag) def __sub__(self, other): return Complex(self.real - other.real, self.imag - other.imag) def __mul__(self, other): return Complex(self.real*other.real - self.imag*other.imag, self.imag*other.real + self.real*other.imag) def __div__(self, other): a,b,c,d = self.real, self.imag, other.real, other.imag # short forms r = float(c**2 + d**2) return Complex((a*c+b*d)/r, (b*c-a*d)/r) def __repr__(self): return '[{}+{}i]'.format(self.real, self.imag)and now I have to implement two more methods in order to return the real and the imaginary part.
This is what I came up with:
def __real1__(self): a,b = self.real, self.imag return a def __imag__(self): a,b=self.real, self.imag return bbut I am not sure if this is correct. Moreover, I don't know how to test it.