Commit e803c84b authored by BITTU KUMAR's avatar BITTU KUMAR

A

parents
class ComplexNumber:
def __init__(comp,x=0,y=0):
comp.x=x
comp.y=y
def __str__(comp):
if comp.y<0 :
return "({0}-i{1})".format(comp.x,abs(comp.y))
else:
return "({0}+i{1})".format(comp.x,comp.y)
def __add__(comp,other):
if (type(other) is ComplexNumber):
return ComplexNumber(comp.x+other.x,comp.y+other.y)
else:
return ComplexNumber(comp.x+other,comp.y)
def __radd__(comp,other):
if (type(other) is ComplexNumber):
return ComplexNumber(comp.x+other.x,comp.y+other.y)
else:
return ComplexNumber(comp.x+other,comp.y)
def __lt__(comp,other):
print("Not Allowed")
def __gt__(comp,other):
print("Not Allowed")
def __eq__(comp,other):
return (comp.x==other.x and comp.y==other.y)
def __ne__(comp,other):
return (comp.x!=other.x or comp.y!=other.y)
def __sub__(comp,other):
if (type(other) is ComplexNumber) :
x=comp.x-other.x
y=comp.y-other.y
return ComplexNumber(x,y)
else:
return ComplexNumber(comp.x-other,comp.y)
def __rsub__(comp,other):
temp=ComplexNumber(other,0)
x=temp.x-comp.x
y=temp.y-comp.y
return ComplexNumber(x,y)
def __mul__(comp,other):
if (type(other) is ComplexNumber ):
x=comp.x*other.x - comp.y*other.y
y=comp.x*other.y + comp.y*other.x
return ComplexNumber(x,y)
else:
return ComplexNumber(other*comp.x,other*comp.y)
def __rmul__(comp,other):
if (type(other) is ComplexNumber ):
x=comp.x*other.x - comp.y*other.y
y=comp.x*other.y + comp.y*other.x
return ComplexNumber(x,y)
else:
return ComplexNumber(other*comp.x,other*comp.y)
def __truediv__(comp,other):
if (type(other) is ComplexNumber ):
temp=comp*conj(other)
temp=temp/(other.x**2+other.y**2)
return temp
else:
return ComplexNumber(comp.x/other,comp.y/other)
def __rtruediv__(comp,other):
temp1=ComplexNumber(other,0)
temp=temp1*conj(comp)
temp=temp/(comp.x**2+comp.y**2)
return temp
def __pow__(comp,num):
x=ComplexNumber(1,0)
for i in range(0,num):
x=x*comp
return x;
def conj(comp):
if (type(comp) is ComplexNumber ):
x=comp.x
y=-comp.y
return ComplexNumber(x,y)
else:
return comp
def mag(comp):
return (comp.x**2+comp.y**2)**(0.5)
from complexno import ComplexNumber
def myexponent(x):
epowx=1
m=1
for i in range(1,100):
m*=x/i
epowx+=m
return epowx
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment