Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions python/NevilleInterpolation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import numpy as np
import matplotlib.pyplot as plt
from NevillePoint import nevillepoint

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete blank line


# [USES] interpolations/NevillePoint
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nu mai e nevoie de asta; delete line

# Function that calculates the Neville interpolation polynomial for a vector
# of points (a, b) with a:xs, b:ys in "elements" points between the first of xs
# and the last of xs; it also plots the interpolation
def nevilleinterpolation(xs, ys, elements=100):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nevilleinterpolation -> NevilleInterpolation, please

n = len(xs)
xss = np.linspace(xs[0], xs[n - 1], elements)
# create an empty vector to store the values of the interpolation in the
# xss' points
yss = []

for i in range(0, len(xss)):
# Calculate the approximation of the function in xss(i)
yss.append(nevillepoint(0, n - 1, xss[i], xs, ys))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yss = []
for i in range(0, len(xss)):
  # Calculate the approximation of the function in xss(i)
  yss.append(nevillepoint(0, n - 1, xss[i], xs, ys))

change to

yss = [NevillePoint(0, n - 1, x, xs, ys) for x in xss]


Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

leave only one blank line



print(yss)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete line

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For Python you can use YAPF[1] for automated code formatting - it is based on building the AST of the program and then printing it out directly in the proper format.
Furthermore, for coding conventions (such as how variables should be named) which are harder to fix automatically (because of Python’s dynamic nature), it is still possible to use something like Pylint[2] or Flake8[3] in order to enforce conventions.

Examples of how to configure these tools can also be found in the yapf repo[1], by looking at the proper configuration files (e.g., .style.yapf or pylintrc).

[1] https://github.com/google/yapf
[2] https://www.pylint.org/
[3] http://flake8.pycqa.org/en/latest/

# plot the interpolated function
plt.plot(xss, yss)
# plot the initial points as circles
plt.plot(xs, ys, 'o', "linewidth", 3)
plt.show()

22 changes: 22 additions & 0 deletions python/NevillePoint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Function that calculates the value of the Neville interpolation polynomial
# P_ij(x) for a vector of points (a, b) with a:xs, b:xs in the abscissa x;
# [NOTE] It doesn't use explicit caching (just the internal Octave caching
# system)


def nevillepoint(i, j, x, xs, ys):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nevillepoint -> NevillePoint

# Base case: P_ii(x) = f(x_i)
# ys is 1 - indexed, so we have to add one to the index
if i == j:
y = ys[i]
return y

# x_j - x_i
delta = xs[j] - xs[i]
# P(i, j - 1, x)
pij_ = (xs[j] - x) * nevillepoint(i, j - 1, x, xs, ys)
# P(i + 1, j, x)
pi_j = (x - xs[i]) * nevillepoint(i + 1, j, x, xs, ys)
# P(i, j, x)
y = (pij_ + pi_j) / delta
return y