25 lines
579 B
Python
25 lines
579 B
Python
|
|
||
|
def lagrange_polynom(points, j):
|
||
|
nom = ""
|
||
|
dem = 1
|
||
|
xj = points[j][0]
|
||
|
for u in range(len(points)):
|
||
|
if u == j:
|
||
|
continue
|
||
|
nom += f"(x - {points[u][0]})"
|
||
|
dem *= xj - points[u][0]
|
||
|
return (nom, dem)
|
||
|
|
||
|
def interpolate_lagrange(points):
|
||
|
n = len(points)
|
||
|
poly = ""
|
||
|
for i in range(n):
|
||
|
(lagrange, dem) = lagrange_polynom(points, i)
|
||
|
poly += f" + {points[i][1]*dem}*{lagrange}"
|
||
|
return poly
|
||
|
|
||
|
def test():
|
||
|
points = [[1,2], [3,5], [4,-1]]
|
||
|
print(f"polynom via lagrange: {interpolate_lagrange(points)}")
|
||
|
|