-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlagrange.py
47 lines (37 loc) · 1.31 KB
/
lagrange.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
import numpy as np
from vandermonde import GF
# p = np.poly1d([1, 2, 3, 4])
# print(p)
# roots = [1, 2]
# poly = np.poly1d(roots, True)
# print(poly)
class Lagrange:
def __init__(self, gf) :
self.gf = gf
def lagrange_polynomial(self, x_values, y_values):
"""
x_values is the array containing the x values of your set of coordinates
y_values is the array containing the y values of your set of coordinate
{(1,2), (3,4)} --> x_values, y_values = [1,3], [2,4]
"""
# x_values = [0,1,2]
# y_values = [2,1,4]
n = len(x_values)
polynomial = np.poly1d([0])
for i in range(n):
roots = []
roots.extend(element for element in x_values if element != x_values[i])
factor = 1
for j in range(n):
if i != j:
factor *= x_values[i]-x_values[j]
p_roots = (y_values[i]*np.poly1d(roots, True))/factor
polynomial += p_roots
return polynomial
# print(Lagrange([0,1,2],[2,1,4]))
# print(Lagrange([-2,1,3,7],[5,7,11,34]))
gf = GF(3, 0b1011) # Initialize GF(2^3) with primitive polynomial 0b1011
lagrange = Lagrange(gf)
x_values = [0, 1, 2]
y_values = [2, 1, 4]
polynomial = lagrange.lagrange_polynomial(x_values, y_values)