Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Python has the complex class for performing complex number arithmetic. For this

ID: 3753308 • Letter: P

Question

Python has the complex class for performing complex number arithmetic. For this assignment, you will design and implement your own Complex class. Note that the complex class in Python is named in lowercase, while our custom Complex class is named with C in uppercase.

A complex number is of the form a + bi, where a and b are real numbers and i is -1. The numbers a and b are known as the real part and the imaginary part of the complex number, respectively. You can perform addition, subtraction, multiplication, and division for complex numbers using the following formulas:

(a + bi) + (c + di) = (a + c) + (b + d)i
(a + bi) - (c + di) = (a - c) + (b - d)i
(a + bi) * (c + di) = (ac - bd) + (bc + ad)i
(a + bi) / (c + di) = (ac + bd)/(c2 + d2) + (bc - ad)i/(c2 + d2)

You can also obtain the absolute value (or length) of a complex number using the formula:

Implement a class named Complex for representing complex numbers with methods _ _add_ _, _ _sub_ _, _ _mul_ _, _ _truediv_ _, and _ _abs_ _ for performing complex-number operations using +, -, *, /, and abs(). Note: In python 3, _ _truediv_ _ is used to override / and _ _floordiv_ _ is used to override //. The _ _abs_ _(self) special method is used to override the absolute value function; this will allow you to write abs(c), where c is an object of the class Complex. Also, implement the comparison methods _ _eq_ _, _ _ne_ _, _ _lt_ _, ...etc. to compare two Complex numbers based on their length. For the purpose of this assignment, assume that Complex numbers are compared based on the values of their lengths. So, for two Complex numbers c1 and c2, c1 is less than c2, if and only if abs(c1) < abs(c2). Similarly c1 equals c2 if and only if abs(c1) equals abs(c2). The methods _ _eq_ _ and _ _ne_ _ should allow you to compare a Complex number with objects of other types for equality and inequality, respectively.

Provide a constructor __init__(self, a = 0, b = 0) to create a complex number a + bi with the default values 0 and 0 for a and b. Your _ _str_ _ method returns the Complex number as a string of the form (a, bi) (it should look as in the sample output below.) If b is 0, it simply returns a --without parentheses. Likewise if a is 0, it returns bi. If both a and b are 0, it returns the string 0. So, the parentheses will be part of the string representation only when both a and b are not 0.

Use the following main function to test your code --copy and paste after the code of the class--.

The function prompts the user to enter two Complex numbers. The input should be provided as two numbers separated by a space. The program displays the two complex numbers, their absolute values, the result of their addition, subtraction, multiplication, and division. It also displays the result of comparing the two Complex numbers using the different comparison operators.

The output from your program must be similar to and use the same format as the following sample output, where input from the user is shown in red:

|a + bi| = _______ a2 + b2

Explanation / Answer

Solution:

#########Complex.py

Sample Run:

Enter the first complex number: 3 4
Enter the second complex number: 2 -4

c1 is (3, 4i)
c2 is (2, -4i)
|(3, 4i)| = 5.0
|(2, -4i)| = 4.47213595499958
(3, 4i) + (2, -4i) = (5, 0i)
(3, 4i) - (2, -4i) = (1, 8i)
(3, 4i) * (2, -4i) = (22, -4i)
(3, 4i) / (2, -4i) = (-0.5, 1i)
Is c1 < c2? False
Is c1 <= c2? False
Is c1 > c2? True
Is c1 >= c2? True
Is c1 == c2? False
Is c1 != c2? True
Is c1 == 'Hello There'? False
Is c1 != 'Hello There'? True


Note: if you have any doubt please do comment below before giving any negative feedback, I will be glad to help you out. thanks!