]> gitweb.michael.orlitzky.com - sage.d.git/blobdiff - mjo/interpolation.py
Update all doctests to use index[] notation instead of __call__.
[sage.d.git] / mjo / interpolation.py
index 1d594c38913595c213c7a0ffe2c37d42a658eae7..5d65d154e1ce567a7a408c305abcf91596176f4c 100644 (file)
@@ -1,6 +1,26 @@
 from sage.all import *
 from misc import product
 
+
+def lagrange_denominator(k, xs):
+    """
+    Return the denominator of the kth Lagrange coefficient.
+
+    INPUT:
+
+      - ``k`` -- The index of the coefficient.
+
+      - ``xs`` -- The list of points at which the function values are
+        known.
+
+    OUTPUT:
+
+    The product of all xs[j] with j != k.
+
+    """
+    return product([xs[k] - xs[j] for j in range(0, len(xs)) if j != k])
+
+
 def lagrange_coefficient(k, x, xs):
     """
     Returns the coefficient function l_{k}(variable) of y_{k} in the
@@ -12,9 +32,9 @@ def lagrange_coefficient(k, x, xs):
 
     INPUT:
 
-      - ``k`` -- the index of the coefficient.
+      - ``k`` -- The index of the coefficient.
 
-      - ``x`` -- the symbolic variable to use for the first argument
+      - ``x`` -- The symbolic variable to use for the first argument
         of l_{k}.
 
       - ``xs`` -- The list of points at which the function values are
@@ -22,7 +42,7 @@ def lagrange_coefficient(k, x, xs):
 
     OUTPUT:
 
-    A symbolic function of one variable.
+    A symbolic expression of one variable.
 
     TESTS::
 
@@ -31,8 +51,8 @@ def lagrange_coefficient(k, 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])
+    numerator = lagrange_psi(x, xs)/(x - xs[k])
+    denominator = lagrange_denominator(k, xs)
 
     return (numerator / denominator)
 
@@ -53,7 +73,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::
 
@@ -89,10 +109,10 @@ def divided_difference_coefficients(xs):
         [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 ]
+    coeffs = [ QQ(1)/lagrange_denominator(k, xs) for k in range(0, len(xs)) ]
     return coeffs
 
+
 def divided_difference(xs, ys):
     """
     Return the Newton divided difference of the points (xs[k],
@@ -157,7 +177,7 @@ def newton_polynomial(x, xs, ys):
 
     OUTPUT:
 
-    A symbolic function.
+    A symbolic expression.
 
     TESTS:
 
@@ -175,7 +195,120 @@ def newton_polynomial(x, xs, ys):
 
     for k in range(0, degree+1):
         term  = divided_difference(xs[:k+1], ys[:k+1])
-        term *= product([ x - xk for xk in xs[:k]])
+        term *= lagrange_psi(x, 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 ])