X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=mjo%2Finterpolation.py;h=e32ed6d24712da31ff96bcfa6c3c53278abc112a;hb=3cfc8e228ae337aed975118444a8cbad9a5a7ac3;hp=5d65d154e1ce567a7a408c305abcf91596176f4c;hpb=89ea7cefd713fbd44e6838603185a70ace6edf54;p=sage.d.git diff --git a/mjo/interpolation.py b/mjo/interpolation.py index 5d65d15..e32ed6d 100644 --- a/mjo/interpolation.py +++ b/mjo/interpolation.py @@ -1,5 +1,5 @@ from sage.all import * -from misc import product +product = prod def lagrange_denominator(k, xs): @@ -48,7 +48,7 @@ def lagrange_coefficient(k, x, xs): 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 + 1/8*(pi + 6*x)*(pi - 2*x)*(pi - 6*x)*x/pi^4 """ numerator = lagrange_psi(x, xs)/(x - xs[k]) @@ -60,7 +60,7 @@ def lagrange_coefficient(k, x, xs): def lagrange_polynomial(x, xs, ys): """ - Return the Lagrange form of the interpolation polynomial in `x` of + Return the Lagrange form of the interpolating polynomial in `x` at the points (xs[k], ys[k]). INPUT: @@ -94,6 +94,42 @@ def lagrange_polynomial(x, xs, ys): +def lagrange_interpolate(f, x, xs): + """ + Interpolate the function ``f`` at the points ``xs`` using the + Lagrange form of the interpolating polynomial. + + INPUT: + + - ``f`` -- The function to interpolate. + + - ``x`` -- The independent variable of the resulting polynomial. + + - ``xs`` -- A list of points at which to interpolate ``f``. + + OUTPUT: + + A polynomial in ``x`` which interpolates ``f`` at ``xs``. + + EXAMPLES: + + We're exact on polynomials of degree `n` if we use `n+1` points:: + + sage: t = SR.symbol('t', domain='real') + sage: lagrange_interpolate(x^2, t, [-1,0,1]).simplify_rational() + t^2 + + """ + # f should be a function of one variable. + z = f.variables()[0] + # We're really just doing map(f, xs) here; the additional + # gymnastics are to avoid a warning when calling `f` with an + # unnamed argument. + ys = [ f({z: xk}) for xk in xs ] + return lagrange_polynomial(x, xs, ys) + + + def divided_difference_coefficients(xs): """ Assuming some function `f`, compute the coefficients of the @@ -150,7 +186,7 @@ def divided_difference(xs, ys): sage: f = function('f', x) sage: divided_difference([x], [f(x=x)]) f(x) - sage: x1,x2 = var('x1,x2') + sage: x1,x2 = SR.var('x1,x2') sage: divided_difference([x1,x2], [f(x=x1),f(x=x2)]) f(x1)/(x1 - x2) - f(x2)/(x1 - x2)