Question: Write the class Complex that supports the basic complex number operations. Such operations are addition (+), subtraction (-), multiplication (*) and division (/) of complex

Write the class Complex that supports the basic complex number operations. Such operations are addition (+), subtraction (-), multiplication (*) and division (/) of complex numbers, and addition (+), subtraction (-) 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 5 operators in order to override their behavior.

- You will need other special methods to achieve a legible object representation (what you see when printing and returning the object).

- Addition, subtraction and 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 performing an invalid operation, return None

- You are not allowed to modify the constructor or any given code.

- Note that to support number (+, - or *) Complex, you are going to need 3 other special methods that can reuse code from the Complex (+, - or *) number operations. Hint: Section 3.3.1. Basic customization (__eq__) and Full Section 3.3.8.

In addition, 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!

python

class Complex: ''' >>> a=Complex(5.2,-6) >>> b=Complex(2,14) >>> a+b (7.2, 8i) >>> a-b (3.2, -20i) >>> a*b (94.4, 60.8i) >>> a/b (-0.368, -0.424i) >>> b*5 (10, 70i) >>> 5*b (10, 70i) >>> 5+a (10.2, -6i) >>> a+5 (10.2, -6i) >>> 5-b (3, -14i) >>> b-5 (-3, 14i) >>> print(a) 5.2-6i >>> print(b) 2+14i >>> b (2, 14i) >>> isinstance(a+b, Complex) True >>> a.conjugate (5.2, 6i) >>> b.conjugate (2, -14i) >>> b==Complex(2,14) True >>> a==b False ''' def __init__(self, real, imag): # You are not allowed to modify the constructor self.real = real self.imag = imag

#--- YOUR CODE STARTS HERE

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!