]> gitweb.michael.orlitzky.com - sage.d.git/blobdiff - mjo/interpolation.py
Add the SymbolSequence class; eventually headed upstream.
[sage.d.git] / mjo / interpolation.py
index d5011ea47e154048b79396f4141fb907597bc614..8c3936edabaf5aff434f5f1728edffa899330f6d 100644 (file)
@@ -22,7 +22,7 @@ def lagrange_coefficient(k, x, xs):
 
     OUTPUT:
 
-    A symbolic function of one variable.
+    A symbolic expression of one variable.
 
     TESTS::
 
@@ -53,7 +53,7 @@ def lagrange_polynomial(x, xs, ys):
 
     OUTPUT:
 
-    A symbolic function (polynomial) interpolating each (xs[k], ys[k]).
+    A symbolic expression (polynomial) interpolating each (xs[k], ys[k]).
 
     TESTS::
 
@@ -93,81 +93,79 @@ def divided_difference_coefficients(xs):
                for xj in xs ]
     return coeffs
 
-def divided_difference(f, xs):
+
+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])
+    coeffs = divided_difference_coefficients(xs)
+    v_cs = vector(coeffs)
+    v_ys = vector(ys)
+    return v_cs.dot_product(v_ys)
 
 
-def newton_polynomial(f, x, xs):
+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:
 
-    A symbolic function.
+    A symbolic expression.
 
     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: N = newton_polynomial(x, xs, ys)
         sage: bool(N == L)
         True
 
@@ -177,8 +175,120 @@ 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
 
     return N
+
+
+def hermite_coefficient(k, x, xs):
+    """
+    Return the Hermite coefficient h_{k}(x). See Atkinson, p. 160.
+
+    INPUT:
+
+      - ``k`` -- The index of the coefficient.
+
+      - ``x`` -- The symbolic variable to use as the argument of h_{k}.
+
+      - ``xs`` -- The list of points at which the function values are
+        known.
+
+    OUTPUT:
+
+    A symbolic expression.
+
+    """
+    lk = lagrange_coefficient(k, x, xs)
+    return (1 - 2*lk.diff(x)(x=xs[k])*(x - xs[k]))*(lk**2)
+
+
+def hermite_deriv_coefficient(k, x, xs):
+    """
+    Return the Hermite derivative coefficient, \tilde{h}_{k}(x). See
+    Atkinson, p. 160.
+
+    INPUT:
+
+      - ``k`` -- The index of the coefficient.
+
+      - ``x`` -- The symbolic variable to use as the argument of h_{k}.
+
+      - ``xs`` -- The list of points at which the function values are
+        known.
+
+    OUTPUT:
+
+    A symbolic expression.
+
+    """
+    lk = lagrange_coefficient(k, x, xs)
+    return (x - xs[k])*(lk**2)
+
+
+def hermite_interpolant(x, xs, ys, y_primes):
+    """
+    Return the Hermite interpolant `H(x)` such that H(xs[k]) = ys[k]
+    and H'(xs[k]) = y_primes[k] for each k.
+
+    Reference: Atkinson, p. 160.
+
+    INPUT:
+
+      - ``x`` -- The symbolic variable to use as the argument of H(x).
+
+      - ``xs`` -- The list of points at which the function values are
+        known.
+
+      - ``ys`` -- The function values at the `xs`.
+
+      - ``y_primes`` -- The derivatives at the `xs`.
+
+    OUTPUT:
+
+    A symbolic expression.
+
+    TESTS:
+
+        sage: xs = [ 0, pi/6, pi/2 ]
+        sage: ys = map(sin, xs)
+        sage: y_primes = map(cos, xs)
+        sage: H = hermite_interpolant(x, xs, ys, y_primes)
+        sage: expected  = -27/4*(pi - 6*x)*(pi - 2*x)^2*sqrt(3)*x^2/pi^4
+        sage: expected += (5*(pi - 2*x)/pi + 1)*(pi - 6*x)^2*x^2/pi^4
+        sage: expected += 81/2*((pi - 6*x)/pi + 1)*(pi - 2*x)^2*x^2/pi^4
+        sage: expected += (pi - 6*x)^2*(pi - 2*x)^2*x/pi^4
+        sage: bool(H == expected)
+        True
+
+    """
+    s1 = sum([ ys[k] * hermite_coefficient(k, x, xs)
+               for k in range(0, len(xs)) ])
+
+    s2 = sum([ y_primes[k] * hermite_deriv_coefficient(k, x, xs)
+               for k in range(0, len(xs)) ])
+
+    return (s1 + s2)
+
+
+def lagrange_psi(x, xs):
+    """
+    The function,
+
+      Psi(x) = (x - xs[0])*(x - xs[1])* ... *(x - xs[-1])
+
+    used in Lagrange and Hermite interpolation.
+
+    INPUT:
+
+      - ``x`` -- The independent variable of the resulting expression.
+
+      - ``xs`` -- A list of points.
+
+    OUTPUT:
+
+    A symbolic expression in one variable, `x`.
+    """
+
+    return product([ (x - xj) for xj in xs ])