Question
Has to be in python. A complex number is of the form a + bi, where a and b are real numbers and i is
Has to be in python.
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:
|a + bi| = |
|
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--.
def main(): input_line = input("Enter the first complex number: ") input_line = list(map(float,input_line.split())) a, b = input_line[0], input_line[1] c1 = Complex(a, b) input_line = input("Enter the second complex number: ") input_line = list(map(float,input_line.split())) a, b = input_line[0], input_line[1] c2 = Complex(a, b) print() print("c1 is", c1) print("c2 is", c2) print("|" + str(c1) + "| = " + str(abs(c1))) print("|" + str(c2) + "| = " + str(abs(c2))) print(c1, " + ", c2, " = ", c1 + c2) print(c1, " - ", c2, " = ", c1 - c2) print(c1, " * ", c2, " = ", c1 * c2) print(c1, " / ", c2, " = ", c1 / c2) print("Is c1 < c2?", c1 < c2) print("Is c1 <= c2?", c1 <= c2) print("Is c1 > c2?", c1 > c2) print("Is c1 >= c2?", c1 >= c2) print("Is c1 == c2?", c1 == c2) print("Is c1 != c2?", c1 != c2) print("Is c1 == 'Hello There'?", c1 == 'Hello There') print("Is c1 != 'Hello There'?", c1 != 'Hello There') main()
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:
>>> main() Enter the first complex number: 2.5 4.5 Enter the second complex number: -2.5 11.2 c1 is (2.5, 4.5i) c2 is (-2.5, 11.2i) |(2.5, 4.5i)| = 5.1478150704935 |(-2.5, 11.2i)| = 11.475626344561764 (2.5, 4.5i) + (-2.5, 11.2i) = 15.7i (2.5, 4.5i) - (-2.5, 11.2i) = (5.0, -6.699999999999999i) (2.5, 4.5i) * (-2.5, 11.2i) = (-56.65, 16.75i) (2.5, 4.5i) / (-2.5, 11.2i) = (0.335257043055661, -0.2980484471106386i) Is c1 < c2? True Is c1 <= c2? True Is c1 > c2? False Is c1 >= c2? False Is c1 == c2? False Is c1 != c2? True Is c1 == 'Hello There'? False Is c1 != 'Hello There'? True >>> main() Enter the first complex number: 3 4 Enter the second complex number: 2 -4 c1 is (3.0, 4.0i) c2 is (2.0, -4.0i) |(3.0, 4.0i)| = 5.0 |(2.0, -4.0i)| = 4.47213595499958 (3.0, 4.0i) + (2.0, -4.0i) = 5.0 (3.0, 4.0i) - (2.0, -4.0i) = (1.0, 8.0i) (3.0, 4.0i) * (2.0, -4.0i) = (22.0, -4.0i) (3.0, 4.0i) / (2.0, -4.0i) = (-0.5, 1.0i) 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 >>> main() Enter the first complex number: 3.5 -2.5 Enter the second complex number: -3.5 2.5 c1 is (3.5, -2.5i) c2 is (-3.5, 2.5i) |(3.5, -2.5i)| = 4.301162633521313 |(-3.5, 2.5i)| = 4.301162633521313 (3.5, -2.5i) + (-3.5, 2.5i) = 0 (3.5, -2.5i) - (-3.5, 2.5i) = (7.0, -5.0i) (3.5, -2.5i) * (-3.5, 2.5i) = (-6.0, 17.5i) (3.5, -2.5i) / (-3.5, 2.5i) = -1.0 Is c1 < c2? False Is c1 <= c2? True Is c1 > c2? False Is c1 >= c2? True Is c1 == c2? True Is c1 != c2? False Is c1 == 'Hello There'? False Is c1 != 'Hello There'? True >>> This is everything in the pictures.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started