This program is supposed to add, subtract, multiply, and divide complex numbers and doubles as well as complex numbers against complex numbers. When i try to run it, I get a syntax error at line 59 and cannot figure out why. Any help/advice/pointers would be greatly appreciated.
class complex:
def __init__(self, realPart=1, imaginaryPart=2):
self.realPart = realPart
self.imaginaryPart = imaginaryPart
def __add__(self, other):
if isinstance(other, double):
return complex(self.realPart+other, self.imaginaryPart)
elif isinstance(other, complex):
return complex(self.realPart + other.realPart, self.imaginaryPart + other.imaginaryPart)
else:
raise TypeError
def __truediv__(self, other):
if isinstance(other, double):
return complex((self.realPart*other)/(other**2), (self.imaginaryPart*other)/(other**2))
elif isinstance(other, complex):
return complex(((self.realPart * other.realPart) + (self.imaginaryPart * other.imaginaryPart))/(other.realPart**2 + other.imaginaryPart**2), (self.imaginaryPart*other.realPart - self.realPart*other.imaginaryPart)/(other.realPart**2 + other.imaginaryPart**2))
else:
raise TypeError
'''def __float__(self):
return float(self.num) / self.den
def __int__(self):
return self.num / self.den'''
def __mul__(self, other):
if isinstance(other, double):
return complex(self.realPart*other, self.imaginaryPart*other)
elif isinstance(other, complex):
return complex(self.realPart*other.realPart - self.imaginaryPart*other.imaginaryPart, self.realPart*other.imaginaryPart + self.imaginaryPart*other.realPart)
else:
raise TypeError
def __radd__(self, other):
return self + other
def __rtruediv__(self, other):
return complex(other) / self
def __rmul__(self, other):
return self * other
def __rsub__(self, other):
return complex(other) - self
def __str__(self):
return '(' + str(self.realPart) + ' + ' + str(self.imaginaryPart) + 'i' + ')'
def __sub__(self, other):
if isinstance(other, double):
return complex(self.realPart-other, self.imaginaryPart)
elif isinstance(other, complex):
return complex(self.realPart - other.realPart, self.imaginaryPart - other.imaginaryPart)
else:
raise TypeError
if __name__ == '__main__':
a = complex(2, 5)
b = complex(1, 3)
i = 4
print('%s + %s = %s' % (a, b, a + b))
print('%s - %s = %s' % (a, b, a - b))
print('%s * %s = %s' % (a, b, a * b))
print('%s / %s = %s' % (a, b, a / b))
print('%s + %i = %s' % (a, i, a + i))
print('%s - %i = %s' % (a, i, a - i))
print('%s * %i = %s' % (a, i, a * i))
print('%s / %i = %s' % (a, i, a / i))
print('%i + %s = %s' % (i, a, i + a))
print('%i - %s = %s' % (i, a, i - a))
print('%i * %s = %s' % (i, a, i * a))
print('%i / %s = %s' % (i, a, i / a))