fork(2) download
  1. import cmath
  2. import math
  3.  
  4. def iseven(x):
  5. if 0 == int(x) % 2:
  6. return True
  7. else:
  8. return False
  9.  
  10.  
  11. def convert(x):
  12. if x % 4 == 0 and iseven(x) == True:
  13. return int(1)
  14. elif x % 4 == 2 and iseven(x) == True:
  15. return int(-1)
  16. elif (x + 1) % 4 == 0 and iseven(x) == False:
  17. return complex(0, -1)
  18. elif (x + 1) % 4 == 2 and iseven(x) == False:
  19. return complex(0, 1)
  20.  
  21.  
  22. def simplify():
  23. total = int(input('How many terms are there? '))
  24. converted = []
  25. for x in range(total):
  26. term = input('What is the term? ')
  27. if term == 'i':
  28. final = complex(0, 1)
  29. converted.append(complex(final))
  30. elif term == '-i':
  31. final = -complex(0, 1)
  32. converted.append(complex(final))
  33. else:
  34. number = term.split("i")
  35. if len(number) == 1:
  36. final = int(number[0])
  37. converted.append(complex(final))
  38. elif number[1] == "":
  39. coeff = int(number[0])
  40. final = coeff * complex(0, 1)
  41. converted.append(final)
  42. else:
  43. number = term.split("i^")
  44. exp = int(number[1])
  45. ivalue = convert(exp)
  46. if number[0] == "":
  47. coeff = 1
  48. elif number[0] == "-":
  49. coeff = -1
  50. else:
  51. coeff = int(number[0])
  52. final = coeff * ivalue
  53. converted.append(complex(final))
  54. # print(converted)
  55. final = complex(0, 0)
  56. for x in range(total):
  57. final += converted[x]
  58. final2 = str(final).replace('j', 'i')
  59. print('The combination of your terms is', final2)
  60.  
  61.  
  62. def modulus(number):
  63. num = number.replace("i", "").split(" ")
  64. real = num[0]
  65. if real.startswith('sqrt'):
  66. real = math.sqrt(float(real[5:-1]))
  67. elif real.startswith('-sqrt'):
  68. real = math.sqrt(float(real[6:-1]))
  69. else:
  70. real = int(num[0])
  71.  
  72. im = num[2]
  73. if im.startswith('sqrt'):
  74. im = math.sqrt(float(im[5:-1]))
  75. elif len(num[2]) == 0:
  76. im = 1
  77. else:
  78. im = int(num[2])
  79. complex2 = complex(real, im)
  80.  
  81. squared = (float(abs(complex2))) ** 2
  82. rounded = str(round(squared))
  83. return str('sqrt(' + rounded + ')') + ' OR ' + str(round(math.sqrt(squared), 3))
  84.  
  85.  
  86. def polar(number2):
  87. num = number2.replace("i", "").split(" ")
  88. real = num[0]
  89. if real.startswith('sqrt'):
  90. real = math.sqrt(float(real[5:-1]))
  91. elif real.startswith('-sqrt'):
  92. real = -(math.sqrt(float(real[6:-1])))
  93. else:
  94. real = float(num[0])
  95.  
  96. if num[1] == '-':
  97. imag = num[2]
  98. if imag.startswith('sqrt'):
  99. imag = -(math.sqrt(float(imag[5:-1])))
  100. elif len(num[2]) == 0:
  101. imag = -1
  102. else:
  103. imag = -(float(num[2]))
  104.  
  105. if num[1] == '+':
  106. imag = num[2]
  107. if imag.startswith('sqrt'):
  108. imag = math.sqrt(float(imag[5:-1]))
  109. elif len(num[2]) == 0:
  110. imag = 1
  111. else:
  112. imag = float(num[2])
  113.  
  114. rad = cmath.phase(complex(real, imag))
  115. degrees = (180 * rad) / math.pi
  116. return str(round(degrees, 1))
  117.  
  118.  
  119. def quadratic():
  120. a = input('What is the value of a? ')
  121. aa = a.replace("i", "").split(" ")
  122. xa = aa[0]
  123. if xa.startswith('sqrt'):
  124. xa = math.sqrt(float(xa[5:-1]))
  125. elif xa.startswith('-sqrt'):
  126. xa = -(math.sqrt(float(xa[6:-1])))
  127. else:
  128. xa = int(aa[0])
  129.  
  130. if aa[1] == '-':
  131. ya = aa[2]
  132. if ya.startswith('sqrt'):
  133. ya = -(math.sqrt(float(ya[5:-1])))
  134. elif len(aa[2]) == 0:
  135. ya = -1
  136. else:
  137. ya = -float(aa[2])
  138.  
  139. if aa[1] == '+':
  140. ya = aa[2]
  141. if ya.startswith('sqrt'):
  142. ya = math.sqrt(float(ya[5:-1]))
  143. elif len(aa[2]) == 0:
  144. ya = 1
  145. else:
  146. ya = int(aa[2])
  147.  
  148. compa = complex(xa, ya)
  149.  
  150. b = input('What is the value of b? ')
  151. bb = b.replace("i", "").split(" ")
  152. xb = bb[0]
  153. if xb.startswith('sqrt'):
  154. xb = math.sqrt(float(xb[5:-1]))
  155. elif xb.startswith('-sqrt'):
  156. xb = -(math.sqrt(float(xb[6:-1])))
  157. else:
  158. xb = int(bb[0])
  159.  
  160. if bb[1] == '-':
  161. yb = bb[2]
  162. if yb.startswith('sqrt'):
  163. yb = -math.sqrt(float(yb[5:-1]))
  164. elif len(bb[2]) == 0:
  165. yb = -1
  166. else:
  167. yb = -float(bb[2])
  168.  
  169. if bb[1] == '+':
  170. yb = bb[2]
  171. if yb.startswith('sqrt'):
  172. yb = math.sqrt(float(yb[5:-1]))
  173. elif len(bb[2]) == 0:
  174. yb = 1
  175. else:
  176. yb = int(bb[2])
  177.  
  178. compb = complex(xb, yb)
  179.  
  180. c = input('What is the value of c? ')
  181. cc = c.replace("i", "").split(" ")
  182. xc = cc[0]
  183. if xc.startswith('sqrt'):
  184. xc = math.sqrt(float(xc[5:-1]))
  185. elif xc.startswith('-sqrt'):
  186. xc = -(math.sqrt(float(xc[6:-1])))
  187. else:
  188. xc = int(cc[0])
  189. if cc[1] == '-':
  190. yc = cc[2]
  191. if yc.startswith('sqrt'):
  192. yc = -math.sqrt(float(yc[5:-1]))
  193. elif len(cc[2]) == 0:
  194. yc = -1
  195. else:
  196. yc = -float(cc[2])
  197.  
  198. if cc[1] == '+':
  199. yc = cc[2]
  200. if yc.startswith('sqrt'):
  201. yc = math.sqrt(float(yc[5:-1]))
  202. elif len(cc[2]) == 0:
  203. yc = 1
  204. else:
  205. yc = int(cc[2])
  206.  
  207. compc = complex(xc, yc)
  208. # print(compa, compb, compc)
  209. sqrt = ((compb ** 2) - (4 * compa * compc)) ** 0.5
  210.  
  211. sol1 = ((-compb) + sqrt) / (2 * compa)
  212. sol2 = ((-compb) - sqrt) / (2 * compa)
  213.  
  214. sol1real = round((sol1.real), 2)
  215. sol2real = round((sol2.real), 2)
  216. sol1imag = round((sol1.imag), 2)
  217. sol2imag = round((sol2.imag), 2)
  218.  
  219. x = str(complex(sol1real, sol1imag))
  220. y = str(complex(sol2real, sol2imag))
  221.  
  222. sol1comp = x.replace("j", "i")
  223. sol2comp = y.replace("j", "i")
  224.  
  225. print('Your solutions are: \n' + sol1comp + '\n' + 'and\n' + sol2comp)
  226.  
  227.  
  228. def main():
  229. print('1. Simplification of complex i values \n'
  230. '2. Modulus of a complex number \n'
  231. '3. Polar form of a complex number \n'
  232. '4. Solve a quadratic equation with complex numbers \n\n')
  233.  
  234. choice = input('I want to compute: ')
  235.  
  236. if int(choice) == 1:
  237. print("\nSimplification of a complex i values \n")
  238. simplify()
  239.  
  240. elif str(choice) == '2':
  241. print("\nModulus of a complex number: \n")
  242. number2 = input('What is the complex number? ')
  243. print(modulus(number2))
  244.  
  245. elif str(choice) == '3':
  246. print("\nPolar form of a complex number \n")
  247. number3 = input('What is the complex number? ')
  248. print('(' + modulus(number3) + ', ' + polar(number3) + '˚' + ')')
  249.  
  250. elif str(choice) == '4':
  251. print("\nThis code solves quadratic equations in the form az^2 + bz + c = 0 \n"
  252. "Where a, b, and c are complex numbers")
  253. quadratic()
  254.  
  255. else:
  256. print("Sorry! That's not an option")
  257.  
  258. if __name__ == '__main__':
  259. main()
  260.  
  261.  
Runtime error #stdin #stdout #stderr 0.02s 10008KB
stdin
Standard input is empty
stdout
1. Simplification of complex i values 
2. Modulus of a complex number 
3. Polar form of a complex number 
4. Solve a quadratic equation with complex numbers 


I want to compute: 
This code solves quadratic equations in the form az^2 + bz + c = 0 
Where a, b, and c are complex numbers
What is the value of a? What is the value of b? 
stderr
Traceback (most recent call last):
  File "./prog.py", line 259, in <module>
  File "./prog.py", line 253, in main
  File "./prog.py", line 167, in quadratic
ValueError: could not convert string to float: