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

Program in Python Design a class in Python that represents complex numbers and s

ID: 3857845 • Letter: P

Question

Program in Python

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

Python code:

from math import pow
class complexMethod:
def __init__(self, realNo, imagNo):
self.realNo = realNo
self.imagNo = imagNo
def __addition__(self, other):
return complexMethod(self.realNo+other.realNo, self.imagNo+other.imagNo)
def __substraction__(self, other):
return complexMethod(self.realNo-other.realNo, self.imagNo-other.imagNo)
def __multiplication__(self, other):
return complexMethod(self.realNo*other.realNo-self.imagNo*other.imagNo, self.realNo*other.imagNo+self.imagNo*other.realNo)
def __division__(self, other):
try:
return self.__multiplication__(complexMethod(other.realNo, -1*other.imagNo)).__multiplication__(complexMethod(1.0/(other.mod().realNo)**2, 0))
except Exception as e:
print e
return None
def mod(self):
return pow(self.realNo**2+self.imagNo**2, 0.5)
def __str__(self, precision=2):
return str(("%."+"%df"%precision)%float(self.realNo))+('+' if self.imagNo>=0 else '-')+str(("%."+"%df"%precision)%float(abs(self.imagNo)))+'i'

print "Enter realNo and imaginary part of complex No - 1(separeated by space)"
A = complexMethod(*map(float, raw_input().strip().split()))
print "Enter realNo and imaginary part of complexMethod No - 2(separeated by space)"
B = complexMethod(*map(float, raw_input().strip().split()))

print "Addition: " + str(A+B)
print "Subtraction: " + str(A-B)
print "Multiplication: " + str(A*B)
print "Division: "+ str(A/B)
print "Modulas of complex Number A: " + str(A.mod())

utput:

Enter realNo and imaginary part of complex No - 1
3 4
Enter realNo and imaginary part of complex No - 2
2 3
Addition: 5.00+7.00i
Subtraction: 1.00+1.00i
Multiplication: -6.00+17.00i
Division: 1.38-0.08i
Modulas of complex Number A: 5.0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote