]> gitweb.michael.orlitzky.com - sage.d.git/blobdiff - mjo/interpolation.py
Make the divided_difference() function rely on divided_difference_coefficients().
[sage.d.git] / mjo / interpolation.py
index 747c680da2f73c777c2f86cf4f20e4cb4300b3a1..1d594c38913595c213c7a0ffe2c37d42a658eae7 100644 (file)
@@ -1,5 +1,5 @@
 from sage.all import *
-load('~/.sage/init.sage')
+from misc import product
 
 def lagrange_coefficient(k, x, xs):
     """
@@ -38,27 +38,28 @@ def lagrange_coefficient(k, x, xs):
 
 
 
-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
@@ -67,76 +68,92 @@ def lagrange_polynomial(f, x, xs):
         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
 
 
-def divided_difference(f, xs):
+
+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(xs, ys):
     """
-    Return the Newton divided difference of `f` at the points
-    `xs`. Reference:
+    Return the Newton divided difference of the points (xs[k],
+    ys[k]). Reference:
 
       http://en.wikipedia.org/wiki/Divided_differences
 
     INPUT:
 
-      - ``f`` -- The function whose divided difference we seek.
+      - ``xs`` -- The list of x-values.
 
-      - ``xs`` -- The list of points at which to compute `f`.
+      - ``ys`` -- The function values at `xs`.
 
     OUTPUT:
 
-    The divided difference of `f` at ``xs``.
+    The (possibly symbolic) divided difference function.
 
     TESTS::
 
-        sage: divided_difference(sin, [0])
+        sage: xs = [0]
+        sage: ys = map(sin, xs)
+        sage: divided_difference(xs, ys)
         0
-        sage: divided_difference(sin, [0, pi])
+        sage: xs = [0, pi]
+        sage: ys = map(sin, xs)
+        sage: divided_difference(xs, ys)
         0
-        sage: divided_difference(sin, [0, pi, 2*pi])
+        sage: xs = [0, pi, 2*pi]
+        sage: ys = map(sin, xs)
+        sage: divided_difference(xs, ys)
         0
 
     We try something entirely symbolic::
 
         sage: f = function('f', x)
-        sage: divided_difference(f, [x])
+        sage: divided_difference([x], [f(x=x)])
         f(x)
         sage: x1,x2 = var('x1,x2')
-        sage: divided_difference(f, [x1,x2])
-        (f(x1) - f(x2))/(x1 - x2)
+        sage: divided_difference([x1,x2], [f(x=x1),f(x=x2)])
+        f(x1)/(x1 - x2) - 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):
+    coeffs = divided_difference_coefficients(xs)
+    v_cs = vector(coeffs)
+    v_ys = vector(ys)
+    return v_cs.dot_product(v_ys)
+
+
+def newton_polynomial(x, xs, ys):
     """
-    Return the Newton form of the interpolating polynomial of `f` at
-    the points `xs` in the variable `x`.
+    Return the Newton form of the interpolating polynomial of the
+    points (xs[k], ys[k]) 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`.
+      - ``xs`` -- The list of x-values.
+
+      - ``ys`` -- The function values at `xs`.
 
     OUTPUT:
 
@@ -145,8 +162,9 @@ def newton_polynomial(f, x, xs):
     TESTS:
 
         sage: xs = [ -pi/2, -pi/6, 0, pi/6, pi/2 ]
-        sage: L = lagrange_polynomial(sin, x, xs)
-        sage: N = newton_polynomial(sin, x, xs)
+        sage: ys = map(sin, xs)
+        sage: L = lagrange_polynomial(x, xs, ys)
+        sage: N = newton_polynomial(x, xs, ys)
         sage: bool(N == L)
         True
 
@@ -156,7 +174,7 @@ def newton_polynomial(f, x, xs):
     N = SR(0)
 
     for k in range(0, degree+1):
-        term  = divided_difference(f, xs[:k+1])
+        term  = divided_difference(xs[:k+1], ys[:k+1])
         term *= product([ x - xk for xk in xs[:k]])
         N += term