Polynomial Interpolation
Introduction
Polynomial interpolation is a mathematical technique used to construct a polynomial function that passes through a given set of points. Given a set of n points (x_0, y_0), (x_1, y_1), …, (x_n-1, y_n-1), polynomial interpolation finds a unique polynomial of degree n-1 or less that passes through these points. This technique is widely used in various fields such as computer graphics, numerical analysis, and signal processing.
Implementation
The following code is an implementation of the polynomial interpolation algorithm in OCaml from Owl library. The function polint takes three arguments - xs, ys, and x. The arrays xs and ys represent the x-coordinates and y-coordinates of the n points, respectively. The variable x represents the point at which we want to evaluate the polynomial. The function returns a tuple of two values - the interpolated value of the polynomial at x and the error estimate.
let polint xs ys x =
let n = Array.length xs in
let m = Array.length ys in
let error () =
let s =
Printf.sprintf
"polint requires that xs and ys have the same length, but xs length is %i \
whereas ys length is %i"
n
m
in
Owl_exception.INVALID_ARGUMENT s
in
Owl_exception.verify (m = n) error;
let c = Array.copy ys in
let d = Array.copy ys in
let ns = ref 0 in
let dy = ref 0. in
let dif = ref (abs_float (x -. xs.(0))) in
for i = 0 to n - 1 do
let dift = abs_float (x -. xs.(i)) in
if dift < !dif
then (
ns := i;
dif := dift)
done;
let y = ref ys.(!ns) in
ns := !ns - 1;
for m = 0 to n - 2 do
for i = 0 to n - m - 2 do
let ho = xs.(i) -. x in
let hp = xs.(i + m + 1) -. x in
let w = c.(i + 1) -. d.(i) in
let den = ho -. hp in
assert (den <> 0.);
let den = w /. den in
c.(i) <- ho *. den;
d.(i) <- hp *. den
done;
if (2 * !ns) + 2 < n - m - 1
then dy := c.(!ns + 1)
else (
dy := d.(!ns);
ns := !ns - 1);
y := !y +. !dy
done;
!y, !dy
Step-by-step Explanation
-
The function first checks if the lengths of the arrays
xsandysare the same. If they are not, an exception is raised. -
Two arrays
canddare created, which are initialized to the values in the arrayys. -
The variable
nsis initialized to 0, which will be used to keep track of the index of the closest point tox. -
The variable
difis initialized to the absolute difference betweenxand the first point in the arrayxs. -
A loop is executed over all the points in the array
xs. For each point, the absolute difference betweenxand the point is computed. If this difference is less than the current value ofdif, then the index of the point is stored in the variablens, and the value ofdifis updated to the new difference. -
The value of
yis initialized to the y-coordinate of the point in the arrayysthat is closest tox. -
The variable
nsis decremented by 1. -
A loop is executed n-1 times. In each iteration, two nested loops are executed over the remaining points in the array
xs. The variablemis used to keep track of the number of iterations. -
In the inner loop, the variable
hois initialized to the difference between the current point andx, and the variablehpis initialized to the difference between the point m positions ahead andx. -
The variable
wis initialized to the difference between the y-coordinates of the point m positions ahead and the current point. -
The variable
denis initialized to the difference betweenhoandhp. Ifdenis 0, an exception is raised. -
The variable
denis updated tow/den. -
The value of
cat the current index is updated toho*den, and the value ofdat the current index is updated tohp*den. -
If the index of the closest point to
xis less thann-m-1multiplied by 2, then the value ofdyis updated toc[ns+1]. Otherwise, the value ofdyis updated tod[ns], and the index of the closest point toxis decremented by 1. -
The value of
yis updated toy+dy. -
The function returns the tuple
(y, dy).
Complexity Analysis
The polynomial interpolation algorithm has a time complexity of O(n^2), where n is the number of points. This is because the algorithm involves a nested loop over all the points in the input arrays. In the outer loop, the algorithm iterates n-1 times, and in the inner loop, it iterates over the remaining n-m-1 points. Therefore, the total number of iterations is given by the sum of the first n-1 positive integers, which is n(n-1)/2. Thus, the time complexity of the algorithm is O(n^2).
In terms of space complexity, the algorithm uses two arrays of size n to store the y-coordinates and two additional variables to store the interpolated value and the error estimate. Therefore, the space complexity of the algorithm is O(n).