-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cubic_equation_solver(py3).py
455 lines (295 loc) · 11.5 KB
/
Cubic_equation_solver(py3).py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
#!usr/bin/env python3
import sys
def set_up():
print('What is your variable name? (only one letter)')
org_x = input()
if len(org_x) != 1:
print('\n!!!! This variable is not of length one !!!!')
sys.exit(0)
x = org_x.lower()
print('\nWhat is your equation?')
print(
'(Example equation: x^3 + x^2 + x - 6 = 4) [Automatically put equal to zero]\n')
s = sys.stdin.readline().strip().lower()
return (org_x, x, s)
def find_a(org_x, s, i, m):
if s[:i] == '':
return 1
else:
try:
return float(s[:i])
except (ValueError, SyntaxError):
if m == 4:
print(
'\n!!!! Error at the values before the "{}**3" !!!!'.format(org_x))
elif m == 3:
print(
'\n!!!! Error at the values before the "{}^3" !!!!'.format(org_x))
sys.exit(0)
def find_b(org_x, s, j, j_start, n):
chars = ''.join([c for c in s[j_start:j] if c != ' '])
if chars == '+':
return 1
elif chars == '-':
return -1
if chars[0] == '+' or chars[0] == '-':
try:
return float(chars)
except (ValueError, SyntaxError):
if n == 4:
print(
'\n!!!! Error at the values before the "{}**2" !!!!'.format(org_x))
elif n == 3:
print(
'\n!!!! Error at the values before the "{}^2" !!!!'.format(org_x))
sys.exit(0)
else:
if n == 4:
print(
'\n!!!! Error at the values before the "{}**2" !!!!'.format(org_x))
elif n == 3:
print(
'\n!!!! Error at the values before the "{}^2" !!!!'.format(org_x))
sys.exit(0)
def find_c(org_x, s, j, l, n):
chars = ''.join([c for c in s[j + n:l] if c != ' '])
if chars == '+':
return 1
elif chars == '-':
return -1
if chars[0] == '+' or chars[0] == '-':
try:
return float(chars)
except (ValueError, SyntaxError):
print('\n!!!! Error at the values before the "{}" !!!!'.format(org_x))
sys.exit(0)
else:
print('\n!!!! Error at the values before the "{}" !!!!'.format(org_x))
sys.exit(0)
def find_d_no_eq(org_x, s, k, l):
chars = ''.join([c for c in s[l + 1:k] if c != ' '])
if s[l + 1:k] == '' or len(chars) == 0:
return 0
if chars[0] == '+' or chars[0] == '-':
try:
return float(chars)
except (ValueError, SyntaxError):
print(
'\n!!!! Error at the values after the "{}", just before the end !!!!'.format(org_x))
sys.exit(0)
else:
print(
'\n!!!! Error at the values after the "{}", just before the end !!!!'.format(org_x))
sys.exit(0)
def find_d_with_eq(org_x, s, k, l): # deals with equals
d_left = find_d_no_eq(org_x, s, k, l)
if k == len(s) or (k + 1 == len(s) and s[k + 1] == '='):
if s[k + 1] == '=':
p = 2
else:
p = 1
chars = ''.join([c for c in s[k + p:] if c != ' '])
if len(chars) == 0:
return d_left
else:
try:
return d_left - float(chars)
except (ValueError, SyntaxError):
if p == 2:
print(
'\n!!!! Error at the values after the "==", just at the end !!!!')
elif p == 1:
print(
'\n!!!! Error at the values after the "=", just at the end !!!!')
sys.exit(0)
else:
return d_left
def get_a_b_c_d(org_x, x, s):
#------------------------A--------------------------------
i = 0
# find first x
while i < len(s) and s[i:i + 4] != '{}**3'.format(x) and s[i:i + 3] != '{}^3'.format(x):
i = i + 1
if i < len(s):
if s[i:i + 4] == '{}**3'.format(x):
m = 4
elif s[i:i + 3] == '{}^3'.format(x):
m = 3
else:
print('\n!!!! Error, This is not a cubic equation !!!!')
sys.exit(0)
# 'i' is the position of the first x
# 'i + m' is the position after the 3
a = find_a(org_x, s, i, m)
#----------------------B-------------------------------
j = j_start = i + m
while j < len(s) and s[j:j + 4] != '{}**2'.format(x) and s[j:j + 3] != '{}^2'.format(x):
j = j + 1
if j < len(s):
if s[j:j + 4] == '{}**2'.format(x):
n = 4
elif s[j:j + 3] == '{}^2'.format(x):
n = 3
else:
b = 0
j = j_start
n = 0
if n:
b = find_b(org_x, s, j, j_start, n)
# 'j' is the position of the second x
# 'j + n' is the position after the **2/^2
#--------------------C-------------------------------
l = l_start = j + n
while l < len(s) and s[l] != x: # find third x
l = l + 1
if l < len(s):
# 'l' is the position of the third x
c = find_c(org_x, s, j, l, n) # get value for c
else:
c = 0
l = l_start - 1
else:
b = 0
c = 0
l = j_start - 1
#--------------------D---------------------------------
k = l + 1
while k < len(s) and s[k] != '=': # find equals sign
k = k + 1
if k == len(s):
d = find_d_no_eq(org_x, s, k, l) # get value for d
elif k < len(s):
d = find_d_with_eq(org_x, s, k, l) # get value for d
else:
print('\n!!!! Error, Wrong variable name given !!!!')
sys.exit(0)
return (a, b, c, d)
def get_roots(a, b, c, d):
zero = (b**2) - (3 * a * c)
one = (2 * (b**3)) - (9 * a * b * c) + (27 * d * (a**2))
small_sqr = ((one**2) - (4 * (zero**3)))**(1 / 2)
big_sqr_plus = ((one + small_sqr) / 2)**(1 / 3)
big_sqr_minus = ((one - small_sqr) / 2)**(1 / 3)
polynomial_discriminant = (18 * a * b * c * d) - (4 * d * (b**3)) + \
((b**2) * (c**2)) - (4 * a * (c**3)) - (27 * (a**2) * (d**2))
pos_cube_root_of_unity = complex((-1 / 2) + (1 / 2j * (3)**(1 / 2)))
neg_cube_root_of_unity = complex((-1 / 2) - (1 / 2j * (3)**(1 / 2)))
if polynomial_discriminant == 0:
if zero == 0:
# One triple root ---- x**3 - 3x**2 + 3x - 1
root_1 = root_2 = root_3 = -b / (3 * a)
else:
# One double root and a single root ---- x**3 + x**2 - 33x + 63
root_1 = root_2 = ((9 * a * d) - (b * c)) / (2 * zero)
root_3 = ((4 * a * b * c) - (9 * d * (a**2)) - b**3) / (a * zero)
else:
# 1 real root + 2 complex conjugates ---- x**3 + x**2 + x + 1
# OR 3 disict real routes ---- x**3 - 6x**2 + 11x - 6
if one + small_sqr == 0:
root_1 = -(1 / (3 * a)) * (
b + big_sqr_minus + (zero / big_sqr_minus))
root_2 = -(1 / (3 * a)) * (
b + (pos_cube_root_of_unity * big_sqr_minus) + (zero / big_sqr_minus))
root_3 = -(1 / (3 * a)) * (
b + (neg_cube_root_of_unity * big_sqr_minus) + (zero / big_sqr_minus))
else:
root_1 = -(1 / (3 * a)) * (
b + big_sqr_plus + (zero / big_sqr_plus))
root_2 = -(1 / (3 * a)) * (
b + (pos_cube_root_of_unity * big_sqr_plus) + (zero / (pos_cube_root_of_unity * big_sqr_plus)))
root_3 = -(1 / (3 * a)) * (
b + (neg_cube_root_of_unity * big_sqr_plus) + (zero / (neg_cube_root_of_unity * big_sqr_plus)))
return [root_1, root_2, root_3]
def strip_string(s):
t = s.rstrip('0').rstrip('.')
if t == '':
return '0'
else:
return t
def tidy_roots(l):
strings = []
for r in l:
comp = []
if not isinstance(r, complex):
a = str(r)
j = 0
while j < len(a) and a[j] != '.':
j = j + 1
if j + 4 < len(a):
strings.append(strip_string('{:.4f}'.format(r)))
else:
strings.append(strip_string(a))
else:
if r.real != 0:
a = str(r.real)
j = 0
while j < len(a) and a[j] != '.':
j = j + 1
if j + 4 < len(a):
if strip_string('{:.4f}'.format(r.real)) != '0' and strip_string('{:.4f}'.format(r.real)) != '-0':
comp.append(strip_string('{:.4f}'.format(r.real)))
else:
comp.append(strip_string(a))
if r.imag != 0:
b = str(abs(r.imag))
i = 0
while i < len(b) and b[i] != '.':
i = i + 1
if i + 4 < len(b):
if strip_string('{:.4f}'.format(r.imag)) != '0' and strip_string('{:.4f}'.format(r.imag)) != '-0':
if r.imag > 0:
comp.append(strip_string(
'+ {:.4f}'.format(abs(r.imag))))
elif r.imag < 0:
comp.append(strip_string(
'- {:.4f}'.format(abs(r.imag))))
else:
if r.imag > 0:
comp.append('+ ' + strip_string(b))
elif r.imag < 0:
comp.append('- ' + strip_string(b))
if len(comp) == 2:
strings.append('{} {}j'.format(comp[0], comp[1]))
elif len(comp) == 1:
if strip_string('{:.4f}'.format(r.real)) == '0' or strip_string('{:.4f}'.format(r.real)) == '-0':
strings.append('{}j'.format(comp[0]))
else:
strings.append(str(comp[0]))
elif len(comp) == 0:
strings.append('0')
return strings
def sign(n):
return n >= 0
def print_solution(org_x, root_list, a):
new = []
for r in root_list:
try:
if sign(int(r)):
new.append('- {}'.format(abs(int(r))))
else:
new.append('+ {}'.format(abs(int(r))))
except ValueError:
break
if len(new) == len(root_list):
if a == 1:
print('\nThe factors are:')
print('({} {})({} {})({} {})'.format(org_x, new[
0], org_x, new[1], org_x, new[2]))
print('\nThe roots are approximately:')
no_duplicute_roots = list(set(root_list))
if len(no_duplicute_roots) == 3:
print('{} = {} AND {} = {} AND {} = {}'.format(
org_x, no_duplicute_roots[0], org_x, no_duplicute_roots[1], org_x, no_duplicute_roots[2]))
elif len(no_duplicute_roots) == 2:
print('{} = {} AND {} = {}'.format(
org_x, no_duplicute_roots[0], org_x, no_duplicute_roots[1]))
elif len(no_duplicute_roots) == 1:
print('{} = {}'.format(org_x, no_duplicute_roots[0]))
def main():
org_x, x, s = set_up()
a, b, c, d = get_a_b_c_d(org_x, x, s)
list_of_roots = get_roots(a, b, c, d)
tidied_roots = tidy_roots(list_of_roots)
print_solution(org_x, tidied_roots, a)
if __name__ == '__main__':
main()