I am currently trying to write a program that deals with complex numbers. I have to use classes and methods. I am trying to be able to add, subtract, multiply etc., complex numbers, as well as compare them to one another. I think I have gotten a good start but have some issues.
I have started each method, and I think I just need to fill in the gaps. In the method, I used self.x as a placeholder. I'm not really sure what goes there. First off, the program needs to create it's own complex numbers. I'm new to making methods and functions and I'm not sure if I used (self) in the correct places.
I'm pretty sure I have some syntax issues, i.e. It keeps saying that the variable "i" is not defined, when I clearly have it defined in multiple places.
Once all this is sorted out, I need to create a test function that actually uses the code.
Any help would be appreciated. Thanks!
My code so far:
import math
i = math.sqrt(-1)
class Complex(object):
def __init__(Complex, a, b):
'''Creates Complex Number'''
a = 0
b = 0
return(Complex, a, b)
def __str__(Complex, a, b):
'''Returns complex number as a string'''
a = 0
b = 0
return str(a, b)
def __add__(self):
'''Adds complex numbers'''
i = math.sqrt(-1)
self.x = (a + bi) + (c + di) = (a + c) + (b + d)i
def __sub__(self):
'''Subtracts complex numbers'''
self.x = (a + bi) - (c + di) = (a - c) + (b - d)i
def __mul__(self):
'''Multiplies complex numbers'''
self.x = (a + bi) * (c + di) = (ac - bd) + (bc + ad)i
def __div__(self):
'''Divides complex numbers'''
self.x = (a + bi) / (c + di) = (ac + bd)/(c**2 + d**2) + (bc - ad)i/(c**2 + d**2)
def __abs__(self):
'''Determines absolute value of complex numbers'''
self.x = math.sqrt(a**2 + b**2)