Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.03 KB | None | 0 0
  1. class Polynom:
  2. def __init__(self, coeffs):
  3. self.coeffs=coeffs
  4. self.deg=len(coeffs)-1
  5.  
  6. def __add__(self, other):
  7. if type(other)==Polynom:
  8. h=[]
  9. if len(self.coeffs)>len(other.coeffs):
  10. u=len(self.coeffs)-len(other.coeffs)
  11. for j in range(len(other.coeffs)):
  12. k=self.coeffs[j]+other.coeffs[j]
  13. h.append(k)
  14. h.append(self.coeffs[len(self.coeffs)-1])
  15. if len(self.coeffs)<len(other.coeffs):
  16. u=len(other.coeffs)-len(self.coeffs)
  17. for i in range(len(self.coeffs)):
  18. k=self.coeffs[i]+other.coeffs[i]
  19. h.append(k)
  20. h.append(other.coeffs[len(other.coeffs)-1])
  21. return Polynom(h)
  22. else:
  23. print("Sorrrrrry")
  24.  
  25. def __sub__(self, other):
  26. if type(other)==Polynom:
  27. h=[]
  28. if len(self.coeffs)>len(other.coeffs):
  29. for j in range(len(other.coeffs)):
  30. k=self.coeffs[j]-other.coeffs[j]
  31. h.append(k)
  32. h.append(self.coeffs[len(self.coeffs)-1])
  33. if len(self.coeffs)<len(other.coeffs):
  34. for i in range(len(self.coeffs)):
  35. k=self.coeffs[i]-other.coeffs[i]
  36. h.append(k)
  37. h.append(other.coeffs[len(other.coeffs)-1])
  38. if len(self.coeffs)==len(other.coeffs):
  39. for i in range(len(self.coeffs)):
  40. k=self.coeffs[i]-other.coeffs[i]
  41. h.append(k)
  42. return Polynom(h)
  43. else:
  44. print("Sorrrrrry")
  45.  
  46. def __mul__(self, other):
  47. if type(other) in [float, int]:
  48. return Polynom(list(map(lambda x: x*other, self.coeffs)))
  49. else:
  50. if type(other)==Polynom:
  51. p=Polynom([])
  52. for i in range(len(self.coeffs)+1):
  53. new_coeffs=[]
  54. if i>0:
  55. for i in range(i):
  56. new_coeffs.append(0)
  57. for j in range(len(other.coeffs)):
  58. new_coeffs.append(self.coeffs[i]*other.coeffs[j])
  59. if i==0:
  60. p=Polynom(new_coeffs)
  61. else:
  62. p = p + Polynom(new_coeffs)
  63. new=[]
  64. for k in range(1, len(p.coeffs)):
  65. new.append(p.coeffs[k])
  66. return Polynom(new)
  67. else:
  68. print("Sorrrrrry")
  69.  
  70. def __str__(self):
  71. result=""
  72. for i in range(self.deg+1):
  73. c=self.coeffs[-1-i]
  74. if c==0:
  75. continue
  76. elif c==1:
  77. result+= "+ x^{} ".format(self.deg - i)
  78. elif c==-1:
  79. result+= "- x^{} ".format(self.deg - i)
  80. elif c>0:
  81. result+= "+ {}*x^{} ".format(c, self.deg - i)
  82. elif c<0:
  83. result+= "- {}*x^{} ".format(-c, self.deg - i)
  84. return result.strip("+ ")
  85.  
  86. def __neg__(self):
  87. return self*(-1)
  88.  
  89. def __call__(self,x):
  90. result=0
  91. for i in range(self.deg + 1):
  92. result+=self.coeffs[i]*x**i
  93. return result
  94.  
  95. def derivative(self):
  96. new_coeffs=[]
  97. for i in range(1, self.deg+1):
  98. new_coeffs.append(self.coeffs[i]*i)
  99. return Polynom(new_coeffs)
  100. def __bool__(self):
  101. if self.coeffs!=[]:
  102. return True
  103. else:
  104. return False
  105. def __divmod__(self, other):
  106. h = Polynom(list(self.coeffs))
  107. div_coeffs = []
  108. k=[]
  109. while h.deg>=other.deg:
  110. l=[]
  111. new_coeffs=[]
  112. if h.deg-other.deg>0:
  113. for i in range(h.deg-other.deg):
  114. new_coeffs.append(0)
  115. for j in range(other.deg + 1):
  116. new_coeffs.append(other.coeffs[j]*h.coeffs[h.deg])
  117. div_coeffs.append(h.coeffs[h.deg])
  118. h=h-Polynom(new_coeffs)
  119. for i in range(h.deg):
  120. l.append(h.coeffs[i])
  121. h=Polynom(list(l))
  122. div_coeffs=div_coeffs[::-1]
  123. k=Polynom(list(div_coeffs))
  124. return k, h
  125.  
  126.  
  127. def __floordiv__(self, other):
  128. return self.__divmod__(other)[0]
  129.  
  130. def __mod__(self, other):
  131. return self.__divmod__(other)[1]
  132.  
  133.  
  134.  
  135. def number_of_roots(self, a, b):
  136. p=self//gcd(self, self.derivative())
  137. p1, p2 = p, p.derivative()
  138. system=[p1, p2]
  139. while p2:
  140. p1, p2 = p2, p1%p2
  141. system.append(p2)
  142. system=system[:-1]
  143. list_a=[]
  144. list_b=[]
  145. for s in system:
  146. if s(a)!=0:
  147. list_a.append(s(a))
  148. if s(b)!=0:
  149. list_b.append(s(b))
  150. n_a=0
  151. n_b=0
  152. for i in range(len(list_a)-1):
  153. if list_a[i]*list_a[i+1]<0:
  154. n_a+=1
  155. if list_b[i]*list_b[i+1]<0:
  156. n_b+=1
  157. return n_a-n_b
  158.  
  159.  
  160.  
  161. def gcd(p1, p2):
  162. while p2:
  163. p1, p2 = p2, p1%p2
  164. return p1
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174. #p1=Polynom([1,1])
  175. p2=Polynom([1, -2, 1])
  176. #print(p2)
  177. #print(p1)
  178. #print(p2.derivative())
  179. #print(p2%p1)
  180. #print(gcd(p2,p1))
  181. p3=Polynom([2,0,1])
  182. p4=Polynom([0,1,1])
  183. p1 = Polynom([24, -50, 35, -10, 1])
  184. print(p1)
  185. print(p1.number_of_roots(0, 6))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement