-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpolynomial.py
executable file
·33 lines (31 loc) · 987 Bytes
/
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
class Polynomial:
def __init__(self, deg=0, coeffs=[0], pairs=None):
"""
coefficients are from lower degree to higher.
a0 + a1 x + a2 x^2 + ... + an x^n
"""
assert deg == len(coeffs) - 1
self.deg = deg
self.coeffs = coeffs
self.pairs = pairs
def multiply(a, b):
c = [0 for _ in range(a.deg + b.deg + 1)]
for x in range(a.deg + 1):
for y in range(b.deg + 1):
c[x+y] += a.coeffs[x] * b.coeffs[y]
return Polynomial(a.deg + b.deg, c)
def eval_at(self, x):
"""
x is the list of x values to evaluate the polynomial.
"""
if type(x) is not list:
x = [x]
y = []
for i in x:
y_i = 0
for d in range(self.deg+1):
y_i += self.coeffs[d] * (i ** d)
y.append(y_i)
if len(x) >= d + 1:
self.pairs = list(zip(x,y))
return y