-
Notifications
You must be signed in to change notification settings - Fork 0
/
polynomial.py
50 lines (43 loc) · 1.57 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
class Polymonial():
def __init__(self, coeffs: list):
self.coeff = coeffs
self.deg = len(coeffs)
def __str__(self):
poly = str(self.coeff[0])
for i in range(1, self.deg):
poly += ' + ' + str(self.coeff[i]) + 'G^' + str(i)
return poly
def multi(self, another_poly):
result = Polymonial([0]*(self.deg + another_poly.deg - 1))
for i in range(0, self.deg):
for j in range(0, another_poly.deg):
result.coeff[i+j] += self.coeff[i] * another_poly.coeff[j]
return result
def add(self, another_poly):
if self.deg > another_poly.deg:
result = Polymonial([0]*self.deg)
lower_deg = another_poly.deg
for i in range(lower_deg, self.deg):
result.coeff[i] = self.coeff[i]
else:
result = Polymonial([0]*another_poly.deg)
lower_deg = self.deg
for i in range(lower_deg, another_poly.deg):
result.coeff[i] = another_poly.coeff[i]
for i in range(0, lower_deg):
result.coeff[i] += self.coeff[i] + another_poly.coeff[i]
return result
def multiX(self, scalar):
for i in range(0, self.deg):
self.coeff[i] *= scalar
return self
def multiXPoly(polys):
results = polys[0]
for i in range(1, len(polys)):
results = results.multi(polys[i])
return results
def addXPoly(polys):
results = polys[0]
for i in range(1, len(polys)):
results = results.add(polys[i])
return results