From 338e9760de6de021be69d55a6c494e1ac25777c5 Mon Sep 17 00:00:00 2001 From: Matthew Wisdom Date: Tue, 19 Oct 2021 07:41:05 -0700 Subject: [PATCH 1/9] Added nevilles algorithm for polynomial interpolation --- maths/nevilles_method.py | 64 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 maths/nevilles_method.py diff --git a/maths/nevilles_method.py b/maths/nevilles_method.py new file mode 100644 index 000000000000..ceb6bee731e1 --- /dev/null +++ b/maths/nevilles_method.py @@ -0,0 +1,64 @@ +""" + Python program to show how to interpolate and evaluate a polynomial + using Neville's method. + Neville’s method evaluates a polynomial that passes through a + given set of x and y points for a particular x value (x0) using the + Newton polynomial form. + Reference: + https://rpubs.com/aaronsc32/nevilles-method-polynomial-interpolation +""" + + +def neville_interpolate(x, y, x0) -> list: + """ + Interpolate and evaluate a polynomial using Neville's method. + + Arguments: + x, y: Iterables of x and corresponding y points through which the + polynomial passes. + x0: The value of x to evaluate the polynomial for. + + Return Value: A list of the approximated value and the Neville iterations + table respectively. + + >>> import pprint + + >>> neville_interpolate((1,2,3,4,6), (6,7,8,9,11), 5)[0] + 10.0 + + >>> pprint.pprint(neville_interpolate((1,2,3,4,6), (6,7,8,9,11), 99)[1]) + [[0, 6, 0, 0, 0], + [0, 7, 0, 0, 0], + [0, 8, 104.0, 0, 0], + [0, 9, 104.0, 104.0, 0], + [0, 11, 104.0, 104.0, 104.0]] + + + >>> neville_interpolate((1,2,3,4,6), (6,7,8,9,11), 99)[0] + 104.0 + + >>> neville_interpolate((1,2,3,4,6), (6,7,8,9,11), '') + Traceback (most recent call last): + File "", line 1, in + File "/home/mattwisdom/thealgo/nevilles_method.py", line 35, in neville_interpola\ +te + for i in range(2, n): + TypeError: unsupported operand type(s) for -: 'str' and 'int' + """ + n = len(x) + q = [[0] * n for i in range(n)] + for i in range(n): + q[i][1] = y[i] + + for i in range(2, n): + for j in range(i, n): + q[j][i] = ((x0 - x[j - i + 1]) * q[j][i - 1] - ( + x0 - x[j]) * q[j - 1][i - 1]) / (x[j] - x[j - i + 1]) + + return [q[n - 1][n - 1], q] + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From 784eba29b814c2540d8a8f0d28ccddd6bd635503 Mon Sep 17 00:00:00 2001 From: Matthew Wisdom Date: Tue, 19 Oct 2021 20:06:31 +0100 Subject: [PATCH 2/9] Added type hinting for neville_interpolate function arguments. --- maths/nevilles_method.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/nevilles_method.py b/maths/nevilles_method.py index ceb6bee731e1..8f782e5aaf00 100644 --- a/maths/nevilles_method.py +++ b/maths/nevilles_method.py @@ -9,7 +9,7 @@ """ -def neville_interpolate(x, y, x0) -> list: +def neville_interpolate(x: list, y: list, x0: int) -> list: """ Interpolate and evaluate a polynomial using Neville's method. From da087ae6679e39bd83a13ff435fb14ab57594ef5 Mon Sep 17 00:00:00 2001 From: Matthew Wisdom Date: Tue, 19 Oct 2021 20:08:46 +0100 Subject: [PATCH 3/9] Added more descriptive names --- maths/nevilles_method.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/maths/nevilles_method.py b/maths/nevilles_method.py index 8f782e5aaf00..3a9589339671 100644 --- a/maths/nevilles_method.py +++ b/maths/nevilles_method.py @@ -9,12 +9,12 @@ """ -def neville_interpolate(x: list, y: list, x0: int) -> list: +def neville_interpolate(x_points: list, y_points: list, x0: int) -> list: """ Interpolate and evaluate a polynomial using Neville's method. Arguments: - x, y: Iterables of x and corresponding y points through which the + x_points, y_points: Iterables of x and corresponding y points through which the polynomial passes. x0: The value of x to evaluate the polynomial for. From 026fd5e08687642bec2cf3e39a6db87f1ce311d7 Mon Sep 17 00:00:00 2001 From: Matthew Wisdom Date: Tue, 19 Oct 2021 20:10:19 +0100 Subject: [PATCH 4/9] Update nevilles_method.py --- maths/nevilles_method.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/maths/nevilles_method.py b/maths/nevilles_method.py index 3a9589339671..2e3440f78854 100644 --- a/maths/nevilles_method.py +++ b/maths/nevilles_method.py @@ -45,15 +45,15 @@ def neville_interpolate(x_points: list, y_points: list, x0: int) -> list: for i in range(2, n): TypeError: unsupported operand type(s) for -: 'str' and 'int' """ - n = len(x) + n = len(x_points) q = [[0] * n for i in range(n)] for i in range(n): - q[i][1] = y[i] + q[i][1] = y_points[i] for i in range(2, n): for j in range(i, n): - q[j][i] = ((x0 - x[j - i + 1]) * q[j][i - 1] - ( - x0 - x[j]) * q[j - 1][i - 1]) / (x[j] - x[j - i + 1]) + q[j][i] = ((x0 - x_points[j - i + 1]) * q[j][i - 1] - ( + x0 - x_points[j]) * q[j - 1][i - 1]) / (x_points[j] - x_points[j - i + 1]) return [q[n - 1][n - 1], q] From 08eabb42e662010a8ced09fdfa4edc75d6d8da12 Mon Sep 17 00:00:00 2001 From: Matthew Wisdom Date: Tue, 19 Oct 2021 20:22:16 +0100 Subject: [PATCH 5/9] Fixed some linting issues --- maths/nevilles_method.py | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/maths/nevilles_method.py b/maths/nevilles_method.py index 2e3440f78854..c1b8ec73d477 100644 --- a/maths/nevilles_method.py +++ b/maths/nevilles_method.py @@ -12,36 +12,28 @@ def neville_interpolate(x_points: list, y_points: list, x0: int) -> list: """ Interpolate and evaluate a polynomial using Neville's method. - Arguments: - x_points, y_points: Iterables of x and corresponding y points through which the - polynomial passes. + x_points, y_points: Iterables of x and corresponding y points through + which the polynomial passes. x0: The value of x to evaluate the polynomial for. - Return Value: A list of the approximated value and the Neville iterations table respectively. - >>> import pprint - >>> neville_interpolate((1,2,3,4,6), (6,7,8,9,11), 5)[0] 10.0 - >>> pprint.pprint(neville_interpolate((1,2,3,4,6), (6,7,8,9,11), 99)[1]) [[0, 6, 0, 0, 0], [0, 7, 0, 0, 0], [0, 8, 104.0, 0, 0], [0, 9, 104.0, 104.0, 0], [0, 11, 104.0, 104.0, 104.0]] - - >>> neville_interpolate((1,2,3,4,6), (6,7,8,9,11), 99)[0] 104.0 - >>> neville_interpolate((1,2,3,4,6), (6,7,8,9,11), '') Traceback (most recent call last): File "", line 1, in - File "/home/mattwisdom/thealgo/nevilles_method.py", line 35, in neville_interpola\ -te + File "/home/mattwisdom/thealgo/nevilles_method.py", line 35, in neville_interpo\ +late for i in range(2, n): TypeError: unsupported operand type(s) for -: 'str' and 'int' """ @@ -53,7 +45,8 @@ def neville_interpolate(x_points: list, y_points: list, x0: int) -> list: for i in range(2, n): for j in range(i, n): q[j][i] = ((x0 - x_points[j - i + 1]) * q[j][i - 1] - ( - x0 - x_points[j]) * q[j - 1][i - 1]) / (x_points[j] - x_points[j - i + 1]) + x0 - x_points[j]) * q[j - 1][i - 1]) / ( + x_points[j] - x_points[j - i + 1]) return [q[n - 1][n - 1], q] From 107bde8d3985efd2db9130b6ab8d192f3fe57efb Mon Sep 17 00:00:00 2001 From: Matthew Wisdom Date: Tue, 19 Oct 2021 20:37:34 +0100 Subject: [PATCH 6/9] Fixed type hinting error --- maths/nevilles_method.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/nevilles_method.py b/maths/nevilles_method.py index c1b8ec73d477..e03ce66e4885 100644 --- a/maths/nevilles_method.py +++ b/maths/nevilles_method.py @@ -9,7 +9,7 @@ """ -def neville_interpolate(x_points: list, y_points: list, x0: int) -> list: +def neville_interpolate(x_points: list, y_points: list, x0: float) -> list: """ Interpolate and evaluate a polynomial using Neville's method. Arguments: From 707bacf3ef452e9c8038683e427c0ba75047ce51 Mon Sep 17 00:00:00 2001 From: Matthew Wisdom Date: Wed, 20 Oct 2021 14:12:00 -0700 Subject: [PATCH 7/9] Fixed nevilles_method.py --- maths/nevilles_method.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/maths/nevilles_method.py b/maths/nevilles_method.py index e03ce66e4885..4d2d952ea1df 100644 --- a/maths/nevilles_method.py +++ b/maths/nevilles_method.py @@ -9,7 +9,7 @@ """ -def neville_interpolate(x_points: list, y_points: list, x0: float) -> list: +def neville_interpolate(x_points: list, y_points: list, x0: int) -> list: """ Interpolate and evaluate a polynomial using Neville's method. Arguments: @@ -44,9 +44,10 @@ def neville_interpolate(x_points: list, y_points: list, x0: float) -> list: for i in range(2, n): for j in range(i, n): - q[j][i] = ((x0 - x_points[j - i + 1]) * q[j][i - 1] - ( - x0 - x_points[j]) * q[j - 1][i - 1]) / ( - x_points[j] - x_points[j - i + 1]) + q[j][i] = ( + (x0 - x_points[j - i + 1]) * q[j][i - 1] + - (x0 - x_points[j]) * q[j - 1][i - 1] + ) / (x_points[j] - x_points[j - i + 1]) return [q[n - 1][n - 1], q] From 9e32a1cb637c5b2afd334fd89e8b3b271731da10 Mon Sep 17 00:00:00 2001 From: Matthew Wisdom Date: Thu, 21 Oct 2021 08:59:30 +0100 Subject: [PATCH 8/9] Add ellipsis for doctest spanning multiple lines --- maths/nevilles_method.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/maths/nevilles_method.py b/maths/nevilles_method.py index 4d2d952ea1df..530f68402a4c 100644 --- a/maths/nevilles_method.py +++ b/maths/nevilles_method.py @@ -32,8 +32,8 @@ def neville_interpolate(x_points: list, y_points: list, x0: int) -> list: >>> neville_interpolate((1,2,3,4,6), (6,7,8,9,11), '') Traceback (most recent call last): File "", line 1, in - File "/home/mattwisdom/thealgo/nevilles_method.py", line 35, in neville_interpo\ -late + File "/home/mattwisdom/thealgo/nevilles_method.py", line 35, in neville_interpo + ... late for i in range(2, n): TypeError: unsupported operand type(s) for -: 'str' and 'int' """ From 1d760bba256ddd270481634f820bc09ed3a86b48 Mon Sep 17 00:00:00 2001 From: John Law Date: Tue, 2 Nov 2021 17:57:53 +0800 Subject: [PATCH 9/9] Update nevilles_method.py --- maths/nevilles_method.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/maths/nevilles_method.py b/maths/nevilles_method.py index 530f68402a4c..5583e4269b32 100644 --- a/maths/nevilles_method.py +++ b/maths/nevilles_method.py @@ -32,9 +32,7 @@ def neville_interpolate(x_points: list, y_points: list, x0: int) -> list: >>> neville_interpolate((1,2,3,4,6), (6,7,8,9,11), '') Traceback (most recent call last): File "", line 1, in - File "/home/mattwisdom/thealgo/nevilles_method.py", line 35, in neville_interpo - ... late - for i in range(2, n): + ... TypeError: unsupported operand type(s) for -: 'str' and 'int' """ n = len(x_points)