-
Notifications
You must be signed in to change notification settings - Fork 0
/
Polynomial.py
64 lines (55 loc) · 1.88 KB
/
Polynomial.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
from random import randrange
class Zx:
def __init__(self, coeffs):
self.coeffs = coeffs
def coefficient(self, n):
if (type(n) != type(1)) or n < 0:
print('Coefficient does not exist!')
if n >= len(self.coeffs):
return 0
else:
return self.coeffs[n]
def degree(self):
return len(self.coeffs)-1
def eval(self, x):
result = 0
for i in range(len(self.coeffs)):
result += self.coeffs[i]*(x**i)
return result
def add(self, other):
length = max(self.degree(),other.degree()) + 1
result = [0] * length
for i in range(len(result)):
result[i] = self.coefficient(i) + other.coefficient(i)
return Zx(result)
def multiply_single_term(self, coefficient, degree):
result = Zx(self.coeffs[:])
result.coeffs[0:0] = [0]*degree
for i in range(len(result.coeffs)):
result.coeffs[i] *= coefficient
return result
def multiply(self, other):
result = Zx([])
for term in range(other.degree()+1):
result=result.add(self.multiply_single_term(other.coefficient(term),term))
return result
def print_polynomial(self):
terms = []
for i in range(len(self.coeffs)):
if i == 0:
terms.append(str(self.coeffs[i]))
elif i == 1:
terms.append(str(self.coeffs[i])+"x")
else:
terms.append(str(self.coeffs[i])+"x^"+str(i))
terms.reverse()
return "+".join(terms)
def randompoly(self,d,n):
self.coeffs = [0]*n
for j in range(d):
while True:
r = randrange(n)
if not self.coeffs[r]:
break
self.coeffs[r] = 1-2*randrange(2)
self.print_polynomial()