In: Computer Science
Write the class Complex that supports the basic complex number operations. Such operations are addition (+), subtraction (-) and multiplication (*) of complex numbers, and multiplication (*) of a complex by a scalar (float or int). All methods must return (not print) the result. Class also supports the rich comparison for equality (= =)
- Check the doctest for object behavior examples.
- You must use the special methods for those 4 operators in order to override their behavior
- You will need other special methods to achieve a legible object representation.
- Multiplication by scalar and by another complex number use the same operator, so you must check the type of the object in order to decide which operation you have to perform
- Rich comparison returns a Boolean value
- The rest of the methods must return a new Complex object, this means the original
objects should not change after the operation is performed.
- Test your code, this is how you ensure you get the most credit out of your work!!
- When returning error messages, make sure your string contains the word ‘error’
- You are not allowed to modify the constructor or any given code.
- Hint: Section 3.3.1. Basic customization (__eq__) and Full Section 3.3.8. Emulating numeric types inhttps://docs.python.org/3/reference/datamodel.html#emulating-numeric-types
Write the property method conjugate into the Complex class. The complex conjugate of the complex number z = x + yi is given by x – yi
Remember, you are not allowed to modify the given constructor. The method conjugate must be a property method, otherwise, no credit will be given. Property methods are discussed in the video lectures!
Starting Code:
class Complex: ''' >>> a=Complex(5,-6) >>> b=Complex(2,14) >>> a+b (7, 8i) >>> a-b (3, -20i) >>> a*b (94, 58i) >>> b*5 (10, 70i) >>> 5*b (10, 70i) >>> print(a) 5-6i >>> print(b) 2+14i >>> b (2, 14i) >>> isinstance(a+b, Complex) True >>> a.conjugate (5, 6i) >>> b==Complex(2,14) True >>> a==b False ''' def __init__(self, real, imag): self.real = real self.imag = imag
class Complex:
def __init__(self,real,imag):
self.real = real
self.imag = imag
def __add__(self,two):
return Complex(self.real +
two.real, self.imag + two.imag)
def __sub__(self,two):
return Complex(self.real -
two.real, self.imag - two.imag)
def __rmul__(self,two):
if(type(two) == int or type(two) ==
float):
return
Complex(self.real * two,self.imag*two)
def __mul__(self,two):
if(type(two) == Complex):
res =
Complex(0,0)
res.real =
(self.real * two.real) - (self.imag * two.imag)
res.imag =
(self.real * two.imag) + (self.imag * two.real)
return res
else:
return
Complex(self.real * two,self.imag*two)
def conjugate(self):
return
Complex(self.real,-(self.imag))
def __eq__(self,two):
if(type(two) == Complex):
return self.real
== two.real and self.imag == two.imag
else:
return
false
def __str__(self):
return "({} ,
{}i)".format(self.real,self.imag)
a = Complex(5,-6)
b = Complex(2,14)
print(a+b)
print(a-b)
print(a*b)
print(b*5)
print(5*b)
print(a)
print(b)
print(isinstance(a+b,Complex))
print(a.conjugate())
print(b == Complex(2,14))
print(a == b)