from sage.all import * from misc import product def lagrange_coefficient(k, x, xs): """ Returns the coefficient function l_{k}(variable) of y_{k} in the Lagrange polynomial of f. See, http://en.wikipedia.org/wiki/Lagrange_polynomial for more information. INPUT: - ``k`` -- the index of the coefficient. - ``x`` -- the symbolic variable to use for the first argument of l_{k}. - ``xs`` -- The list of points at which the function values are known. OUTPUT: A symbolic function of one variable. TESTS:: sage: xs = [ -pi/2, -pi/6, 0, pi/6, pi/2 ] sage: lagrange_coefficient(0, x, xs) 1/8*(pi - 6*x)*(pi - 2*x)*(pi + 6*x)*x/pi^4 """ numerator = product([x - xs[j] for j in range(0, len(xs)) if j != k]) denominator = product([xs[k] - xs[j] for j in range(0, len(xs)) if j != k]) return (numerator / denominator) def lagrange_polynomial(x, xs, ys): """ Return the Lagrange form of the interpolation polynomial in `x` of at the points (xs[k], ys[k]). INPUT: - ``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 each (xs[k], ys[k]). TESTS:: sage: xs = [ -pi/2, -pi/6, 0, pi/6, pi/2 ] 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 sage: expected += 27/16*(pi - 2*x)*(pi + 2*x)*(pi + 6*x)*x/pi^4 sage: bool(L == expected) True """ 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 def divided_difference_coefficients(xs): """ Assuming some function `f`, compute the coefficients of the divided difference f[xs[0], ..., xs[n]]. TESTS: sage: divided_difference_coefficients([0]) [1] sage: divided_difference_coefficients([0, pi]) [-1/pi, 1/pi] sage: divided_difference_coefficients([0, pi, 2*pi]) [1/2/pi^2, -1/pi^2, 1/2/pi^2] """ coeffs = [ product([ (QQ(1) / (xj - xi)) for xi in xs if xi != xj ]) for xj in xs ] return coeffs def divided_difference(f, xs): """ Return the Newton divided difference of `f` at the points `xs`. Reference: http://en.wikipedia.org/wiki/Divided_differences INPUT: - ``f`` -- The function whose divided difference we seek. - ``xs`` -- The list of points at which to compute `f`. OUTPUT: The divided difference of `f` at ``xs``. TESTS:: sage: divided_difference(sin, [0]) 0 sage: divided_difference(sin, [0, pi]) 0 sage: divided_difference(sin, [0, pi, 2*pi]) 0 We try something entirely symbolic:: sage: f = function('f', x) sage: divided_difference(f, [x]) f(x) sage: x1,x2 = var('x1,x2') sage: divided_difference(f, [x1,x2]) (f(x1) - f(x2))/(x1 - x2) """ if (len(xs) == 1): # Avoid that goddamned DeprecationWarning when we have a named # argument but don't know what it is. if len(f.variables()) == 0: return f(xs[0]) else: v = f.variables()[0] return f({ v: xs[0] }) # Use the recursive definition. numerator = divided_difference(f, xs[1:]) numerator -= divided_difference(f, xs[:-1]) return numerator / (xs[-1] - xs[0]) def newton_polynomial(f, x, xs): """ Return the Newton form of the interpolating polynomial of `f` at the points `xs` in the variable `x`. INPUT: - ``f`` -- The function to interpolate. - ``x`` -- The independent variable to use for the interpolating polynomial. - ``xs`` -- The list of points at which to interpolate `f`. OUTPUT: A symbolic function. TESTS: sage: xs = [ -pi/2, -pi/6, 0, pi/6, pi/2 ] sage: ys = map(sin, xs) sage: L = lagrange_polynomial(x, xs, ys) sage: N = newton_polynomial(sin, x, xs) sage: bool(N == L) True """ degree = len(xs) - 1 N = SR(0) for k in range(0, degree+1): term = divided_difference(f, xs[:k+1]) term *= product([ x - xk for xk in xs[:k]]) N += term return N