From 201931050a82b533453295c7ac5eb2ce63878d5b Mon Sep 17 00:00:00 2001 From: teridax Date: Tue, 25 Apr 2023 16:27:32 +0200 Subject: [PATCH] finished newton polynom --- .idea/.gitignore | 8 +++++ .idea/Python-Math-Algorithms.iml | 8 +++++ .../inspectionProfiles/profiles_settings.xml | 6 ++++ .idea/misc.xml | 4 +++ .idea/modules.xml | 8 +++++ .idea/vcs.xml | 6 ++++ newton_polynom/__init__.py | 31 +++++++++++-------- 7 files changed, 58 insertions(+), 13 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/Python-Math-Algorithms.iml create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/Python-Math-Algorithms.iml b/.idea/Python-Math-Algorithms.iml new file mode 100644 index 0000000..909438d --- /dev/null +++ b/.idea/Python-Math-Algorithms.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..a971a2c --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..c1955aa --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/newton_polynom/__init__.py b/newton_polynom/__init__.py index f71e814..6620b2b 100644 --- a/newton_polynom/__init__.py +++ b/newton_polynom/__init__.py @@ -14,27 +14,32 @@ # Licensed under the GPLv2 License, Version 2.0 (the "License"); # Copyright (c) Sven Vogel -def combine(p0, p1): - return (p1[1] - p1[0]) / (p0[1] - p0[0]) - - -def combine_n(*points): - k = len(points) - 1 +def combine_n(points): + k = len(points) if k == 1: - return combine(points[0], points[1]) + return points[0][1] + elif k == 2: + return (points[1][1] - points[0][1]) / (points[1][0] - points[0][0]) else: - return (combine_n(points[1:k]) - combine_n(points[0:(k - 1)])) / (points[k][0] - points[0][0]) + return (combine_n(points[1:k]) - combine_n(points[0:(k - 1)])) / (points[k - 1][0] - points[0][0]) -def newton_polynom(*points): - for x in range(len(points)): +# returns a polynom that will intersect all supplied points as long as +# the number of points is bigger than 1 +def newton_polynom(points): + for x in range(1, len(points)): - print(combine_n(points[0:x])) + if x == 1: + print(points[0][1], end=' + ') + else: + print(end=' + ') + + print(combine_n(points[0:(x + 1)]), end='') for y in range(x): - print(format(" * (x - %s)", points[y][0])) + print(" * (x - {value:.2f})".format(value=points[y][0]), end='') def test(): - newton_polynom([1, 2], [3, 4], [9, -5]) + newton_polynom([[-1, 5], [2, -1], [3, -1], [4, 5], [8, 9]])