![]() |
real & imag part - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: real & imag part (/thread-18239.html) |
real & imag part - mcgrim - May-10-2019 this code is supposed to contain methods for complex numbers 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. RE: real & imag part - buran - May-10-2019 I am confused and wanted to ask this for some time already. There is built-in complex type. Why implement something different. Or why not inherit from the built-in class? Then why implement methods when you have self.real and self.imag attributes already? class Complex(): def __init__(self, real, imag): self.real = real self.imag = imag c = Complex(1, 2) print(c.real) print(c.imag)finally, if you HAVE to define such methods (again - you don't need them) for example this: def __imag__(self): a, b = self.real, self.imag return bis better simply as def __imag__(self): return self.imag RE: real & imag part - mcgrim - May-10-2019 I just solved it. All I did was removing the underscores from said methods, and then, to test them you just have to write .real1 or .imag1 attached to an interval, this way you obtain the real or imag part. Now I am dealing with another problem which I am not quite understanding. How to solve this same exact problem but having boolean answers as return? This is the latest code: 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 is_imag(self): a,b=self.real, self.imag def __repr__(self): return '[{}+{}i]'.format(self.real, self.imag) c=Complex(1,2) d=Complex(3,4) print(c+d) print((c+d).real1()) print((c+d).imag1()) RE: real & imag part - buran - May-10-2019 did you check my post? Also note that this def is_imag(self): a,b=self.real, self.imagwill return None which in turn in a boolean expression will always be evaluated to False i.e. if some_instance.is_imag():will always be False RE: real & imag part - mcgrim - May-10-2019 I read your post but the task is to return specifically the real part of the complex number (and the imag) so what differs between one method and the other is the return value, which for what I need, must exclusively return a (or b depending on the method). This being said, I am not understanding your last comment: Why would a False be returned at all times? If the method implements the real part, it should return true for the first element and false for the second, if the number is in the following format (a+b*i). RE: real & imag part - buran - May-10-2019 (May-10-2019, 10:39 AM)mcgrim Wrote: (and the imag) so what differs between one method and the other is the return value, which for what I need, must exclusively return a (or b depending on the method). yes and I gave you example for that :-) here it is again class Complex(): def __init__(self, real, imag): self.real = real self.imag = imag c = Complex(1, 2) print(c.real) print(c.imag) for the second partYou understand that your method will always return None, right? There is no explicit return, so by default it will return None. Then read this https://docs.python.org/3.7/library/stdtypes.html#truth |