May-12-2019, 01:12 PM
I have to check if whether or not two complex numbers are equal (check my 'equal' function in the code), but this code always returns 'False' no matter what I input.
Why does this happen?
Why does this happen?
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 real1(self): a,b = self.real, self.imag return a def imag1(self): a,b=self.real, self.imag return b def arg(self): a,b=self.real, self.imag #return (np.arctan(b/a)) if a>0 and b>0: return (np.arctan(b/a),abs(a-b)) elif a<0 and b>0: return (pi - np.arctan(b/a),abs(a-b) ) elif a<0 and b<0: return (- pi + np.arctan(b/a),abs(a-b)) elif a>0 and b<0: return (-np.arctan(b/a),abs(a-b)) def equal(self, other): a,b,c,d=self.real, self.imag, other.real, other.imag if (a==c and b==d): print("[{} and {}]".format(a=c,b=d)) else: print("numbers not equal") def __repr__(self): return '[{}+{}i]'.format(self.real, self.imag) c=Complex(3,4) d=Complex(3,4) print(equal(c,d))