Program in Python with proper indentation and syntax Design a class in Python th
ID: 3857891 • Letter: P
Question
Program in Python with proper indentation and syntax
Design a class in Python that represents complex numbers and supports important operations such as addition, subtraction, multiplication and division. For the Pyhton version you will need to implement the following functions for each operation:
op: Complex × Complex Complex
op: Complex × double Complex
op: double × Complex Complex
Where op is one of +, -, *, or /. In addition, you will need to overload the stream insertion operator << to print objects of this type.
A constructor must be defined as well as overloading the assignment operator to allow for implicit conversion from doubles to Complex. Any other methods you deem appropriate should also be included. The more complete your class the better.
The Python version you should also include functions for converting from complexes to strings.
The required files for this project are: a complex.h file that contains the declaration of the complex class, The python files required are a complex.py file.
Explanation / Answer
HI,
Please go throw below code
# Python code to demonstrate the working of
# complex(), real() and imag()
# importing "cmath" for complex number operations
import cmath
# Initializing real numbers
x = 5
y = 3
# converting x and y into complex number
z = complex(x,y);
# printing real and imaginary part of complex number
print ("The real part of complex number is : ",end="")
print (z.real)
print ("The imaginary part of complex number is : ",end="")
print (z.imag)
finding of phase of complex number
# Python code to demonstrate the working of
# phase()
# importing "cmath" for complex number operations
import cmath
# Initializing real numbers
x = -1.0
y = 0.0
# converting x and y into complex number
z = complex(x,y);
# printing phase of a complex number using phase()
print ("The phase of complex number is : ",end="")
print (cmath.phase(z))
this is to Converting from polar to rectangular form and vice versa# Python code to demonstrate the working of
# polar() and rect()
# importing "cmath" for complex number operations
import cmath
import math
# Initializing real numbers
x = 1.0
y = 1.0
# converting x and y into complex number
z = complex(x,y);
# converting complex number into polar using polar()
w = cmath.polar(z)
# printing modulus and argument of polar complex number
print ("The modulus and argument of polar complex number is : ",end="")
print (w)
# converting complex number into rectangular using rect()
w = cmath.rect(1.4142135623730951, 0.7853981633974483)
# printing rectangular form of complex number
print ("The rectangular form of complex number is : ",end="")
print (w)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.