Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
real & imag part
#1
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 b
but I am not sure if this is correct. Moreover, I don't know how to test it.
Reply
#2
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 b
is better simply as
def __imag__(self):
    return self.imag
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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())
Reply
#4
did you check my post?

Also note that this

def is_imag(self):
    a,b=self.real, self.imag
will 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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
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).
Reply
#6
(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)
Output:
1 2 >>>
for the second part
You 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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020