hozer

jshturm

Jun 7th, 2014
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.90 KB | None | 0 0
  1. from numpy import poly1d
  2. import numpy
  3.  
  4.  
  5. def printPoly(f):
  6.     for i in range(len(f.c)):
  7.         if f.c[i] != 0:
  8.             if f.c[i] > 0 and i != 0:
  9.                 znak='+'
  10.             else:
  11.                 znak = ''
  12.             cof = 0
  13.             if f.c[i] % 1 == 0:
  14.                 cof = int(f.c[i])
  15.             else:
  16.                 cof = numpy.around(f.c[i], decimals=1)
  17.             xstep = ''
  18.             if i == len(f.c)-1:
  19.                 xstep = ''
  20.             elif i == len(f.c)-2:
  21.                 xstep = 'x'
  22.             else:
  23.                 xstep = 'x^' + str(len(f.c) - i-1)
  24.             print(znak + str(cof) + xstep, end='')
  25.     print('\n')
  26.    
  27. def GetRoots(coef):
  28.     rts = numpy.roots(coef)
  29.     decroots = []
  30.     for rt in rts:
  31.         if numpy.imag(rt).flat[0] == 0:
  32.             decroots.append(numpy.around(numpy.real(rt).flat[0], decimals=5))
  33.     return decroots, len(rts)
  34.  
  35. def printTable(th, table, zch):
  36.     print('\n')
  37.     print('', end='\t')
  38.     for element in th:
  39.         print(element, end="\t")
  40.        
  41.     print('\n')
  42.     i = 0
  43.     for element in table:
  44.         print("f"+str(i), end="\t")
  45.         i+=1
  46.         for j in element:
  47.             print(j, end="\t")
  48.         print('\n')
  49.     print('', end='\t')
  50.     for element in zch:
  51.         print(element, end="\t")
  52.     print('\n')
  53.  
  54.  
  55. def zchange(arr, i):
  56.     c = 0
  57.     z = arr[0][i]
  58.     for k in range(len(arr)):
  59.         if z == -arr[k][i]:
  60.             z = arr[k][i]
  61.             c += 1
  62.     return c
  63.  
  64. def getInterval(tablh, tablzch):
  65.     intervals = {}
  66.     for j in range(len(tablzch)-1):
  67.         difference = abs(tablzch[j] - tablzch[j+1])
  68.         if difference != 0:
  69.             intervals['(' + str(tablh[j]) + '; ' + str(tablh[j+1]) + ')'] = difference
  70.     for key in intervals.keys():
  71.         print(key + ' - ' + str(intervals[key]) + ' roots')
  72.  
  73. def calc(coef=[]):
  74.     if len(coef) == 0:
  75.         coef = input("Input coef: ").split()
  76.     for i in range(len(coef)):
  77.         coef[i] = float(coef[i])
  78.  
  79.     f = [poly1d(coef), poly1d(coef).deriv()]
  80.  
  81.     while len(f[-1]) > 0:
  82.         tmp, ft = f[-2] / f[-1]
  83.         f.append(-ft)
  84.  
  85.     tabl = []
  86.     tablHead = ['inf', 0, 'inf']
  87.     for fp in f:
  88.         try:
  89.             fx = 0
  90.             if fp(0) == 0.0:
  91.                 fx = 0
  92.             else:
  93.                 fx = fp(0) / abs(fp(0))
  94.             row = [fp(-10000) / abs(fp(-10000)), fx, fp(10000) / abs(fp(10000))]
  95.         except BaseException:
  96.             pass
  97.         tabl.append(row)
  98.  
  99.     znk = []
  100.     for i in range(len(tabl[0])):
  101.         znk.append(zchange(tabl, i))
  102.  
  103.     vidrc = abs(znk[0] - znk[1])
  104.     dodrc = abs(znk[1] - znk[2])
  105.  
  106.  
  107.     for i in range(len(f)):
  108.         print('f' + str(i), end=': ')
  109.         printPoly(f[i])
  110.  
  111.     i = -1
  112.     while vidrc > 0:
  113.         tablHead.insert(1, i)
  114.         for fi in range(len(f)):
  115.             x = i
  116.             if f[fi](x) != 0:
  117.                 fx = f[fi](x) / abs(f[fi](x))
  118.             else:
  119.                 fx = 0
  120.             tabl[fi].insert(1, fx)
  121.         i -= 1
  122.         roots_on = abs(zchange(tabl, 1) - zchange(tabl, 2))
  123.         if roots_on >= 1:
  124.             vidrc -= roots_on
  125.  
  126.     i = 1
  127.     while dodrc > 0:
  128.         tablHead.insert(-1, i)
  129.         for fi in range(len(f)):
  130.             x = i
  131.             if f[fi](x) != 0:
  132.                 fx = f[fi](x) / abs(f[fi](x))
  133.             else:
  134.                 fx = 0
  135.             tabl[fi].insert(-1, fx)
  136.         i += 1
  137.         roots_on = abs(zchange(tabl, -2) - zchange(tabl, -3))
  138.         if roots_on >= 1:
  139.             dodrc -= roots_on
  140.  
  141.     tablZch = []
  142.     for i in range(len(tabl[0])):
  143.         tablZch.append(zchange(tabl, i))
  144.  
  145.     printTable(tablHead, tabl, tablZch)
  146.     getInterval(tablHead, tablZch)
  147.    
  148.     _roots, ca_roots = GetRoots(coef)
  149.     print("\nCount of real roots:" + str(len(_roots)))
  150.     print("Count of complex roots: " + str(ca_roots - len(_roots)))
  151.     print("Roots: ")
  152.     for i in _roots:
  153.         print('\t'+str(i))
  154.  
  155.  
  156. def main():
  157.     do = True
  158.     while (do):
  159.         calc()
  160.         if(input("\nOnce more? (y/n): ").lower() != 'y'):
  161.             do = False
  162.         print('\n')
  163.  
  164. def tester():
  165.     t_coef = []
  166.     t_coef.append([2, -10, 10, -3])
  167.     t_coef.append([1, -3, -3, 11, -3, -3, 1])
  168.     t_coef.append([1, 1, -4, -3, 3, 1])
  169.     t_coef.append([1, -2, 3, -9, 1])
  170.     t_coef.append([1, 5, 10, -5, -3])
  171.     t_coef.append([1, -2, 3, -9, 1])
  172.     t_coef.append([1, -2, -7, 8, 1])
  173.     t_coef.append([9, 0, -126, -252, -140])
  174.     t_coef.append([1, -1, 1, -1, -1])
  175.     t_coef.append([1, -2, 1, -2, 1])
  176.     t_coef.append([1, -4, 8, -12, 8])
  177.     t_coef.append([3, 12, 9, -1])
  178.     t_coef.append([4, -12, 8, -1])
  179.     t_coef.append([2, -8, 8, -1])
  180.     for test in t_coef:
  181.         print('\n' + str(test))
  182.         calc(test)
  183.  
  184. mode = input("Set mode (1 - normal mode, 2 - presentation): ")
  185. if mode == '1':
  186.     main()
  187. elif mode == '2':
  188.     tester()
Advertisement
Add Comment
Please, Sign In to add comment