-
-
Notifications
You must be signed in to change notification settings - Fork 47k
Added nevilles algorithm for polynomial interpolation #5447
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
338e976
Added nevilles algorithm for polynomial interpolation
matt-wisdom 784eba2
Added type hinting for neville_interpolate function arguments.
matt-wisdom da087ae
Added more descriptive names
matt-wisdom 026fd5e
Update nevilles_method.py
matt-wisdom 08eabb4
Fixed some linting issues
matt-wisdom 107bde8
Fixed type hinting error
matt-wisdom 3163bb6
Merge branch 'TheAlgorithms:master' into master
matt-wisdom 707bacf
Fixed nevilles_method.py
matt-wisdom 2fd98ab
Merge branch 'TheAlgorithms:master' into master
matt-wisdom 9e32a1c
Add ellipsis for doctest spanning multiple lines
matt-wisdom 1d760bb
Update nevilles_method.py
poyea File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
""" | ||
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_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. | ||
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 "<stdin>", line 1, in <module> | ||
... | ||
TypeError: unsupported operand type(s) for -: 'str' and 'int' | ||
""" | ||
n = len(x_points) | ||
q = [[0] * n for i in range(n)] | ||
for i in range(n): | ||
q[i][1] = y_points[i] | ||
|
||
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]) | ||
|
||
return [q[n - 1][n - 1], q] | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.