forked from kalmar/DALGLI0
added polynomial addition
This commit is contained in:
parent
09dcfac9ab
commit
bc3dcdaabe
@ -9,6 +9,22 @@ class Polynomial:
|
|||||||
self.degree = len(coef_list) - 1
|
self.degree = len(coef_list) - 1
|
||||||
self.coefficients = coef_list
|
self.coefficients = coef_list
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def add(p1, p2):
|
||||||
|
result = []
|
||||||
|
f = p1.coefficients
|
||||||
|
g = p2.coefficients
|
||||||
|
if len(f) >= len(g):
|
||||||
|
result = f
|
||||||
|
for i in range(0, len(g)):
|
||||||
|
result[i] = f[i] + g[i]
|
||||||
|
else:
|
||||||
|
result = g
|
||||||
|
for i in range(0, len(f)):
|
||||||
|
result[i] = f[i] + g[i]
|
||||||
|
result = [x % int(Polynomial.n) for x in result]
|
||||||
|
return Polynomial(result)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def multiply(p1, p2):
|
def multiply(p1, p2):
|
||||||
result = [0] * (p1.degree + p2.degree + 1)
|
result = [0] * (p1.degree + p2.degree + 1)
|
||||||
@ -17,14 +33,14 @@ class Polynomial:
|
|||||||
for i in range(0, len(f)):
|
for i in range(0, len(f)):
|
||||||
for j in range(0, len(g)):
|
for j in range(0, len(g)):
|
||||||
result[i+j] += f[i] * g[j]
|
result[i+j] += f[i] * g[j]
|
||||||
result = [x % int(n) for x in result]
|
result = [x % int(Polynomial.n) for x in result]
|
||||||
return Polynomial(result)
|
return Polynomial(result)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def divide(p1, p2):
|
def divide(p1, p2):
|
||||||
def inverse(x):
|
def inverse(x):
|
||||||
for i in range(1, int(n)):
|
for i in range(1, int(Polynomial.n)):
|
||||||
r = (i * x) % int(n)
|
r = (i * x) % int(Polynomial.n)
|
||||||
if r == 1:
|
if r == 1:
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
@ -41,13 +57,13 @@ class Polynomial:
|
|||||||
tmp_coef = f_lead_coef * inverse(g_lead_coef)
|
tmp_coef = f_lead_coef * inverse(g_lead_coef)
|
||||||
tmp_exp = len(f) - 1 - g_deg
|
tmp_exp = len(f) - 1 - g_deg
|
||||||
tmp = []
|
tmp = []
|
||||||
for i in range(tmp_exp):
|
for _ in range(tmp_exp):
|
||||||
tmp.append(0)
|
tmp.append(0)
|
||||||
tmp.append(tmp_coef)
|
tmp.append(tmp_coef)
|
||||||
tmp_poly = Polynomial(tmp)
|
tmp_poly = Polynomial(tmp)
|
||||||
sub = Polynomial.multiply(p2, tmp_poly)
|
sub = Polynomial.multiply(p2, tmp_poly)
|
||||||
f = [x - y for x, y in zip(f, sub.coefficients)]
|
f = [x - y for x, y in zip(f, sub.coefficients)]
|
||||||
f = [x % int(n) for x in f]
|
f = [x % int(Polynomial.n) for x in f]
|
||||||
while f and f[-1] == 0:
|
while f and f[-1] == 0:
|
||||||
f.pop()
|
f.pop()
|
||||||
return Polynomial(f)
|
return Polynomial(f)
|
||||||
@ -60,6 +76,7 @@ class Polynomial:
|
|||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
Polynomial.n = int(sys.argv[1])
|
||||||
c1 = ast.literal_eval(sys.argv[2])
|
c1 = ast.literal_eval(sys.argv[2])
|
||||||
c2 = ast.literal_eval(sys.argv[3])
|
c2 = ast.literal_eval(sys.argv[3])
|
||||||
|
|
||||||
@ -86,5 +103,4 @@ def main():
|
|||||||
print(ans)
|
print(ans)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
n = int(sys.argv[1])
|
main()
|
||||||
main()
|
|
Loading…
Reference in New Issue
Block a user