Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Polynom:
- def __init__(self, coeffs):
- self.coeffs=coeffs
- self.deg=len(coeffs)-1
- def __add__(self, other):
- if type(other)==Polynom:
- h=[]
- if len(self.coeffs)>len(other.coeffs):
- u=len(self.coeffs)-len(other.coeffs)
- for j in range(len(other.coeffs)):
- k=self.coeffs[j]+other.coeffs[j]
- h.append(k)
- h.append(self.coeffs[len(self.coeffs)-1])
- if len(self.coeffs)<len(other.coeffs):
- u=len(other.coeffs)-len(self.coeffs)
- for i in range(len(self.coeffs)):
- k=self.coeffs[i]+other.coeffs[i]
- h.append(k)
- h.append(other.coeffs[len(other.coeffs)-1])
- return Polynom(h)
- else:
- print("Sorrrrrry")
- def __sub__(self, other):
- if type(other)==Polynom:
- h=[]
- if len(self.coeffs)>len(other.coeffs):
- for j in range(len(other.coeffs)):
- k=self.coeffs[j]-other.coeffs[j]
- h.append(k)
- h.append(self.coeffs[len(self.coeffs)-1])
- if len(self.coeffs)<len(other.coeffs):
- for i in range(len(self.coeffs)):
- k=self.coeffs[i]-other.coeffs[i]
- h.append(k)
- h.append(other.coeffs[len(other.coeffs)-1])
- if len(self.coeffs)==len(other.coeffs):
- for i in range(len(self.coeffs)):
- k=self.coeffs[i]-other.coeffs[i]
- h.append(k)
- return Polynom(h)
- else:
- print("Sorrrrrry")
- def __mul__(self, other):
- if type(other) in [float, int]:
- return Polynom(list(map(lambda x: x*other, self.coeffs)))
- else:
- if type(other)==Polynom:
- p=Polynom([])
- for i in range(len(self.coeffs)+1):
- new_coeffs=[]
- if i>0:
- for i in range(i):
- new_coeffs.append(0)
- for j in range(len(other.coeffs)):
- new_coeffs.append(self.coeffs[i]*other.coeffs[j])
- if i==0:
- p=Polynom(new_coeffs)
- else:
- p = p + Polynom(new_coeffs)
- new=[]
- for k in range(1, len(p.coeffs)):
- new.append(p.coeffs[k])
- return Polynom(new)
- else:
- print("Sorrrrrry")
- def __str__(self):
- result=""
- for i in range(self.deg+1):
- c=self.coeffs[-1-i]
- if c==0:
- continue
- elif c==1:
- result+= "+ x^{} ".format(self.deg - i)
- elif c==-1:
- result+= "- x^{} ".format(self.deg - i)
- elif c>0:
- result+= "+ {}*x^{} ".format(c, self.deg - i)
- elif c<0:
- result+= "- {}*x^{} ".format(-c, self.deg - i)
- return result.strip("+ ")
- def __neg__(self):
- return self*(-1)
- def __call__(self,x):
- result=0
- for i in range(self.deg + 1):
- result+=self.coeffs[i]*x**i
- return result
- def derivative(self):
- new_coeffs=[]
- for i in range(1, self.deg+1):
- new_coeffs.append(self.coeffs[i]*i)
- return Polynom(new_coeffs)
- def __bool__(self):
- if self.coeffs!=[]:
- return True
- else:
- return False
- def __divmod__(self, other):
- h = Polynom(list(self.coeffs))
- div_coeffs = []
- k=[]
- while h.deg>=other.deg:
- l=[]
- new_coeffs=[]
- if h.deg-other.deg>0:
- for i in range(h.deg-other.deg):
- new_coeffs.append(0)
- for j in range(other.deg + 1):
- new_coeffs.append(other.coeffs[j]*h.coeffs[h.deg])
- div_coeffs.append(h.coeffs[h.deg])
- h=h-Polynom(new_coeffs)
- for i in range(h.deg):
- l.append(h.coeffs[i])
- h=Polynom(list(l))
- div_coeffs=div_coeffs[::-1]
- k=Polynom(list(div_coeffs))
- return k, h
- def __floordiv__(self, other):
- return self.__divmod__(other)[0]
- def __mod__(self, other):
- return self.__divmod__(other)[1]
- def number_of_roots(self, a, b):
- p=self//gcd(self, self.derivative())
- p1, p2 = p, p.derivative()
- system=[p1, p2]
- while p2:
- p1, p2 = p2, p1%p2
- system.append(p2)
- system=system[:-1]
- list_a=[]
- list_b=[]
- for s in system:
- if s(a)!=0:
- list_a.append(s(a))
- if s(b)!=0:
- list_b.append(s(b))
- n_a=0
- n_b=0
- for i in range(len(list_a)-1):
- if list_a[i]*list_a[i+1]<0:
- n_a+=1
- if list_b[i]*list_b[i+1]<0:
- n_b+=1
- return n_a-n_b
- def gcd(p1, p2):
- while p2:
- p1, p2 = p2, p1%p2
- return p1
- #p1=Polynom([1,1])
- p2=Polynom([1, -2, 1])
- #print(p2)
- #print(p1)
- #print(p2.derivative())
- #print(p2%p1)
- #print(gcd(p2,p1))
- p3=Polynom([2,0,1])
- p4=Polynom([0,1,1])
- p1 = Polynom([24, -50, 35, -10, 1])
- print(p1)
- print(p1.number_of_roots(0, 6))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement