Python-Math-Algorithms/lagrange/__init__.py

25 lines
579 B
Python
Raw Permalink Normal View History

2023-11-21 10:35:58 +00:00
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)}")