Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from numpy import poly1d
- import numpy
- def printPoly(f):
- for i in range(len(f.c)):
- if f.c[i] != 0:
- if f.c[i] > 0 and i != 0:
- znak='+'
- else:
- znak = ''
- cof = 0
- if f.c[i] % 1 == 0:
- cof = int(f.c[i])
- else:
- cof = numpy.around(f.c[i], decimals=1)
- xstep = ''
- if i == len(f.c)-1:
- xstep = ''
- elif i == len(f.c)-2:
- xstep = 'x'
- else:
- xstep = 'x^' + str(len(f.c) - i-1)
- print(znak + str(cof) + xstep, end='')
- print('\n')
- def GetRoots(coef):
- rts = numpy.roots(coef)
- decroots = []
- for rt in rts:
- if numpy.imag(rt).flat[0] == 0:
- decroots.append(numpy.around(numpy.real(rt).flat[0], decimals=5))
- return decroots, len(rts)
- def printTable(th, table, zch):
- print('\n')
- print('', end='\t')
- for element in th:
- print(element, end="\t")
- print('\n')
- i = 0
- for element in table:
- print("f"+str(i), end="\t")
- i+=1
- for j in element:
- print(j, end="\t")
- print('\n')
- print('', end='\t')
- for element in zch:
- print(element, end="\t")
- print('\n')
- def zchange(arr, i):
- c = 0
- z = arr[0][i]
- for k in range(len(arr)):
- if z == -arr[k][i]:
- z = arr[k][i]
- c += 1
- return c
- def getInterval(tablh, tablzch):
- intervals = {}
- for j in range(len(tablzch)-1):
- difference = abs(tablzch[j] - tablzch[j+1])
- if difference != 0:
- intervals['(' + str(tablh[j]) + '; ' + str(tablh[j+1]) + ')'] = difference
- for key in intervals.keys():
- print(key + ' - ' + str(intervals[key]) + ' roots')
- def calc(coef=[]):
- if len(coef) == 0:
- coef = input("Input coef: ").split()
- for i in range(len(coef)):
- coef[i] = float(coef[i])
- f = [poly1d(coef), poly1d(coef).deriv()]
- while len(f[-1]) > 0:
- tmp, ft = f[-2] / f[-1]
- f.append(-ft)
- tabl = []
- tablHead = ['inf', 0, 'inf']
- for fp in f:
- try:
- fx = 0
- if fp(0) == 0.0:
- fx = 0
- else:
- fx = fp(0) / abs(fp(0))
- row = [fp(-10000) / abs(fp(-10000)), fx, fp(10000) / abs(fp(10000))]
- except BaseException:
- pass
- tabl.append(row)
- znk = []
- for i in range(len(tabl[0])):
- znk.append(zchange(tabl, i))
- vidrc = abs(znk[0] - znk[1])
- dodrc = abs(znk[1] - znk[2])
- for i in range(len(f)):
- print('f' + str(i), end=': ')
- printPoly(f[i])
- i = -1
- while vidrc > 0:
- tablHead.insert(1, i)
- for fi in range(len(f)):
- x = i
- if f[fi](x) != 0:
- fx = f[fi](x) / abs(f[fi](x))
- else:
- fx = 0
- tabl[fi].insert(1, fx)
- i -= 1
- roots_on = abs(zchange(tabl, 1) - zchange(tabl, 2))
- if roots_on >= 1:
- vidrc -= roots_on
- i = 1
- while dodrc > 0:
- tablHead.insert(-1, i)
- for fi in range(len(f)):
- x = i
- if f[fi](x) != 0:
- fx = f[fi](x) / abs(f[fi](x))
- else:
- fx = 0
- tabl[fi].insert(-1, fx)
- i += 1
- roots_on = abs(zchange(tabl, -2) - zchange(tabl, -3))
- if roots_on >= 1:
- dodrc -= roots_on
- tablZch = []
- for i in range(len(tabl[0])):
- tablZch.append(zchange(tabl, i))
- printTable(tablHead, tabl, tablZch)
- getInterval(tablHead, tablZch)
- _roots, ca_roots = GetRoots(coef)
- print("\nCount of real roots:" + str(len(_roots)))
- print("Count of complex roots: " + str(ca_roots - len(_roots)))
- print("Roots: ")
- for i in _roots:
- print('\t'+str(i))
- def main():
- do = True
- while (do):
- calc()
- if(input("\nOnce more? (y/n): ").lower() != 'y'):
- do = False
- print('\n')
- def tester():
- t_coef = []
- t_coef.append([2, -10, 10, -3])
- t_coef.append([1, -3, -3, 11, -3, -3, 1])
- t_coef.append([1, 1, -4, -3, 3, 1])
- t_coef.append([1, -2, 3, -9, 1])
- t_coef.append([1, 5, 10, -5, -3])
- t_coef.append([1, -2, 3, -9, 1])
- t_coef.append([1, -2, -7, 8, 1])
- t_coef.append([9, 0, -126, -252, -140])
- t_coef.append([1, -1, 1, -1, -1])
- t_coef.append([1, -2, 1, -2, 1])
- t_coef.append([1, -4, 8, -12, 8])
- t_coef.append([3, 12, 9, -1])
- t_coef.append([4, -12, 8, -1])
- t_coef.append([2, -8, 8, -1])
- for test in t_coef:
- print('\n' + str(test))
- calc(test)
- mode = input("Set mode (1 - normal mode, 2 - presentation): ")
- if mode == '1':
- main()
- elif mode == '2':
- tester()
Advertisement
Add Comment
Please, Sign In to add comment