import cmath
import math
def iseven(x):
if 0 == int(x) % 2:
return True
else:
return False
def convert(x):
if x % 4 == 0 and iseven(x) == True:
return int(1)
elif x % 4 == 2 and iseven(x) == True:
return int(-1)
elif (x + 1) % 4 == 0 and iseven(x) == False:
return complex(0, -1)
elif (x + 1) % 4 == 2 and iseven(x) == False:
return complex(0, 1)
def simplify():
total = int(input('How many terms are there? '))
converted = []
for x in range(total):
term = input('What is the term? ')
if term == 'i':
final = complex(0, 1)
converted.append(complex(final))
elif term == '-i':
final = -complex(0, 1)
converted.append(complex(final))
else:
number = term.split("i")
if len(number) == 1:
final = int(number[0])
converted.append(complex(final))
elif number[1] == "":
coeff = int(number[0])
final = coeff * complex(0, 1)
converted.append(final)
else:
number = term.split("i^")
exp = int(number[1])
ivalue = convert(exp)
if number[0] == "":
coeff = 1
elif number[0] == "-":
coeff = -1
else:
coeff = int(number[0])
final = coeff * ivalue
converted.append(complex(final))
# print(converted)
final = complex(0, 0)
for x in range(total):
final += converted[x]
final2 = str(final).replace('j', 'i')
print('The combination of your terms is', final2)
def modulus(number):
num = number.replace("i", "").split(" ")
real = num[0]
if real.startswith('sqrt'):
real = math.sqrt(float(real[5:-1]))
elif real.startswith('-sqrt'):
real = math.sqrt(float(real[6:-1]))
else:
real = int(num[0])
im = num[2]
if im.startswith('sqrt'):
im = math.sqrt(float(im[5:-1]))
elif len(num[2]) == 0:
im = 1
else:
im = int(num[2])
complex2 = complex(real, im)
squared = (float(abs(complex2))) ** 2
rounded = str(round(squared))
return str('sqrt(' + rounded + ')') + ' OR ' + str(round(math.sqrt(squared), 3))
def polar(number2):
num = number2.replace("i", "").split(" ")
real = num[0]
if real.startswith('sqrt'):
real = math.sqrt(float(real[5:-1]))
elif real.startswith('-sqrt'):
real = -(math.sqrt(float(real[6:-1])))
else:
real = float(num[0])
if num[1] == '-':
imag = num[2]
if imag.startswith('sqrt'):
imag = -(math.sqrt(float(imag[5:-1])))
elif len(num[2]) == 0:
imag = -1
else:
imag = -(float(num[2]))
if num[1] == '+':
imag = num[2]
if imag.startswith('sqrt'):
imag = math.sqrt(float(imag[5:-1]))
elif len(num[2]) == 0:
imag = 1
else:
imag = float(num[2])
rad = cmath.phase(complex(real, imag))
degrees = (180 * rad) / math.pi
return str(round(degrees, 1))
def quadratic():
a = input('What is the value of a? ')
aa = a.replace("i", "").split(" ")
xa = aa[0]
if xa.startswith('sqrt'):
xa = math.sqrt(float(xa[5:-1]))
elif xa.startswith('-sqrt'):
xa = -(math.sqrt(float(xa[6:-1])))
else:
xa = int(aa[0])
if aa[1] == '-':
ya = aa[2]
if ya.startswith('sqrt'):
ya = -(math.sqrt(float(ya[5:-1])))
elif len(aa[2]) == 0:
ya = -1
else:
ya = -float(aa[2])
if aa[1] == '+':
ya = aa[2]
if ya.startswith('sqrt'):
ya = math.sqrt(float(ya[5:-1]))
elif len(aa[2]) == 0:
ya = 1
else:
ya = int(aa[2])
compa = complex(xa, ya)
b = input('What is the value of b? ')
bb = b.replace("i", "").split(" ")
xb = bb[0]
if xb.startswith('sqrt'):
xb = math.sqrt(float(xb[5:-1]))
elif xb.startswith('-sqrt'):
xb = -(math.sqrt(float(xb[6:-1])))
else:
xb = int(bb[0])
if bb[1] == '-':
yb = bb[2]
if yb.startswith('sqrt'):
yb = -math.sqrt(float(yb[5:-1]))
elif len(bb[2]) == 0:
yb = -1
else:
yb = -float(bb[2])
if bb[1] == '+':
yb = bb[2]
if yb.startswith('sqrt'):
yb = math.sqrt(float(yb[5:-1]))
elif len(bb[2]) == 0:
yb = 1
else:
yb = int(bb[2])
compb = complex(xb, yb)
c = input('What is the value of c? ')
cc = c.replace("i", "").split(" ")
xc = cc[0]
if xc.startswith('sqrt'):
xc = math.sqrt(float(xc[5:-1]))
elif xc.startswith('-sqrt'):
xc = -(math.sqrt(float(xc[6:-1])))
else:
xc = int(cc[0])
if cc[1] == '-':
yc = cc[2]
if yc.startswith('sqrt'):
yc = -math.sqrt(float(yc[5:-1]))
elif len(cc[2]) == 0:
yc = -1
else:
yc = -float(cc[2])
if cc[1] == '+':
yc = cc[2]
if yc.startswith('sqrt'):
yc = math.sqrt(float(yc[5:-1]))
elif len(cc[2]) == 0:
yc = 1
else:
yc = int(cc[2])
compc = complex(xc, yc)
# print(compa, compb, compc)
sqrt = ((compb ** 2) - (4 * compa * compc)) ** 0.5
sol1 = ((-compb) + sqrt) / (2 * compa)
sol2 = ((-compb) - sqrt) / (2 * compa)
sol1real = round((sol1.real), 2)
sol2real = round((sol2.real), 2)
sol1imag = round((sol1.imag), 2)
sol2imag = round((sol2.imag), 2)
x = str(complex(sol1real, sol1imag))
y = str(complex(sol2real, sol2imag))
sol1comp = x.replace("j", "i")
sol2comp = y.replace("j", "i")
print('Your solutions are: \n' + sol1comp + '\n' + 'and\n' + sol2comp)
def main():
print('1. Simplification of complex i values \n'
'2. Modulus of a complex number \n'
'3. Polar form of a complex number \n'
'4. Solve a quadratic equation with complex numbers \n\n')
choice = input('I want to compute: ')
if int(choice) == 1:
print("\nSimplification of a complex i values \n")
simplify()
elif str(choice) == '2':
print("\nModulus of a complex number: \n")
number2 = input('What is the complex number? ')
print(modulus(number2))
elif str(choice) == '3':
print("\nPolar form of a complex number \n")
number3 = input('What is the complex number? ')
print('(' + modulus(number3) + ', ' + polar(number3) + '˚' + ')')
elif str(choice) == '4':
print("\nThis code solves quadratic equations in the form az^2 + bz + c = 0 \n"
"Where a, b, and c are complex numbers")
quadratic()
else:
print("Sorry! That's not an option")
if __name__ == '__main__':
main()