Python Forum
check equality returns False
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
check equality returns False
#1
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?

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))
Reply
#2
First, you code raises an error because there is not equal function in the module namespace. The equal function is a bound method of the complex class. You would have to call it with c.equal(d). Although, if you implement it as __eq__, you can call it with c == d.

Second, the equal method has no return statement, so it always returns None (the default return value). Your method only prints things, which is not the same as returning something. You want to add return true if the numbers are equal, and return False if they aren't.

Third, I'm assuming this is homework, as Python support complex numbers natively. A number with j after it is imaginary, so 10 + 8j is a complex number. So is complex(10, 8).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Forum Jump:

User Panel Messages

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