-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpolynomial.py
65 lines (49 loc) · 2.02 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
65
from math import *
class Polynomial:
def __init__(self, q: int, coefficients=None, encoding=0):
self.q = q
if coefficients == None:
self.coefficients = from_encoding(encoding, q)
else:
self.coefficients = coefficients
def __add__(self, other):
return Polynomial(self.q,
coefficients=list(map(lambda t: t[0] + t[1], zip(self.coefficients, other.coefficients))))
def __pow__(self, other, modulo=None):
if modulo is None:
new_coeff = [x * other for x in self.coefficients]
else:
new_coeff = [x * other % modulo for x in self.coefficients]
return Polynomial(self.q, coefficients=new_coeff)
def __str__(self):
return str(self.coefficients)
def __len__(self):
return len(self.coefficients)
def __iter__(self):
return iter(self.coefficients)
def __getitem__(self, item):
return self.coefficients[item]
def __int__(self):
return self.encode()
def eval_at(self, x, N=None):
result = self.coefficients[-1]
for i in range(-2, -len(self.coefficients) - 1, -1):
if N == None:
result = result * x + self.coefficients[i]
else:
result = (result * x + self.coefficients[i]) % N
return result
def encode(self) -> int:
return self.eval_at(self.q)
def left(self):
leftcoeff = self.coefficients[:len(self.coefficients) // 2]
return Polynomial(q=self.q, coefficients=leftcoeff)
def right(self):
rightcoeff = self.coefficients[len(self.coefficients) // 2:]
return Polynomial(q=self.q, coefficients=rightcoeff)
## We assume that encoding is the encoding of a polynomial with positive integers only
def from_encoding(encoding: int, q: int) -> list:
d = int(log(encoding, q)) + 1
si = [encoding % q ** i for i in range(d + 1)]
coefficients = [(si[i + 1] - si[i]) // q ** i for i in range(d - 1, -1, -1)]
return coefficients