-1

I am trying to add/sub/multiply two complex numbers. Terminal said there is a SyntaxError at "ComplexCompute". What does that mean? Thanks.

Class ComplexCompute (object):
    def __init__(self, realPart, imagPart):
        self.realPart = realPart
        self.imagPart = imagPart
    def __add__(self, other)
        r1 = self.imagPart
        i1 = self.imagPart
        r2 = other.realPart
        i2 = self.imagPart
        resultR = r1+r2
        resultI = i1+i2
        result = complex(resultR, resultI)
        return result
    def __sub__(self, other)
        r1 = self.imagPart
        i1 = self.imagPart
        r2 = other.realPart
        i2 = self.imagPart
        resultR = r1-r2
        resultI = i1-i2
        result = complex(resultR, resultI)
        return result
    def __mul__(self, other)
        r1 = self.imagPart
        i1 = self.imagPart
        r2 = other.realPart
        i2 = self.imagPart
        resultR = (r1*r2-i1*i2)
        resultI = (r1*i2+r2*i1)
        result = complex(resultR, resultI)
        return result    
c1 = ComplexCompute(2,3)
c2 = ComplexCompute(1,4)
print c1+c2
print c1-c2
print c1*c2

I edited the name of Class in some method. But the terminal showed:

<main.Complex object at 0x1005d8b90>

<main.Complex object at 0x1005d8b90>

<main.Complex object at 0x1005d8b90>

class Complex (object):
    def __init__(self, realPart, imagPart):
        self.realPart = realPart
        self.imagPart = imagPart

    def __add__(self, other):
        r1 = self.imagPart
        i1 = self.imagPart
        r2 = other.realPart
        i2 = other.imagPart 
        resultR = r1+r2
        resultI = i1+i2
        result = Complex(resultR, resultI)
        return result

    def __sub__(self, other):
        r1 = self.imagPart
        i1 = self.imagPart
        r2 = other.realPart
        i2 = other.imagPart
        resultR = r1-r2
        resultI = i1-i2
        result = Complex(resultR, resultI)
        return result

    def __mul__(self, other):
        r1 = self.imagPart
        i1 = self.imagPart
        r2 = other.realPart
        i2 = other.imagPart
        resultR = (r1*r2-i1*i2)
        resultI = (r1*i2+r2*i1)
        result = Complex(resultR, resultI)
        return result   

c1 = Complex(2,3)
c2 = Complex(1,4)

print c1+c2
print c1-c2
print c1*c2

the str method: (which not works)

def __str__(self):
    return '%d+(%d)j'&(self.realPart, self.imagPart)

Latest version: (Terminal shows SyntaxError 'return' outside function in div method)

class Complex (object):
    def __init__(self, realPart, imagPart):
    self.realPart = realPart
    self.imagPart = imagPart            

    def __str__(self):
        if type(self.realPart) == int and type(self.imagPart) == int:
            if self.imagPart >=0:
                return '%d+%di'%(self.realPart, self.imagPart)
            elif self.imagPart <0:
                return '%d%di'%(self.realPart, self.imagPart)
        else:
            if self.imagPart >=0:
                return '%f+%fi'%(self.realPart, self.imagPart)
            elif self.imagPart <0:
               return '%f%fi'%(self.realPart, self.imagPart)

    def __add__(self, other):
        r1 = self.realPart
        i1 = self.imagPart
        r2 = other.realPart
        i2 = other.imagPart
        resultR = r1+r2
        resultI = i1+i2
        result = Complex(resultR, resultI)
        return result

    def __sub__(self, other):
        r1 = self.realPart
        i1 = self.imagPart
        r2 = other.realPart
        i2 = other.imagPart
        resultR = r1-r2
        resultI = i1-i2
        result = Complex(resultR, resultI)
        return result   

    def __mul__(self, other):
        r1 = self.realPart
        i1 = self.imagPart
        r2 = other.realPart
        i2 = other.imagPart
        resultR = (r1*r2-i1*i2)
        resultI = (r1*i2+r2*i1)
        result = Complex(resultR, resultI)
        return result

    def __div__(self, other):
        r1 = self.realPart
        i1 = self.imagPart
        r2 = other.realPart
        i2 = other.imagPart
        resultR = float(float(r1*r2+i1*i2)/float(r2*r2+i2*i2))
        resultI = float(float(r2*i1-r1*i2)/float(r2*r2+i2*i2))
        result = Complex(resultR, resultI)
        return result

c1 = Complex(2,3)
c2 = Complex(1,4)

print c1+c2
print c1-c2
print c1*c2
print c1/c2
16
  • 1
    Your indentation looks incorrect. Is this how your actual code is indented? Commented Oct 25, 2012 at 2:14
  • Capital C in class and space before the parenthesis? Commented Oct 25, 2012 at 2:15
  • @RocketDonkey -- I doubt that the space is an issue, but the capital letter definitely is (see my answer) Commented Oct 25, 2012 at 2:16
  • @mgilson Good call (and sorry, didn't see your post until after I commented :) ) Commented Oct 25, 2012 at 2:18
  • 1
    @lavitanien -- You shouldn't keep correcting the errors on your post for a few reasons. 1) it makes it so that the text/title of the post no longer matches the actual content. 2) it makes the post virtually useless for others who come along and might have the same problem you did (they can no longer see what your problem was!) and least importantly, 3) It makes those of us who answered look stupid because now our answers don't address the question whatsoever. Commented Oct 25, 2012 at 2:38

2 Answers 2

3

Other than your indentation errors (which I assume is due to copy/paste issues), You want it to say class instead of Class (Note the lower case c)

Also, you are missing : after some of your methods:

def __add__(self,other):
                      #^ NEED THIS
Sign up to request clarification or add additional context in comments.

Comments

2

Looks like an indentation error. You need the methods to be indented like so:

class ComplexCompute(object):

  def method(self):
    # blah blah

  def another_method(self):
    # hello

On an unrelated note, watch out for the various bugs where you have another self that should be other. They are in each of your methods implementations. And also you have several times r1 = self.imagPart where you probably meant r1 = self.realPart.

3 Comments

That would raise an IndentationError, not a SyntaxError. Indentation is probably due to copy/paste into SO
@mgilson, he'll have that problem as soon as he reruns it.
True, I assumed they were saying about a "syntax error" not a SyntaxError :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.