From 61ad1fefefcf028f2947cc7c1ec851bf8bda3038 Mon Sep 17 00:00:00 2001 From: servostar Date: Tue, 21 Nov 2023 11:35:58 +0100 Subject: [PATCH] added lagrange polynom calculation --- lagrange/__init__.py | 24 ++++++++++++++++++++++++ main.py | 2 ++ 2 files changed, 26 insertions(+) create mode 100644 lagrange/__init__.py diff --git a/lagrange/__init__.py b/lagrange/__init__.py new file mode 100644 index 0000000..3ef4463 --- /dev/null +++ b/lagrange/__init__.py @@ -0,0 +1,24 @@ + +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)}") + diff --git a/main.py b/main.py index c6a003b..bd68b45 100644 --- a/main.py +++ b/main.py @@ -15,6 +15,7 @@ # Copyright (c) Sven Vogel import fixpoint_approximation +import lagrange import linear_approximation import matrix import newton_polynom @@ -30,3 +31,4 @@ if __name__ == '__main__': matrix.test() taylor_sine_approx.test() integral_approximation.test() + lagrange.test()