]> gitweb.michael.orlitzky.com - sage.d.git/commitdiff
Add the lagrange_interpolate() function, which lets you avoid evaluating your functio...
authorMichael Orlitzky <michael@orlitzky.com>
Wed, 28 Nov 2012 02:54:39 +0000 (21:54 -0500)
committerMichael Orlitzky <michael@orlitzky.com>
Wed, 28 Nov 2012 02:54:39 +0000 (21:54 -0500)
mjo/interpolation.py

index 5d65d154e1ce567a7a408c305abcf91596176f4c..393908670054a9740203de64c3e0338e10f689c0 100644 (file)
@@ -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