-def lagrange_polynomial(f, x, xs):
+def lagrange_polynomial(x, xs, ys):
"""
Return the Lagrange form of the interpolation polynomial in `x` of
- `f` at the points `xs`.
+ at the points (xs[k], ys[k]).
INPUT:
- - ``f`` - The function to interpolate.
-
- ``x`` - The independent variable of the resulting polynomial.
- ``xs`` - The list of points at which we interpolate `f`.
+ - ``ys`` - The function values at `xs`.
+
OUTPUT:
- A symbolic function (polynomial) interpolating `f` at `xs`.
+ A symbolic function (polynomial) interpolating each (xs[k], ys[k]).
TESTS::
sage: xs = [ -pi/2, -pi/6, 0, pi/6, pi/2 ]
- sage: L = lagrange_polynomial(sin, x, xs)
+ sage: ys = map(sin, xs)
+ sage: L = lagrange_polynomial(x, xs, ys)
sage: expected = 27/16*(pi - 6*x)*(pi - 2*x)*(pi + 2*x)*x/pi^4
sage: expected -= 1/8*(pi - 6*x)*(pi - 2*x)*(pi + 6*x)*x/pi^4
sage: expected -= 1/8*(pi - 6*x)*(pi + 2*x)*(pi + 6*x)*x/pi^4
True
"""
- ys = [ f(xs[k]) for k in range(0, len(xs)) ]
ls = [ lagrange_coefficient(k, x, xs) for k in range(0, len(xs)) ]
sigma = sum([ ys[k] * ls[k] for k in range(0, len(xs)) ])
return sigma
TESTS:
sage: xs = [ -pi/2, -pi/6, 0, pi/6, pi/2 ]
- sage: L = lagrange_polynomial(sin, x, xs)
+ sage: ys = map(sin, xs)
+ sage: L = lagrange_polynomial(x, xs, ys)
sage: N = newton_polynomial(sin, x, xs)
sage: bool(N == L)
True